Rotate An Object Toward The Direction The Joystick Is Pressed

I’ve been working with the mobile/android Joysticks for awhile now and I’ve finally got the script to do what I wanted to do thanks to the wonderful people who helped me here in the community. My next task now is to try and make the “Player Object” face the way the joystick is held (view point is 2D Top View Shooter so you’re looking down on the player) by degrees. Like if I push the joystick up left the player faces up left and so forth. I don’t know exactly where to begin. Can anyone please lend me a hand?

//////////////////////////////////////////////////////////////
// SidescrollControl.js
//
// SidescrollControl creates a 2D control scheme where the left
// pad is used to move the character, and the right pad is used
// to make the character jump.
//////////////////////////////////////////////////////////////

#pragma strict

@script RequireComponent( CharacterController )

// This script must be attached to a GameObject that has a CharacterController
var moveTouchPad : Joystick;
var fireTouchPad : Joystick;

var forwardSpeed : float = 4;
var backwardSpeed : float = 4;
var jumpSpeed : float = 16;
var inAirMultiplier : float = 0.25;					// Limiter for ground speed while jumping

private var thisTransform : Transform;
private var character : CharacterController;
private var velocity : Vector3;						// Used for continuing momentum while in air
private var canJump = true;
var ftp: boolean = false;

function Start()
{
	// Cache component lookup at startup instead of doing this every frame		
	thisTransform = GetComponent( Transform );
	character = GetComponent( CharacterController );	

	// Move the character to the correct start position in the level, if one exists
	var spawn = GameObject.Find( "PlayerSpawn" );
	if ( spawn )
		thisTransform.position = spawn.transform.position;
}

function OnEndGame()
{
	// Disable joystick when the game ends	
	moveTouchPad.Disable();	
	fireTouchPad.Disable();	

	// Don't allow any more control changes when the game ends
	this.enabled = false;
}

function Update()
{
	var movement = Vector3.zero;

// Apply movement from move joystick
    if (moveTouchPad.position.y > 0)
    {
        movement += Vector3.up * forwardSpeed * moveTouchPad.position.y;
    }
    else
    {
        movement += Vector3.up * backwardSpeed * moveTouchPad.position.y;
    }
    if (moveTouchPad.position.x > 0)
    {
        movement += Vector3.right * forwardSpeed * moveTouchPad.position.x;     
    }
    else
    {
        movement += Vector3.right * backwardSpeed * moveTouchPad.position.x;
    }
    
    // Check for jump
	if ( ftp == true)
	{		
		var jump = false;
		
		var touchPad = fireTouchPad;
			
		if ( !touchPad.IsFingerDown() )
			canJump = true;
				print("FIRE!!!");
	 	if ( canJump && touchPad.IsFingerDown() )
	 	{
			jump = true;
			canJump = false;
	 	}	
		
		if ( jump )
		{
			// Apply the current movement to launch velocity		
			velocity = character.velocity;
			velocity.y = jumpSpeed;	
		} 
		}
		
	movement *= Time.deltaTime;
	
	// Actually move the character	
	character.Move( movement );
}

I’m note sure how Unity handles joystick input but looking at the documentation it seems like it gives you a value between -1 and 1 for each axis. You could use trigonometry to get the angle.

Look here: Unity - Manual: Input

The function you’re looking for is “Mathf.atan2”. Simply give it your X and- Y axis values and it will return the angle (in radians I believe). Then what you can do is multiply that value with Mathf.rad2deg and use it for your character’s rotation. It could look like this:

char.transform.eulerAngles = new vector3(char.transform.eulerAngles.x, Mathf.atan2(x, y) * Mathf.rad2deg, char.transform.eulerAngles.z);

Now, that’s C# but you can probably figure out how to write it in JS. If you’re not sure how to use the input class, search around (google!) and there’ll be a ton of pages helping you.

Best of luck

float xmove = CrossPlatformInputManager.GetAxis(“Horizontal”) * carSpeed;
float zmove = CrossPlatformInputManager.GetAxis(“Vertical”) * carSpeed;
rb.velocity = new Vector3(xmove, 0, zmove);
movement = new Vector3(xmove, 0.0f, zmove);
transform.LookAt(transform.position + movement);

I just want to say thank you so much u/Semicolon, I was literally stuck on this forever, your answer came in clutch man, thanks so much.