- Home /
Single Joystick Movement
What I am trying to do is make a GameObject move and rotate towards a target. The target is a child of the GameObject and depending what way the single joystick is pointing the target will offset from the GameObject and the GameObject will move towards that point. What is happening is the GameObject will start to do figure-8 like movements. It has a top down view so that the x direction in the joystick and in the world/GameObject are the same and the y joystick axis is the same as the z world/GameObject. This is being developed currently for the Android.
var leftJoyStick :Joystick;
var moveSpeed = 30.0;
var rotSpeed = 10.0;
var player : GameObject;
private var joystickOrgin : Vector2;
private var currentJoystickPosition : Vector2;
private var desiredVector : Vector2;
private var lookDirection : Quaternion;
var target : GameObject;
function Start()
{
// Get the joysticks orgin on start
joystickOrgin = new Vector3(leftJoyStick.position.x,leftJoyStick.position.y);
}
function Update ()
{
//As long as the joystick has been moved in x or y
if(leftJoyStick.position.y > 0.1 || leftJoyStick.position.y < -0.1 || leftJoyStick.position.x > 0.1 || leftJoyStick.position.x < -0.1)
{
//get current joystick position
currentJoystickPosition = new Vector2(leftJoyStick.position.x,leftJoyStick.position.y);
//calculate the vector
desiredVector = (currentJoystickPosition - joystickOrgin);
//move the target position from the players position + desired vector (ignoring y since we do not want to move in that axis)
target.transform.position = new Vector3(player.transform.position.x + (desiredVector.x * 500),0,player.transform.position.z + (desiredVector.y * 500));
//gets the Quaternion of the target position
lookDirection = Quaternion.LookRotation(target.transform.position);
//Rotates the player from current its rotation to the look direction by the rotSpeed*the change in time
player.transform.rotation = Quaternion.Lerp(player.transform.rotation, lookDirection, rotSpeed*Time.deltaTime);
//Moves the player forward
player.transform.Translate(player.transform.forward *moveSpeed);
}
}
I have tried many different ways of attempting this and looked through various sources on how to do this. It seems like I can either just get the movement or rotation working but not both at the same time.