0
votes

I'm using a joystick to move around my player with a rigidbody component attached. When the player is moving I want it to rotate on the Y axis accordingly. E.g. if the player moves right, it should rotate a certain degree on the Y axis.

Please note my game is player in othorgraphic view, which is why the movement is based off the camera to move correctly.

    void FixedUpdate()
{
    Vector3 moveVector = (transform.right * joystick.Horizontal + transform.forward * joystick.Vertical).normalized;
    Vector3 relativeMovement = Camera.main.transform.TransformVector(moveVector);
    rb.AddForce(relativeMovement * speed);
    relativeMovement.y = 0;

Code for my joystick if needed.

    using UnityEngine;
    using UnityEngine.EventSystems;

     public class FloatingJoystick : Joystick
   {
       Vector2 joystickCenter = Vector2.zero;

public bool joyStickWorking = false;
public bool playerMoving = false;

void Start()
{
    background.gameObject.SetActive(false);
}

public override void OnDrag(PointerEventData eventData)
{
    Vector2 direction = eventData.position - joystickCenter;
    inputVector = (direction.magnitude > background.sizeDelta.x / 2f) ? direction.normalized : direction / (background.sizeDelta.x / 2f);
    handle.anchoredPosition = (inputVector * background.sizeDelta.x / 2f) * handleLimit;
    playerMoving = true;
}

public override void OnPointerDown(PointerEventData eventData)
{
    background.gameObject.SetActive(true);
    background.position = eventData.position;
    handle.anchoredPosition = Vector2.zero;
    joystickCenter = eventData.position;
    joyStickWorking = true;
}

public override void OnPointerUp(PointerEventData eventData)
{
    background.gameObject.SetActive(false);
    inputVector = Vector2.zero;
    joyStickWorking = false;
    playerMoving = false;
}
}
1
what is the question that u r asking? - Lincoln Cheng
When my player is moving, I want the player object to also rotate with the movement. So if a car is driving straight and turns right, the front of the car should also turn. - Jakethagun
u might need to elaborate on the question. for e.g, is there a bug with ur current code (what is not working?), or that it is working but u r asking how to implement it better, etc? - Lincoln Cheng

1 Answers