Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SuperSparkplug · Oct 23, 2013 at 08:57 AM · playercontrolorientation

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mattssonon · Oct 23, 2013 at 01:24 PM 0
Share

I can't really visualize your problem, can you provide screenshots of the game view or explain it in more detail?

avatar image SuperSparkplug · Oct 23, 2013 at 08:20 PM 0
Share

Sorry, it was late at night when I wrote that so I probably could've explained this better. Anyway, here's some images.

alt text

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.

alt text

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?

screen shot 2013-10-23 at 4.14.51 pm.png (420.7 kB)
screen shot 2013-10-23 at 4.15.13 pm.png (486.1 kB)
avatar image mattssonon · Oct 24, 2013 at 07:33 AM 1
Share

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);.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SuperSparkplug · Oct 25, 2013 at 08:01 PM 0
Share

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.

avatar image EvilWarren · Oct 26, 2013 at 02:59 AM 0
Share

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.

avatar image SuperSparkplug · Oct 26, 2013 at 07:15 AM 0
Share

I tried your edits, however I don't see any difference. The control issue is still there. =/

avatar image EvilWarren · Oct 26, 2013 at 04:12 PM 0
Share

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.

avatar image SuperSparkplug · Oct 26, 2013 at 09:11 PM 0
Share

$$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.

Show more comments
avatar image
0

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SuperSparkplug · Oct 25, 2013 at 09:05 PM 0
Share

How exactly would I do that? I have no clue how to do it with code.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges