2D Character Rigidbody not moving with arrow keys.
I am creating a movement script which includes jumping and running but while my character animates it will not move despite speed being above 0. Here's the code.
using UnityEngine; using System.Collections;
// TODO - Add in Jumping and animation handling, probably sounds too
public class PlayerMovement : MonoBehaviour { public float speed = 100; // Seems good - subject to change private float initialSpeed = 100; //copies the speed from the player movement speed "5" public float jumpHeight; bool canJump = true;
//public int playerScore = 0; //public int, this means other scripts can access this
//public GUIText scoreOutput; // display text in game - assigned with scoretext game object
Rigidbody2D RigidBodyHandling;
Vector2 StopMove;
Vector3 scale;
Animator AnimatorHandling;
// Use this for initialization
void Start()
{
RigidBodyHandling = gameObject.GetComponent<Rigidbody2D>();
AnimatorHandling = gameObject.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetAxis("Horizontal") > 0) // moving right
{
scale = new Vector3(1.851962f, 1.643932f, 1);
transform.localScale = scale; // Sets player sprite to face right
}
else if (Input.GetAxis("Horizontal") < 0) // moving left
{
scale = new Vector3(-1.851962f, 1.643932f, 1);
transform.localScale = scale; // Sets player sprite to face left
}
if (Input.GetKey(KeyCode.LeftShift) && Input.GetAxis("Horizontal") != 0)
{ //if player holds down shift, and is pressing a movement direction set the max speed & anim to 1.5
speed = initialSpeed * 1.5f; // multiplying the current speed by 1.5
AnimatorHandling.SetFloat("Walk", 1.5f); //speeds up the animation to match
}
else { //If shift is not held down, start walking by with default speed
speed = initialSpeed; //defaults back to original speed "5"
AnimatorHandling.SetFloat("Walk", Mathf.Abs(Input.GetAxis("Horizontal"))); //sets the animation back to "5" too
}
StopMove = new Vector2(0, RigidBodyHandling.velocity.y);
if (Input.GetAxis("Horizontal") != 0 && (RigidBodyHandling.velocity.x < speed && RigidBodyHandling.velocity.x > -speed)) //If player is moving in X or -X direction
{
RigidBodyHandling.AddForce(Vector2.right * Input.GetAxis("Horizontal") * 10); //Moves the player
}
else if (Input.GetAxis("Horizontal") == 0) // If <-/-> isn't pressed
{
RigidBodyHandling.velocity = StopMove; // Stop the player
}
if (Input.GetButtonDown("Jump")) //if the player presses jump
{
if (canJump == true) //if the player can currently jump
{
AnimatorHandling.SetBool("Jump", true); //play the jump animation
RigidBodyHandling.AddForce(transform.up * jumpHeight); //give the rigid body upward force
canJump = false; //stops the player jumping while already in the air
}
}
////take the score output variable and convert it to a string
//scoreOutput.text = ("Score: " + playerScore.ToString()); // linking back to text - types out score + " playerscore" string - the number
}
void OnCollisionEnter2D(Collision2D coll) //check if the player collides with the ground again.
{
if (coll.gameObject.tag == "Ground")
{
AnimatorHandling.SetBool("Jump", false); //stops the jump animation/setting the char back to idle.
AnimatorHandling.SetBool("Land", true); //starts the landing animation.
StartCoroutine(LandingAnimationWait());
}
}
IEnumerator LandingAnimationWait() //A delay while the landing animation plays.
{
float WaitTimer = 0.5f; //sets the wait time.
yield return new WaitForSeconds(WaitTimer);
canJump = true; //once the landing animation is complete allows player to jump again.
AnimatorHandling.SetBool("Land", false); //ends landing animation
}
}
I'm really confused here. Any help?
Your answer
Follow this Question
Related Questions
Cant get dash attack working 0 Answers
Determine the direction an object is actually moving? 0 Answers
Problem with rigidbody 2 Answers
Rigidbody going through Colliders help! 1 Answer
Help converting a character controller script to rigidbody 1 Answer