- Home /
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 :
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) :
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 :
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.
I appreciate your effort with the screenshots, but I'm still not quite understanding your problem. In all the pictures you've shown, it appears to me that the line you've marked as "forward" is indeed the player's forward - i.e. it's the direction they're looking in. How would you like it to change relative to the camera?
The player doesn't always look in the same direction as the camera : he can move freely, and that's the problem. The forward vector should NOT be the player's forward, but rather the perpendicular of the normal of the ground he's standing on. It hope it makes it clearer, thank you for your help !
In fact, the player's Y rotation doesn't matter at all, think Sonic Adventure for example.
Answer by Ulydev · Aug 16, 2015 at 10:36 PM
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;
Answer by jabez · Aug 01, 2015 at 10:47 AM
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);
}
Thank you for your interest in this problem, unfortunately that won't work. The vector is not perpendicular to the ground's normal, and is based on the camera position relative to the player... If I place the camera above the player and look at it, the character will just move towards the ground.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Character controller movement 1 Answer
Xbox Controller & input manager? 1 Answer
Unity 2017.0.3 Mouse Look Script With Clamp!! 0 Answers
Help with my Character Controller 1 Answer