I am making a 2d isometric game. I created a basic player movement script, and everything worked, but as I kept working on the file, the player stopped moving. The animation still works, the player just won’t move. I have been frustrated with this for a few days, so any help is appreciated.
Older working script:
using UnityEngine;
using System.Collections;
public class MovePlayer : MonoBehaviour {
private Rigidbody2D rb;
public int speed;
private Animator anim;
//direction of player. 0,1,2,3 clockwise from top.
private int direction;
private int newDirection = 2;
// Use this for initialization
void Start ()
{
anim = GetComponent<Animator> ();
spriteRenderer = GetComponent<SpriteRenderer> ();
rb = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update ()
{
}
void FixedUpdate ()
{
float moveY=Input.GetAxis("Vertical");
float moveX=Input.GetAxis("Horizontal");
Vector3 move = new Vector3(moveX,moveY,0);
//Debug.Log ("Found Axies");
anim.SetFloat ("MoveX", moveX);
anim.SetFloat ("MoveY", moveY);
if (moveX > 0) {
direction = newDirection;
newDirection = 3;
}
else if(moveX<0){
direction = newDirection;
newDirection = 1;
}
else if(moveY>0){
direction = newDirection;
newDirection = 2;
}
else if(moveY<0) {
direction = newDirection;
newDirection = 0;
}
Debug.Log(direction);
rb.transform.position += move.normalized *speed / 2;
}
}
Code that doesn’t work:
using UnityEngine;
using System.Collections;
public class MovePlayer : MonoBehaviour {
private Rigidbody2D rigidBody2D;
private float speed;
public float runSpeed;
public float walkSpeed;
private Animator anim;
public int divisor;
//direction of player. 1,2,3,4 clockwise from top.
//private int direction;
private int newDirection = 2;
public bool sprinting { get; set;}
private Vector3 input;
// Use this for initialization
void Start ()
{
sprinting = false;
anim = GetComponent<Animator> ();
rigidBody2D = GetComponent<Rigidbody2D> ();
anim.SetInteger ("Direction", 2);
anim.SetInteger ("Speed", 0);
}
//Update is called once per frame
void Update ()
{
if (Input.GetKey (KeyCode.LeftShift)) {
sprinting = true;
Debug.Log ("Sprinting");
} else{
sprinting = false;
Debug.Log ("Walking");
}
}
void FixedUpdate ()
{
float moveY=Input.GetAxis("Vertical");
float moveX=Input.GetAxis("Horizontal");
if (moveX > 0) {
//direction = newDirection;
newDirection = 1;
anim.SetInteger ("Direction", newDirection);
}
else if(moveX<0){
//direction = newDirection;
newDirection = 3;
anim.SetInteger ("Direction", newDirection);
}
else if(moveY>0){
//direction = newDirection;
newDirection = 4;
anim.SetInteger ("Direction", newDirection);
}
else if(moveY<0) {
//direction = newDirection;
newDirection = 2;
anim.SetInteger ("Direction", newDirection);
}
anim.SetFloat ("MoveX", moveX);
anim.SetFloat ("MoveY", moveY);
if (moveX != 0 || moveY != 0) {
anim.SetInteger ("Speed", 1);
} else {
anim.SetInteger ("Speed", 0);
}
if (sprinting)
speed = runSpeed;
else
speed = walkSpeed;
input = new Vector3 (moveX, moveY, 0);
//Debug.Log ("Found Axies");
input = new Vector3(moveX*speed,moveY*speed,0);
Debug.Log ("Speed x: " + (moveX*speed).ToString() + " Speed y: " + (moveY*speed).ToString());
//Debug.Log ("Normalized Movement: " + move.normalized);
//Debug.Log (movement);
rigidBody2D.transform.position += input/divisor;
Debug.Log (rigidBody2D.transform.position);
}
}
I set it in the editor.
– LstCenturian