Question by
miteshchakma · Dec 15, 2015 at 09:26 PM ·
getaxis
Help needed in moving object
void Update () {
float force = 0.0f;
float velocity = Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x) ;
Debug.Log ("velovity " + velocity);
float h = Input.GetAxis ("Horizontal"); //getting input along x-axis only
if (h > 0) {
if (velocity < maxVelocity) {
force = speed;
}
Vector3 scale = transform.localScale;
scale.x = 1;
transform.localScale = scale;
} else if (h < 0) {
if (velocity < maxVelocity) {
force = -speed;
}
Vector3 scale= transform.localScale;
scale.x = -1;
transform.localScale = scale;
}
GetComponent<Rigidbody2D>().AddForce (new Vector2 (force, 0));
} }
This is my code for moving my spite. It is supposed to move my object in horizontal direction when i press right/left arrow key and a/d viceversa. But when i try to run my game my object does not response the way it should be infect i do not get any value at all . note i tried Debug.log to find the value and i suppose update method does not work here
Comment