- Home /
gameobject goes in the wrong left and right directions
I've recently created a Sphere gameobject in Unity, and placed it on top of a Cube. I tried getting the Sphere to move left and right on the cube by pressing both the left and right buttons respectively. The problem I have is when i press left, the sphere rotates right, and when i press right, it rotates left. I just want the sphere to move in the direction im holding. Any advice?
using UnityEngine;
using System.Collections;
public class SphereBehaviourScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
float rotationSpeed = 100.0f;
// Update is called once per frame
void Update () {
float rotation = Input.GetAxis ("Horizontal") * rotationSpeed;
rotation *= Time.deltaTime;
rigidbody2D.AddTorque (rotation);
}
}
I'm a beginner to Unity, hopeful to learn as i go.
Answer by Yofurioso · Jul 28, 2014 at 01:30 AM
If you only want to make the ball go in the direction you press and is going the opossite direction, simply change the line: float rotation = Input.GetAxis ("Horizontal") rotationSpeed; for float rotation = Input.GetAxis ("Horizontal") rotationSpeed * -1;
Thanks! This makes sense actually, i ended up adding a -1 to the equation on line 19:
rigidbody2D.AddTorque (-1 * rotation);
No longer having the issue. Hopefully this is an adequate solution.
Your answer
Follow this Question
Related Questions
What is wrong with my Counter Script? (C#) 1 Answer
Using Texture Assets in Code 1 Answer
Unity Sprites Overlapping, slightly see-through (2D game) 2 Answers
Unity minecart 2d idea 1 Answer
How do I use MoveTowards in 2d? 1 Answer