Transform GameObject According to Position of Camera

Hi All,

I have recently discovered in issue with my third person controller. You see in most games, if you move forward and then turn your camera, the movemente follows the direction you are looking.

However, the movement of my player GameObject simply moves according to the world space. I really need a solution.

Also on top of this, I need a way to be able to turn the GameObject to the left or right of where the camera is facing and then back to the front when I press the different keys for each of these actions.

Here is the code for my PlayerController:

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

public class PlayerController : MonoBehaviour
{
    private Rigidbody rb; 
   
    private float movementX; 
    private float movementY; 
    public float speed = 1; 

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();

    }

    void OnMove(InputValue movementValue)
    {
        Vector2 movementVector = movementValue.Get<Vector2>();

        movementX = movementVector.x; 
        movementY = movementVector.y; 
    }

    void FixedUpdate() //Runs once per fixed interval rather than once per frame
    {
        Vector3 movement = new Vector3(movementX, 0.0f, movementY); 

        rb.AddForce(movement * speed); 
    }
}

I used the Cinemachine package for the camera.

Thanks so much for helping,

Ben

Hello.

First, if want a good answer, we need to see you code…

You need to learn the difference between local space and wolrd space. (Uderstand space as coord references)

For transform, functions that derives from transform directly (like transform.translate or transform.rotate) are normlaly in local space.

You say “movement of my player GameObject simply moves according to the world space” But there are so many ways to move a gameobject…


Things to explore:

-transform.Position vs transform.LocalPosition

-Transform.InverseTransformPoint

-Transform.Translate

-Vector3.MoveTowards


Good Luck.