- Home /
Stop Object From Moving After Key Release
I am using the following code to make an object move left/right. The object has a circle Collider 2D
and a Rigidbody 2D
attached to it. The Collider 2D
has a Physics2D Material
with Bounce = 0
and Friction = 0
.
When use the arrow keys to move left/right and then let go of the keys, the object wants to keep moving left/right. I want it to stop as soon as I let go of the keys.
Sometimes when I move in another direction as soon as I let go of the keys it starts moving the other way.
What is causing this and how can I get it to stop when I let go of the directional keys?
using UnityEngine;
using System.Collections;
public class Move : MonoBehaviour {
// Update is called once per frame
void Update () {
if(!renderer.isVisible){
Debug.Log("Can't see me!");
}
this.transform.Translate(Input.GetAxis("Horizontal") * Time.deltaTime * 15,0,0);
}
}
Answer by robertbu · Jun 06, 2014 at 09:42 AM
Input.GetAxis() does some averaging. You can use Input.GetAxisRaw() instead:
http://docs.unity3d.com/ScriptReference/Input.GetAxisRaw.html
Answer by pacific00 · Jun 06, 2014 at 09:58 AM
void Update () { if(!renderer.isVisible){ Debug.Log("Can't see me!"); } // change ..!!!! if(input.getKeyDown .. or any one of such funcs ) this.transform.Translate(Input.GetAxis("Horizontal") Time.deltaTime 15,0,0); }
Your answer
Follow this Question
Related Questions
Wait a fraction of a second within a Function Update() 1 Answer
How to have a GameObject follow a path with c#? 3 Answers
Best Way to make a character move 1 Answer
Friction 0 material not working with Transform.Translate 0 Answers
Best way to move a Rigidbody2D from point A to point B (Mouse Position) 3 Answers