Sometimes the best "support" in python is finished scripts

Some noobs to python don’t realy want to know how to write python just get a script that does the job they want. They want scripts they can copy. Im going to post a few scripts here.:stuck_out_tongue:

This script will aim at any object with the property “bg”. Add this script to an object to have it aim at the closest object with bg. This is usefull to create heat seaking objects or have an auto aim feature in a game.


 
###########################
# Aim At Enemy Script
# Created by kylona
# make sure to add the property bg
#
# enjoy
###########################
import math
objectlist = GameLogic.getCurrentScene().getObjectList()
controller = GameLogic.getCurrentController()
p1 = controller.getOwner()
enemies = []
distance = []
for object in objectlist:
# you can change "bg" to "(new property name)" if you want a differt property name to activate it.
if hasattr(object, "bg"):
enemies.append(object)
 
#print enemies
for object in enemies:
x = object.getPosition()[0]
y = object.getPosition()[1]
p1x = p1.getPosition()[0]
p1y = p1.getPosition()[1]
dist = math.sqrt(math.pow((x-p1x),2) + math.pow((y-p1y),2))
distance.append(dist)
 
#print distance
close = min(distance)
#print close
idx = distance.index(close)
closob = enemies[idx]
#print closob
pi = 3.141592653589793238462
controller = GameLogic.getCurrentController()
p2 = controller.getOwner()
p1 = closob
p1x = p1.getPosition()[0]
p1y = p1.getPosition()[1]
p1z = p1.getPosition()[2]
pex = p2.getPosition()[0]
pey = p2.getPosition()[1]
pez = p2.getPosition()[2]
xdist = (p1x - pex)
ydist = (p1y - pey)
 
p2ang = [0.0, 0.0, (((math.atan((ydist/xdist)))))]
p2rot= [0.0, 0.0, (pi+((math.atan((ydist/xdist)))))]
#print p2ang
if xdist > 0:
p2.setOrientation(p2ang)
if xdist < 0:
p2.setOrientation(p2rot)
del enemies
del distance
print "lists should be empty"
enemies = []
distance = []
#print enemies
#print distance

Attachments


just add this script to any object in the scene. and dont forget to put your camera names at the top of the script. I got this script form a tutorial on tutorialsforblender3d.com and added the camera name change at the top of the script.

 
##### Viewports.py
cam1 = "OBCamera"
cam2 = "OBp1Camera"
################## viewport needs to know game window height and width
# import Rasterizer module
import Rasterizer
# get game window height and width
height = Rasterizer.getWindowHeight()
width = Rasterizer.getWindowWidth()
################# Using 2 cameras for the player viewports. Need to get them.
# get the current scene
scene = GameLogic.getCurrentScene()
# get list in objects in scene
objList = scene.getObjectList()
# use Camera ( the active camera) for PlayerA
playerA_cam = objList[cam1]
# use Camera.001 for PlayerB
playerB_cam = objList[cam2]
################# Use the top half of the game window for Player A. 
# Player A viewport: top half
left_A = 0
bottom_A = 0
right_A = width /2
top_A = height
# set Player A viewport
playerA_cam.setViewport( left_A, bottom_A, right_A, top_A)
#################Use bottom half for player B. 
# Player B viewport: bottom half
left_B = width /2
bottom_B = 0
right_B = width 
top_B = height
# set Player B viewport
playerB_cam.setViewport( left_B, bottom_B, right_B, top_B)
################## Use the viewports
# use viewport 
playerA_cam.enableViewport(True)
playerB_cam.enableViewport(True)