#transformdirection with camera

1 messages · Page 1 of 1 (latest)

sick goblet
#

Hi, I'm having issuse with the code. the movement is effected by the camera rotation, how can I fix that ?
thanks
here are the code and the video

    public Transform PlayerHolder;
    public Transform CamHolder;
    public Rigidbody RGB;
    [Header("Movement")] public float BaseSpeed = 4f;
    [HideInInspector] public Vector3 MoveInput, CamInput, MoveUse, CamInputUse, CamPosition, RefCamPos;
    void LateUpdate(){
        CamFunction();
        CamHolder.position = PlayerHolder.position;
    }
    void FixedUpdate(){
        MoveFunction();
    }
    void MoveFunction(){
        MoveInput = new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical"));
        MoveUse = CamHolder.TransformDirection(MoveInput.normalized * BaseSpeed * Time.fixedDeltaTime);
        MoveUse.y = 0f;
        RGB.MovePosition(PlayerHolder.position + MoveUse);
    }
    [Header("Camera")] public float CamSpeed = 2.5f;
    [Range(0, 80)] public int TopViewLimit = 80;
    [Range(0, 40)] public int BotViewLimit = 30;
    void CamFunction(){
        CamInput = new Vector3(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"), 0f);
        CamInputUse += new Vector3(- CamInput.y, CamInput.x) * CamSpeed;
        CamInputUse.x = Mathf.Clamp(CamInputUse.x, -BotViewLimit, TopViewLimit);
        CamHolder.rotation = Quaternion.Euler(CamInputUse);
    }
pine pecan
#

It's very hard to see the problem here, because I don't know how you set your hierarchy up. Do you want the player to move in the direction the camera is facing when pressing forward or do you want the player to move independently?

pine pecan
#

I tried recreating your problem with a friend. Here's what we came up with, maybe it helps. We put An empty cameraPivot Object under the player and then made the main camera a child of it and positioned it so it looks good.

#

public class PlayerMovement : MonoBehaviour
{
    [SerializeField] float _speed = 4f;
    [SerializeField] float _camSpeed = 2.5f;
    [SerializeField] int _topViewLimit = 40;
    [SerializeField] int _botViewLimit = -40;
    [SerializeField] Transform _cameraPivot;

    Rigidbody _rb;
    Vector2 _currEulerAngles;

    void Awake()
    {
        _rb = GetComponent<Rigidbody>();
    }

    void LateUpdate()
    {
        HandleCameraRotation();
    }

    void FixedUpdate()
    {
        HandleMovement();
    }

    void HandleMovement()
    {
        Vector2 moveInput = new(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));

        if (moveInput == Vector2.zero) return;

        Vector3 dirVertical = transform.position - Camera.main.transform.position;
        dirVertical.y = 0;

        Vector3 dirHorizontal = dirVertical;

        dirVertical = moveInput.y * dirVertical;

        dirHorizontal = moveInput.x * dirHorizontal;

        dirHorizontal = Quaternion.Euler(90, 0, -90) * new Vector2(dirHorizontal.x, dirHorizontal.z);

        _rb.MovePosition(transform.position + _speed * Time.deltaTime * (dirHorizontal + dirVertical).normalized);
        Debug.Log(_rb.velocity);
    }
    void HandleCameraRotation()
    {
        Vector2 mouseMovement = new(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
        Vector2 eulerAngles = new(-mouseMovement.y, mouseMovement.x);

        _currEulerAngles += eulerAngles * _camSpeed;

        _currEulerAngles.x = Mathf.Clamp(_currEulerAngles.x, _botViewLimit, _topViewLimit);

        _cameraPivot.localRotation = Quaternion.Euler(_currEulerAngles);
    }
}```
sick goblet
sick goblet