I’m using the standard asset mobile joystick.js script for my controls, but I’m having trouble with my character movement. I’m just trying to move, left, right, up and down using AddRelativeTorque and it works to an extent but not quite the way I want.
I have a ball in the scene and rolls around ok at first, but it seems as though the rotation is throwing the controls off as when the ball is stationary it works fine but when I start moving it, the controls don’t behave accordingly.
I’m probably doing this wrong but can anyone help me out here, I would just like to be able to roll my ball in the x and z axis. Here is my code:
#pragma strict
// This script must be attached to a GameObject that has a CharacterController
@script RequireComponent( CharacterController )
var moveJoystick : joystick;
var speed : float = 10; // Ground speed
private var thisTransform : Transform;
private var character : CharacterController;
function Start()
{
// Cache component lookup at startup instead of doing this every frame
thisTransform = GetComponent( Transform );
character = GetComponent( CharacterController );
}
function Update()
{
//transform.eulerAngles.y = 0;
if(moveJoystick.position.x >= 0.5)
{
rigidbody.AddTorque(Vector3.up * 10);
}
else if(moveJoystick.position.x <= -0.5)
{
rigidbody.AddTorque(Vector3.down * 10);
}
else if(moveJoystick.position.y >= 0.5)
{
rigidbody.AddTorque(Vector3.left * 10);
}
else if(moveJoystick.position.y <= -0.5)
{
rigidbody.AddTorque(Vector3.right * 10);
}
}