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);
}
}