- Home /
Adapt Orientation of player controls to camera?
Hi,
For my project, it involves my player character going through a series of rooms, sprawling in different directions. My problem is that as the player goes into room by room, the orientation of the player character stays the same, meaning that when he quickly gets to a room that not on the default angle, his forward could become a diagonal, in relation to the room, even through he is technically still going forward.
I have a camera that always adjusts itself so that the next room appears in the centre when a new one instantiates. I was wondering if it's possible to change the orientation of my player character to correspond with the orientation of my camera object or if there is some other way to fix my controls. Any ideas?
I can't really visualize your problem, can you provide screenshots of the game view or explain it in more detail?
Sorry, it was late at night when I wrote that so I probably could've explained this better. Anyway, here's some images.
This is what it looks like regularly on the when I start the scene. You can go through one of 3 doors. Ignore the text for now, I plan to replace it later. From here I went through the left door.
A room has instantiated on the other side of that door for me to go through and my camera has jumped accordingly to the new room. I've walked through it a bit, however, if you can note where my character is right now that is the result of going in the forward direction which, in relation to this new room is now a diagonal up-right direction. The player orientation for the controls does not change according to the room you're currently in or where the camera is.
Does this help make sense of what I'm asking about?
That does make sense. Are you not rotating the player as he walks around or enters a room? If you want him to point at the middle door in a new room you could try doing something like transform.LookAt(middleDoor.transform.position);
.
Answer by EvilWarren · Oct 24, 2013 at 10:08 AM
If the door your character passes through is facing the right direction you can simply do
character.transform.forward = door.transform.forward
Failing that, you can find a vector from the door you go through to the middle door and set that as the forward vector for your character
character.transform.forward = Vector3.Normalize(middleDoor.transform.position - previousDoor.transform.position);
I'm assuming your floor layout is flat. Without knowing the code for controlling your character, its difficult to discern what would work for sure.
Well currently my code for how he's controlled is this
void OnAnimator$$anonymous$$ove() //Tells Unity that root motion is handled by the script
{
if(anim)
{
Vector3 newPosition = transform.position;
newPosition.z += anim.GetFloat("speed")* mesh$$anonymous$$oveSpeed * Time.deltaTime;
newPosition.x += anim.GetFloat("direction") * mesh$$anonymous$$oveSpeed * Time.deltaTime;
//.y += anim.GetFloat("direction") * mesh$$anonymous$$oveSpeed * Time.deltaTime; //The unexpected glitch in this line amuses me. Credit: It was Beverly's idea
if ($$anonymous$$athf.Abs(Input.GetAxis("Horizontal")) < 0.0001 && $$anonymous$$athf.Abs(Input.GetAxis("Vertical")) < 0.0001) {
transform.forward = lastdirection;
}
else {
transform.forward = Vector3.Normalize(new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"))); // Normalizes the rotation of Z local while moving
lastdirection = Vector3.Normalize(new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical")));
}
transform.position = newPosition;
}
}
I'm already using a Normalize function, however I'm not exactly sure how to re-orient it only when they open a door though.
Okay that makes things clearer. You still need a forward vector as reference, use the door like I mentioned above if you can. Lets call this currentForward. You also need a right pointing vector. Which you can get from a transform if available (again from the door if its pointing the correct direction) or by rotating the forward vector by 90 degrees around the Y axis. Lets call this currentRight. Replace these two lines
newPosition.z += anim.GetFloat("speed")* mesh$$anonymous$$oveSpeed * Time.deltaTime;
newPosition.x += anim.GetFloat("direction") * mesh$$anonymous$$oveSpeed * Time.deltaTime;
with
newPosition += currentForward * anim.GetFloat("speed")* mesh$$anonymous$$oveSpeed * Time.deltaTime;
newPosition += currentRight * anim.GetFloat("direction") * mesh$$anonymous$$oveSpeed * Time.deltaTime;
Then modify this part
else {
transform.forward = Vector3.Normalize(new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical"))); // Normalizes the rotation of Z local while moving
float angle = Vector3.Angle(Vector3.forward,currentForward);
transform.Rotate(Vector3.up, angle);
lastdirection = Vector3.Normalize(new Vector3(Input.GetAxis("Horizontal"), 0.0f, Input.GetAxis("Vertical")));
}
To begin set currentForward to Vector3.forward and currentRight to Vector3.right.
I tried your edits, however I don't see any difference. The control issue is still there. =/
Are you setting currentForward and currentRight each time you enter a new room? What values are you using?
The fact that you don't see a difference suggests to me you are using Vector3.forward and Vector3.right as you're currentForward and currentRight in EVERY room, which shouldn't be the case. Note I mentioned to set it as these values to begin with. This is because only your first room is aligned with its forward and right directions aligned with the global ones. You need new values if the orientation of the camera and room change. You can't use the camera forward because it is slightly angled down. However, you might be able to use the camera right vector and rotate it -90 degrees to get a vector that points forward and will also be parallel to the ground.
$$anonymous$$y mistake. When I said there was no difference, this was due to a bit of an error on my part. That's fixed now so I tried your code and it actually got much worse. Now, my character cannot move. He merely walks in place. Not only that, but left is up, right is down, down is left, up is right, and when you stop pressing buttons he snaps to a different direction. I'm not sure what exactly is going on...
As for current Forward and current right, I tried a few things, including Vector3.forward and Vector3.right, GameObject.Find("$$anonymous$$ain Camera").Transform and a few other things, but nothing seems to change anything. What exactly should I be doing with those variables? What kind of variables should they be anyway, I made them public Vector3s.
Answer by Xepherys · Oct 25, 2013 at 08:58 PM
I don't know if this is possible, as I've never tried to write something quite like what you have, but is it possible to tie controls to the camera directly - so as the camera shifts so many degrees, the controls shift the same?
How exactly would I do that? I have no clue how to do it with code.
Your answer
![](https://koobas.hobune.stream/wayback/20220613120503im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to make camera position relative to a specific target. 1 Answer
Player controller 0 Answers
3D ball control issues 0 Answers
can i start playing from a specefic part of an audio clip ? 1 Answer
Use GUITexture to control Player 1 Answer