Ok so for the animations for my character I need to calculate how fast my player is moving and if hes moving side to side or forward or back to play the proper animations, I can’t use the input to help with that as I’m using the iphone and joysticks to get the input, so depending on how my character is turned, the different values from the joysticks would be messed up when calculating direction. How would I go about doing this? All I need is a point in the right direction. Thanks.
Is it a charactercontroller, or a rigidbody?
charactercontroller
var controller : CharacterController = GetComponent(CharacterController);
if(controller.velocity.z > 0.1){
//I am moving forward
}
else if(controller.velocity.z < -0.1){
//I am moving backward
}
else if(controller.velocity.x > 0.1){
//I am strafing right
}
else if(controller.velocity.x < -0.1){
//I am strafing left
}
You’ll have to add cases for moving diagonally. I didn’t feel like typing anymore, but you get the idea
Can’t believe I didn’t think of that, Thanks!
That worked kinda, all the animations played, but when I turned my character to the side even though he was moving forward it was playing the sidestep animation, how would I go about fixing? Thanks.
Anyone? Heres the functions of my control script that govern the movement, look, and movement animations.
function Look(){
if(!autoLook){
ForwardLook = Vector3(Joystick.position.x, 0, Joystick.position.y);
}
else{
ForwardLook = Vector3(LookJoystick.position.x, 0, LookJoystick.position.y);
}
var newForward = Vector3.Slerp(transform.forward, ForwardLook.normalized, maxRotationSpeed * Time.deltaTime);
transform.forward = newForward;
}
function Move(){
var movement = Vector3( Joystick.position.x, 0, Joystick.position.y );
movement.y = 0;
movement.Normalize();
var absJoyPos = Vector2( Mathf.Abs( Joystick.position.x ), Mathf.Abs( Joystick.position.y ) );
movement *= forwardSpeed * ( ( absJoyPos.x > absJoyPos.y ) ? absJoyPos.x : absJoyPos.y );
movement += velocity;
movement += Physics.gravity;
movement *= Time.deltaTime;
character.Move( movement );
MoveAnim();
}
function MoveAnim(){
if (character.velocity.z > 0) {
animation["Walk"].speed = walkSpeed;
animation.CrossFade("Walk");
walking = true;
rollOK = true;
}
else if (character.velocity.z < 0) {
animation["Walk"].speed = walkSpeed * -1;
animation.CrossFade("Walk");
walking = true;
rollOK = false;
}
else if(character.velocity.x > 0){
animation.CrossFade("SSL");
walking = true;
rollOK = false;
}
else if(character.velocity.x < 0){
animation.CrossFade("SSR");
walking = true;
rollOK = false;
}
else{
animation["IdleLow"].speed = idleSpeed;
animation.CrossFade("IdleLow");
walking = false;
rollOK = false;
}
}
Ok I got it all fixed by taking and modifying the penelope animation controller, heres the final movement animations script, the only thing to add is to have the animations stop when you stop moving.
function MoveAnim(){
var characterVelocity = character.velocity;
var horizontalVelocity : Vector3 = characterVelocity;
horizontalVelocity.y = 0;
var forwardMotion = Vector3.Dot(transform.forward, horizontalVelocity);
var sidewaysMotion = Vector3.Dot(transform.right, horizontalVelocity);
if (Mathf.Abs(forwardMotion) > Mathf.Abs(sidewaysMotion)){
if (forwardMotion > 0){
animation["Walk"].speed = forwardSpeed;
animation.CrossFade("Walk");
}
else{
animation["Walk"].speed = forwardSpeed * -1;
animation.CrossFade("Walk");
}
}
else if (Mathf.Abs(forwardMotion) < Mathf.Abs(sidewaysMotion)){
if (sidewaysMotion > 0){
animation["SSR"].speed = forwardSpeed;
animation.CrossFade("SSR");
}
else{
animation["SSL"].speed = forwardSpeed;
animation.CrossFade("SSL");
}
}
}
Good find! I was about to tell you that you had to look into dot products, I just figured this out myself last night. Wish I found that penelope script, would have saved me some time! haha.
Mine is a little more complex though because I have diagonal motion animations to account for as well.
Nevermind figured it out.