Character movement relative to both camera and transform

Hello everyone, I’d need some help concerning character movement.

Actually, I have a transform (Super Character Controller) that aligns to the normal of the ground it’s standing on. This works pretty well, except I don’t know how to implement movement that takes orientation as a factor.

Basically, I would like the player to move relatively to the camera and its own orientation. Let me explain you with these pictures :

alt text

This is the main view. As you can see, the left and right vectors are based on the camera’s orientation. It should NOT be the transform’s left and right vectors.

Now, the same scene, with a slightly different angle (keep in mind the camera is “supposedly” at the same place) :

alt text

It now makes sense the the forward vector is based on both the player and the camera’s orientations. The forward vector should be based on the camera’s forward and be perpendicular to the ground the player is standing on (= simply its orientation).

A last side view to understand it better :

alt text

The forward vector can NOT be the player’s, as it has to move forward relatively to the camera even if he’s not oriented properly (e.g. looking on the left).

I’d also like to handle the case in which the player is walking on a ceiling. In such a case, the movement forward vector should be the backwards vector and vice-versa, as he would be walking towards the camera.

I definitely need some advice concerning this, I’ve been scratching my head for days yet I still can’t manage to find a proper solution.

Thank you in advance for your help.

What’s good with Unity Answers, at least, is that it makes you look for an answer yourself.

So I finally found an answer :

`
float btwAngle = Vector3.Angle (Vector3.up, groundNormal);

Quaternion tempAngle = Quaternion.AngleAxis (-btwAngle, cameraRight);

move = tempAngle * move;
`

I can think of two things that might work.
In your movement script use
Camera.main.transform
Example :

void Update() {
        if (Input.GetKey("w"))
          rigidbody.addforce(Camera.main.transform.forward * 20);
        
    }
 if (Input.GetKey("s"))
          rigidbody.addforce(-Camera.main.transform.forward * 20);
        
    }

}

Or get the direction from the player by using

    Vector3 Direction = player.transform.position - Camera.main.transform.position;
 if (Input.GetKey("w")){
              rigidbody.addforce(Direction * 20);
            
        }