script to use action animation with mouse movement

im using this script to use action animation with mouse movement:

import bge
GameLogic = bge.logic
from Rasterizer import *
    
cont = GameLogic.getCurrentController()


obj = cont.owner


mouse = cont.sensors["mouse"]


Action = cont.actuators['Action']


Sensitivity = 0.10


CenterY = getWindowHeight()//2


CenterX = getWindowWidth()//2


if mouse.positive:
    bge.render.setMousePosition(int(CenterX), int(CenterY))
    
if mouse.positive:
    YPosition = mouse.position[1]
    xRot = (CenterY - YPosition)*Sensitivity
    obj["prop"] -= xRot
    if obj["prop"] < -80:
        obj["prop"] = -80
    if obj["prop"] > 80:
        obj["prop"] = 80
    setMousePosition(CenterX,CenterY)
        
cont.activate("Action")

if mouse.positive:
bge.render.setMousePosition(int(CenterX), int(CenterY))

if mouse.positive:
YPosition = mouse.position[1]…

why “if mouse.positive:” must be repeated or the script will not work?
thank you for any help

Attachments

testing1.blend (377 KB)

Because you get errors (see console)

btw. the activation of the controller makes most sense when inside the if-block (when the property changed).

is there any one can post a better code to use action animation with mouse movement
thank you for any help

im not really sure what the use of this is, so ill just make some assumptions.

Always(True Pulse) -> Script -> Action, “frame” is the property

import bge

cont = bge.logic.getCurrentController()
owner = cont.owner

mouse = bge.logic.mouse
msX = (mouse.position[0]-0.5)*2
msY = (mouse.position[1]-0.5)*2

zone = 0.1  #deadzone !! cannot be 1 !!
factor = 100  #frame range
frame = 0  #property center value

## Set Frame Based on Cursor Distance from Center ##
cont.activate(cont.actuators["Action"])
ngzn = (1/(1-zone))

if msX > zone:
    frame = ((msX-zone)*ngzn)*factor

elif msX < -zone:
    frame = ((msX+zone)*ngzn)*factor

owner["frame"] = frame

## Center Mouse ##
mouse.position = (0.5, 0.5)

What do you mean with “better”?

The method you used is pretty simple and straight forward:

  • change a property value via mouse input
  • play an action pose according to the property value

You can even remove the “cont.activate” and activate the action actuator with a simple AND controller. I suggested to move it into the if-block as you do not need to activate the controller when the mouse was not moved. The code itself does not need the actuator.

An alternative implementation would be to play the action pose via Python. It is not “better”. It is just a different implementation.

your script is good but when you keep move the mouse the property values keep go behind max animation value.
how to make the property values stop in the max values of the animation? if the animation end in 80 the property value should end in 80.
thank you for your help

this is a rough sketch, im sure alot more fine tuning can be done. however, i would need to know precisely ALL the case scenarios it will be used.

the precision of the tracking is problematic, i know. if i knew the use for it, perhaps we could use a different control scheme all together.



import bge

GameLogic = bge.logic

from Rasterizer import *
    
cont = GameLogic.getCurrentController()




obj = cont.owner




mouse = cont.sensors["mouse"]




Action = cont.actuators['Action']




Sensitivity = 0.10




CenterY = getWindowHeight()//2




CenterX = getWindowWidth()//2




if mouse.positive:
    bge.render.setMousePosition(int(CenterX), int(CenterY))
    
if mouse.positive:
    YPosition = mouse.position[1]
    xRot = (CenterY - YPosition)*Sensitivity
    obj["prop"] -= xRot
    if obj["prop"] < -80:
        obj["prop"] = -80
    if obj["prop"] > 80:
        obj["prop"] = 80
    setMousePosition(CenterX,CenterY)
        
cont.activate("Action")

im trying to make this script better to use for aim offset animation.
the script right now work great, the property values stop in max value of animation but it’s hard to aim to target.
i want to make it aim more smoothly.

Attachments

aiming.blend (1.09 MB)

then you are going about this wrong. what you want is a graphical representation of what the computer is doing.

[see edit]

now you can use a basic mouselook actuator on the camera to get your rotation limits, and have the animation just follow along. raycast from the center of the head straight forward, this will give the point the character is looking at. now, raycast from the barrel of your gun to that eye sight point we found when the gun shoots to see if a target was hit. the gun can be pointed at the eye sight point too.

slow projectiles can be added at the barrel and the forward axis aligned to the eye sight point. then the logic on the ammo can take over, homing, bouncing, etc.

demo blend coming soon, unless someone beats me to it.

EDIT:
just realized dots are circular, way better to just grab the euler rotation directly. DUH…:spin:

can’t wait for your demo, thank you very much

heres a collection of concepts that should prove useful. not the most refined, but it works.

Attachments

anim tracking.blend (999 KB)