Hi guys,
I’m having a bit of a problem with at script and I wondered if anyone with the knowledge could have a look:)
I’m basicly trying to trigger some animations when the player moves the mouse left/right and up/down, you know the typical thing you would see in a modern FPS. The problem I’m having is that the script only seem to trigger the “MachinGunRight” animation. I would also like to have the machin gun center itself when there’s no movement at all. Does anyone see whats wrong?
private var lastMouseX : float = 0;
function Start () {
lastMouseX = Input.GetAxis("Mouse X");
}
function Update (){
if(lastMouseX != Input.GetAxis("Mouse X")){
if(Input.GetAxis("Mouse X") < 0)
{
animation.CrossFade("MachineGunLeft");
}else if(Input.GetAxis("Mouse X") > 0)
{
animation.CrossFade("MachineGunRight");
}
animation["MachineGunRight"].speed = Input.GetAxis("Mouse X")*0.5;
animation["MachineGunLeft"].speed = Input.GetAxis("Mouse X")*0.5;
lastMouseX = Input.GetAxis("Mouse X");
}
}
I can only see where you would need to add in the centering. Though I am a little confused as to why you are creating a left and right machine gun animation. Is this to handle rotation of the gun, or is it to handle the jog of the gun while moving in that direction? (all rotation should be handled by the character controller/mouse look scripts)
code cleanups and adding in the centering.
function Update (){
var input=Input.GetAxis("Mouse X");
if(Mathf.Abs(input)>0.0){
if(input < 0)
animation.CrossFade("MachineGunLeft");
if(input > 0)
animation.CrossFade("MachineGunRight");
animation["MachineGunRight"].speed = input*0.5;
animation["MachineGunLeft"].speed = Mathf.Abs(input)*0.5;
lastMouseX = input;
} else
animation.CrossFade("MachineGunCenter");
}
Thanks for the cleanup BigMisterB, much appreciated:) The animation is to create a more dynamic gun handling. Instead of having the gun static I want it to sway according to which way the player is looking.
It’s sorta working, at least it now sway both right and left, instead of only right (which was the case before your input:) ),but as you can see in the video (link below) it’s jumping quite around. the animation doesn’t crossfade into the center animation, it just “click” so to speak.
http://dl.dropbox.com/u/32042/GunSway.avi
hmm… perhaps you need to only crossfade once (you are crossfading every frame currently)
Try this:
private var lastAnimation="";
function Update (){
var input=Input.GetAxis("Mouse X");
var currentAnimation="";
var speed=1.0;
if(Mathf.Abs(input)>0.0){
if(input < 0)
currentAnimation="MachineGunLeft";
if(input > 0)
currentAnimation="MachineGunRight";
speed=Mathf.Abs(input)*0.5;
lastMouseX = input;
} else
currentAnimation="MachineGunCenter";
if(lastAnimation != currentAnimation){
animation.CrossFade(currentAnimation);
animation[currentAnimation].speed = speed;
lastAnimation=currentAnimation;
}
}
Everything seem to be smoother now and less “clicks”, but it still doesn’t crossfade smothly into the center or main gun “pose”. Maybe it has something to do with the animation itself?
Here’s how it looks now:
http://dl.dropbox.com/u/32042/GunSway2.avi