- Home /
This question was
closed Mar 21, 2016 at 02:15 PM by
mnmwert for the following reason:
I figured out the problem
How do I make my player stop moving when I release the button that I am pressing?
I'm writing a script in which my player moves from left to right using the right/left arrow keys. My problem is that my character will not stop moving once I let go of the arrow. I am new to coding so Can you please explain simply. Here is my script (C#): using UnityEngine; using System.Collections; using System;
public class mover : MonoBehaviour { public float moveSpeed; private float position = 0;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.RightArrow))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(+moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, GetComponent<Rigidbody2D>().velocity.y);
}
}
}
Comment
I've figured out how make my player stop moving but it will only go left now. Here is the script I added:
public float noSpeed;
//the next piece of code is at the end
else { GetComponent().velocity = new Vector2(noSpeed, GetComponent().velocity.y); }