im trying to create a mobile game where you can control the player usnig 2 joysticks, one for movement, and one for looking(i might combine later)
how do i make the character rotate towards the direction the joystick is facing?
heres my current code, the rotate part doesnt work
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Rigidbody2D rb;
public Joystick moveJoystick, shootJoystick;
public Camera cam;
public float moveSpeed;
Vector3 moveDir;
Vector2 mousePos;
Quaternion targetRotation;
public float rotateSpeed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
var input = new Vector2(shootJoystick.Horizontal, shootJoystick.Vertical);
if (input != Vector2.zero)
{
targetRotation = Quaternion.LookRotation(input);
}
// Rotate towards targetRotation here
transform.rotation = Quaternion.Euler(0f, 0f, targetRotation.z);
if (moveJoystick.Horizontal >= .2f || moveJoystick.Horizontal <= -.2f)
moveDir.x = moveJoystick.Horizontal;
else
{
moveDir.x = 0f;
}
if (moveJoystick.Vertical >= .2f || moveJoystick.Vertical <= -.2f)
moveDir.y = moveJoystick.Vertical;
else
{
moveDir.y = 0f;
}
}
private void FixedUpdate()
{
rb.MovePosition(transform.position + (moveDir * moveSpeed * Time.fixedDeltaTime));
}
}