Camera-Relative Movement

Hello,
I have a project in which I have an orbit style camera.
I was wondering how I can make this relative to my player movement.
Ideally this would be done by copying the camera’s y rotation and applying it to the moving object too.
How can I do this? (if it’s possible).
Here is my code:

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

public class RigidMovement : MonoBehaviour
{
    public float speed;
    private Rigidbody rb;
    public Transform relativeTransform;
    public Vector3 camDir;

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

    // Update is called once per frame
    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal");
        float v = Input.GetAxisRaw("Vertical");

        Vector3 tempVect = new Vector3(h, 0, v);
        tempVect = tempVect.normalized * speed * Time.deltaTime;
        rb.MovePosition(transform.position + tempVect);
    }
}

Thanks in advance,
dusty

Try something like

rotatedMovementVector =
    Quaternion.AngleAxis(camera.eulerAngles.y, Vector3.up)
    * originalMovementVector;
1 Like

Thank you very much! It worked. Do you have any method to rotate it as it moves though?
I have tried using transform.rotate with the rotatedMovementVector but it does not work.

I don’t understand your question.

You’re welcome to look at how I did it in my Proximity Buttons package.

See the DemoCameraRotatedControls scene and the RotatedControlsPlayerController script.

proximity_buttons is presently hosted at these locations:

https://bitbucket.org/kurtdekker/proximity_buttons

I mean Have the camera always facing the back of my player. like the cube’s normal and the camera’s normal facing the same way.

Camera.transform.forward = player.transform.forward