how to rotate GameObject towards joystick

So I’m creating a top-down shooter and trying to make the player face the direction of the joystick. You walk with the left stick and aim with the right stick. My goal is to have it so that if you, let’s say, turn the stick to the right your character would look right. This is my current code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(PlayerMotor))]
public class PlayerController : MonoBehaviour {

	Camera cam;
	PlayerMotor motor;

	void Start () {
		cam = Camera.main;
		motor = GetComponent<PlayerMotor>();
	}

	void Update () {
		//movement
		motor.MoveToPoint(new Vector3(transform.position.x + Input.GetAxis("Horizontal"), transform.position.y, transform.position.z + Input.GetAxis("Vertical")));

		//cam control
		cam.transform.position = new Vector3(transform.position.x,
		transform.position.y + 9.0f,
		transform.position.z);

        //this is the problem
		transform.Rotate(new Vector3(transform.position.x + Input.GetAxis("rightStickHorizontal"), transform.position.y, transform.position.z + Input.GetAxis("rightStickVertical")));
	}
}

for some reason when I do this it just turns ever so slowly in the direction of the joystick (appears to be 1 degree per frame).
,So I’m creating a top-down shooter and trying to make the player face the direction of the joystick. You walk with the left stick and rotate with the right stick. My goal is to have it be so that if you point the right stick towards the, let’s say right your character would look right. this is my current code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(PlayerMotor))]
public class PlayerController : MonoBehaviour {

	Camera cam;
	PlayerMotor motor;

	void Start () {
		cam = Camera.main;
		motor = GetComponent<PlayerMotor>();
	}

	void Update () {
		//movement
		motor.MoveToPoint(new Vector3(transform.position.x + Input.GetAxis("Horizontal"), transform.position.y, transform.position.z + Input.GetAxis("Vertical")));

		//cam control
		cam.transform.position = new Vector3(transform.position.x,
		transform.position.y + 9.0f,
		transform.position.z);

        //this is the problem
		transform.Rotate(new Vector3(transform.position.x + Input.GetAxis("rightStickHorizontal"), transform.position.y, transform.position.z + Input.GetAxis("rightStickVertical")));
	}
}

for some reason when I do this it just turns ever so slowly in the direction of the joystick (appears to be 1 degree per frame).

Hello there,

From your description, it seems this is just a speed issue. The behaviour is correct, but it rotates too slowly.

Input.GetAxis() can only go from -1 to 1, and Transform.Rotate() applies that rotation every time it is called. This means that your current code would indeed apply between -1 and 1 degree rotation every frame.

To fix that, I’d recommend implementing a rotation speed variable, and multiply it by the current axis value to get the rotation to apply.

[SerializeField] private float rotationSpeed = 1.0f //You can tweak this in the inspector

transform.Rotate(myAxisValue * rotationSpeed, 0.0f, 0.0f);

Note that if you are building a twin-stick shooter, you might only want to rotate on one axis, not all three.

Now that you have this multiplier, you can do lots of things with it: if you have different character classes, you can tweak that variable to make them feel quicker or slower, you can have speed buffs or debuffs, etc…


I hope that helps!

Cheers,

~LegendBacon