Running script isn't working correctly
Newbie here, I'm working on a running script for a 2D platformer but I can't get it to work. Here's what I've got so far:
using UnityEngine;
using System.Collections;
public class nuntiControllerScript : MonoBehaviour {
public float maxSpeed = 10f;
bool facingRight = true;
public float move;
Animator anim;
// Use this for initialization
void Start()
{
//The animator is called in order to play a running animation.
anim = GetComponent<Animator>();
}
// Update is called once per frame
void FixedUpdate()
{
//While the D key is held down, move is 1
while (Input.GetKey(KeyCode.D))
{
move = 1;
};
//while the A key is held down, move is -1
while (Input.GetKey(KeyCode.A))
{
move = -1;
};
//The value of the speed parameter is altered to be equal to the value of move. This causes the idle animation to transition into running.
anim.SetFloat("Speed", Mathf.Abs(move));
//The velocity of the character is 10 x move, which is either 1, so the character moves to the right, or -1 so he moves to the left
GetComponent<Rigidbody2D>().velocity = new Vector2(move * maxSpeed, GetComponent<Rigidbody2D>().velocity.y);
if (move > 0 && !facingRight) //If the move is greater than 0 and you aren't facing right,
Flip(); //turn around so you are.
else if (move < 0 && facingRight) //If the move is less than 0 and you are facing right,
{ //turn around to face left and set facingRight to false.
Flip();
facingRight = false;
}
}
void Flip()
{
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Whenever I try and run the code as it is now, the program crashes. I've also had issues where:
the sprite would move, albeit only to the left, but there'd be huge lag and the sprite wouldn't flip
the sprite would just rotate slightly to the right when the right arrrow key was pressed
the sprite just wouldn't move (This was the most common problem)
I've been trying to make this work for a couple of days now. Anyone got any advice?
Answer by tanoshimi · Jun 28, 2016 at 11:31 AM
Don't ever use while() in Update() - you'll hold up your entire execution thread by going round and round in that loop until it's no longer true (in your case, for as long as you're holding down the A or D keys).
I suspect you meant:
if(Input.GetKey(KeyCode.D))
{
move = 1;
};
//while the A key is held down, move is -1
if(Input.GetKey(KeyCode.A))
{
move = -1;
};
However, note that even then you shouldn't really check for input inside FixedUpdate - fixed update is intended for physics updates or other events that need to happen on a regular, fixed timestep - listening for input etc. that needs to be done in every frame should be in Update(). You might want to look through some of the tutorials at https://unity3d.com/learn/tutorials/topics/2d-game-creation
Your answer
Follow this Question
Related Questions
Unity2d - Player Movement. Character "jumps" when transition from idle > run 0 Answers
Please help with infinite gravity swapping problem 0 Answers
2D Movement System (Stop Movement on Collision) 1 Answer
Kinematic 2D Rigidbody movement: Rigidbody2D.MovePosition vs Rigidbody2D.position problem 1 Answer
Best way to move back and forth between horizontal planes using doors? 1 Answer