Ball Moving Problems

I’m trying to make a ball rolling game, but I’ve only been successful in making the ball move left to right, but not forwards and backwards. Here’s the code I’m using:

#pragma strict

var rotationSpeed = 200;

function Update ()
{
var rotation1 : float = Input.GetAxis (“Horizontal”) * rotationSpeed;

var rotation2 : float = Input.GetAxis ("Vertical") * rotationSpeed;

rotation1 *= Time.deltaTime;

rotation2 *= Time.deltaTime; 

GetComponent.<Rigidbody>().AddRelativeTorque (Vector3.back * rotation1);

GetComponent.<Rigidbody>().AddRelativeTorque (Vector3.back*-1 * rotation2); 

}
I know that there is probably so much wrong with this, but I’m really new unity and what-not.

Hey, don’t worry. There’s nothing to be ashamed of. We’ve all been new to Unity at some point.

Now, there are a few ways that this can be handled. In this case, I’ll work with what you’re starting here.

There are a few details to keep track of with this:

First, “AddRelativeTorque()” – the torque applied is “relative” to your object. Why is this a problem for you? Well, your object is rapidly rotating. Therefore, its relative orientation is constantly changing.

Second, physics changes should be made in “FixedUpdate()” rather than in “Update()”

And third, “AddTorque()” and “AddForce()” (and their variants) already scale their application to a per-frame basis unless instructed not to.

For future reference, depending on how the game is designed, you may want to factor in camera-oriented controls, but that can be saved for another time, since you’re new to this.

Now, that said, here is a potential approach to this:

private var rb: Rigidbody; // Your Rigidbody will be held here to improve efficiency

private var inputControl: Vector2; // Replacing rotation1 and rotation2
private var reorientedControl: Vector3; // Combine input with axes to require only a single AddTorque()

var rotationSpeed: float = 20.0;
var maxRotationSpeed: float = 200.0;

function Start()
{
	rb = GetComponent(Rigidbody); // Unityscript allows various formatting on this instruction

	rb.maxAngularVelocity = maxRotationSpeed; // This defaults to 7, which is not very useful when the primary mode of movement is rolling
}

function Update()
{
	inputControl = Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
	if(inputControl.sqrMagnitude > 1)
	{
		inputControl = inputControl.normalized;
	}
}

function FixedUpdate()
{
	// Note: The following line does not accommodate camera rotation, but is the line to change in order to do so
	reorientedControl = (inputControl.x * Vector3.forward) + (inputControl.y * -Vector3.right); // Take inputs and convert them into a Vector3 immediately before use.

	rb.AddTorque(reorientedControl * rotationSpeed, ForceMode.Acceleration); // ignore the ball's mass with ForceMode change
}

There are plenty of little details mixed in, so make sure you look at it and do what you can to understand how it works (and, if anything’s wrong, speak up. This code’s also untested).

Edit: Whoops, I did forget a tiny piece.