Character Movement jerking
Got a problem with my movement script. Please don't send me a movement code that works as i'm trying to learn how to code not just chuck together a game of presets and other people's work!
I'm also using Itween for the movement which is a unity add-on for smooth movement but for some reason my player is really jerky when he moves. It's nothing to do with frame rate as it runs at anything from 30 to 150 and the problem remains. Here's a old video of the which shows the player moving around with the same jerky movement: https://www.youtube.com/watch?v=xdWGNoUeVTw
Code:
using UnityEngine; using System.Collections;
public class PlayerMovement : MonoBehaviour
{
public static int playerAnimation;
public int moveSpeed = 3;
public static Vector3 playerPosition;
public static int playerRotation; //manually set the player rotation
Transform myTransform;
public bool running = false;
void Start()
{
myTransform = transform;
playerAnimation = 0;
}
void FixedUpdate(){
//ANIMATION:
if(playerAnimation == 0)
{
GetComponent<Animation>().CrossFade ("Idle", 0.1f);
}
if(playerAnimation == 1)
{
GetComponent<Animation>().CrossFade ("Walk", 0.1f);
}
if(playerAnimation == 2)
{
GetComponent<Animation>().CrossFade ("Attack1");
}
if(playerAnimation == 3)
{
GetComponent<Animation>().CrossFade ("Run");
}
if(playerAnimation == 4)
{
GetComponent<Animation>().CrossFade ("Death");
}
//myTransform.Translate(Vector3.forward * moveSpeed * Input.GetAxis("Vertical") * Time.deltaTime);
//myTransform.Translate(Vector3.right * moveSpeed * Input.GetAxis("Horizontal") * Time.deltaTime);
if(running == false) {
if(Input.GetKey("s"))
{
playerAnimation = 1;
myTransform.eulerAngles = new Vector3(0,180,0);
iTween.MoveBy(gameObject, iTween.Hash("z", 0.25, "speed", 15));
// playerPosition = Mathf.Lerp(playerPosition.y, playerPosition.y + 2f,Time.deltaTime);
} else if(Input.GetKeyUp("s"))
{
playerAnimation = 0;
}
}
if(Input.GetKey("s") && Input.GetKey(KeyCode.LeftShift))
{
running = true;
playerAnimation = 3;
myTransform.eulerAngles = new Vector3(0,180,0);
iTween.MoveBy(gameObject, iTween.Hash("z", 0.5, "speed", 15));
} else if(Input.GetKeyUp(KeyCode.LeftShift))
{
running = false;
}
if(running == false){
if(Input.GetKey("w"))
{
playerAnimation = 1;
myTransform.eulerAngles = new Vector3(0,0,0);
iTween.MoveBy(gameObject, iTween.Hash("z", 0.25 , "speed", 15));
} else if(Input.GetKeyUp("w"))
{
playerAnimation = 0;
}
}
if(Input.GetKey("w") && Input.GetKey(KeyCode.LeftShift))
{
running = true;
playerAnimation = 3;
myTransform.eulerAngles = new Vector3(0,0,0);
iTween.MoveBy(gameObject, iTween.Hash("z", 0.5, "speed", 15));
} else if(Input.GetKeyUp(KeyCode.LeftShift))
{
running = false;
}
if(running == false){
if(Input.GetKey("a"))
{
playerAnimation = 1;
myTransform.eulerAngles = new Vector3(0,-90,0);
iTween.MoveBy(gameObject, iTween.Hash("z", 0.25, "speed", 15));
}else if(Input.GetKeyUp("a"))
{
playerAnimation = 0;
}
}
if(Input.GetKey("a") && Input.GetKey(KeyCode.LeftShift))
{
running = true;
playerAnimation = 3;
myTransform.eulerAngles = new Vector3(0,-90,0);
iTween.MoveBy(gameObject, iTween.Hash("z", 0.5, "speed", 15));
} else if(Input.GetKeyUp(KeyCode.LeftShift))
{
running = false;
}
if(running == false){
if(Input.GetKey("d"))
{
playerAnimation = 1;
myTransform.eulerAngles = new Vector3(0,90,0);
iTween.MoveBy(gameObject, iTween.Hash("z", 0.25, "speed", 15));
} else if(Input.GetKeyUp("d"))
{
playerAnimation = 0;
}
}
if(Input.GetKey("d") && Input.GetKey(KeyCode.LeftShift))
{
running = true;
playerAnimation = 3;
myTransform.eulerAngles = new Vector3(0,90,0);
iTween.MoveBy(gameObject, iTween.Hash("z", 0.5, "speed", 15));
} else if(Input.GetKeyUp(KeyCode.LeftShift))
{
running = false;
}
if(Input.GetKey("space"))
{
playerAnimation = 2;
} else if(Input.GetKeyUp("space"))
{
playerAnimation = 0;
}
if(playerRotation == 1)
{
myTransform.eulerAngles = new Vector3(0,90,0);
playerRotation = 0;
}
playerPosition = new Vector3(myTransform.position.x, myTransform.position.y, myTransform.position.z);
}
}
Thanks for any help in advance!
Comment