Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by TheMonkeysaur · May 13, 2017 at 04:15 PM · rotationdirection

Help with player rotation on rotating sphere

Hi,

I have a player object which I am able to move in a rotating sphere (a planet).

This works really well, I can move around the planet as I want to, horizontal input provide movement and vertical input provide rotation, so I press "A" and rotate left, then "W" to move forwards etc.

The problem I am having is that I don't want the player to have to rotate 180 to move and face in the opposite direction. Currently if the player moves backwards (pressing "S") the player will move backwards while still facing forwards. I would like, when the player inputs a backwards movement, for the player to rotate 180 and face their new direction too.

I'm not sure if the fact that my player is on a sphere which rotates is making this more complicated but all of the solutions I have tried haven't worked and have messed up the movement controls or the starting tranform position.

Any advice much appreciated.

CODE: using UnityEngine;

 public class PlayerMovement : MonoBehaviour
 {
 
     public float moveSpeed = 10f;
     public float turnSpeed = 10f;
 
     Transform myT;
 
     private void Awake()
     {
         myT = transform;
     }
 
     void Update()
     {
         Move();
         Turn();
     }
 
     void Turn()
     {
 
         float dir = turnSpeed * Time.deltaTime * Input.GetAxis("Horizontal");
         myT.Rotate(0, dir, 0);
     }
 
     void Move()
     {
         myT.position += myT.forward * moveSpeed * Time.deltaTime * Input.GetAxis("VerticalP");     
     }
 }
Comment
Add comment
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

1 Reply

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

Answer by NoBrainer-David · May 13, 2017 at 04:26 PM

You could rotate the player-character anytime the user pressed down on the s key, like so:

 if(Input.GetKeyDown("s"))
     myT.Rotate(0, 180, 0);

Now, the character will rotate but then walk backwards in the old direction. You can change this by only getting the absolute value from the vertical axis like so:

 Mathf.Abs(Input.GetAxis("Vertical"))
Comment
Add comment · Show 6 · 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 TheMonkeysaur · May 13, 2017 at 04:54 PM 0
Share

Thanks for the quick reply!

($$anonymous$$y mistake - it did work)

avatar image TheMonkeysaur · May 13, 2017 at 09:54 PM 0
Share

I'm sorry, it did actually work, my camera is set up so I couldn't actually see that it had worked (camera instantly flipped but because the player is a cube with one face standing on a uniform sphere I figured it was glitching!).

This works well, but I'm having a hard time making it work as I would like.

Currently, it will flip everytime "S" is pressed, I have tried to make it flip down only if it was facing up, and flip up (when pressing "W") only if it was facing down.

But it's not working, the player will not flip on "W" and will flip with every press of "S".

Here is what I tried.

  void $$anonymous$$ove()
     {
         float updown = $$anonymous$$athf.Abs(Input.GetAxis("VerticalP"));
 
         myT.position += myT.forward * moveSpeed * Time.deltaTime * updown;
 
         if  (facingUp = true && (Input.Get$$anonymous$$eyDown("s")))              
             myT.Rotate(0, 180, 0);
             facingUp = false;
             Debug.Log("Up");
 
         if (facingUp = false && (Input.Get$$anonymous$$eyDown("w")))            
             myT.Rotate(0, 180, 0);
             facingUp = true;
             Debug.Log("Down");
 }

The debug shows a continual output of Up/Down/Up/Down every frame.

Thanks for any help.

avatar image NoBrainer-David TheMonkeysaur · May 14, 2017 at 06:22 AM 0
Share

It's seems to me to be a problem with your braces. Use:

 if  (facingUp = true && (Input.Get$$anonymous$$eyDown("s")))
 {              
      myT.Rotate(0, 180, 0);
      facingUp = false;
      Debug.Log("Up");
 }
  
 if (facingUp = false && (Input.Get$$anonymous$$eyDown("w")))
 {            
      myT.Rotate(0, 180, 0);
      facingUp = true;
      Debug.Log("Down");
 }

In C#, you use braces to create blocks of code. Only the next line is affected by the if-statement, unless you use braces to form a block of code, like I did above. This can be quite confusing, so some developers prefer to always use the braces for clarity.

avatar image TheMonkeysaur NoBrainer-David · May 14, 2017 at 12:16 PM 0
Share

Thanks, that makes sense!

I'm having a strange problem, in that when I debug facingUp, it's always false no matter what, and despite this, my "if faceingUp = true" is the only code which runs!

 bool facingUp = true;
 
 void Update()
     {
         $$anonymous$$ove();
         Turn();
         Debug.Log(facingUp);
     }
 
  void $$anonymous$$ove()
     {
 
         //float updown = $$anonymous$$athf.Abs(Input.GetAxis("VerticalP"));
 
         myT.position += myT.forward * moveSpeed * Time.deltaTime * (Input.GetAxis("VerticalP"));
 
         if (facingUp = true && (Input.Get$$anonymous$$eyDown("s")))
         {
             myT.Rotate(0, 180, 0);
             facingUp = false;
             Debug.Log("Down");
         }
 
         if (facingUp = false && (Input.Get$$anonymous$$eyDown("w")))
         {
             myT.Rotate(0, 180, 0);
             facingUp = true;
             Debug.Log("Up");
         }
 }


Debug will show "False" every step, and will show "Down" (and flip the player) whenever "S" is pressed (even once FacingUp is no longer true) but nothing will happen when pressing "W".

Can't seem to figure out what is wrong here.

Thanks for any help.

I am assu$$anonymous$$g that && in my if statement requires both conditions to return true before running the code, that is right, isn't it?

Show more comments
Show more comments

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

117 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

OverlapBoxAll rotation 2 Answers

AddForce in direction of different object? 1 Answer

Rotate a camera by 90 degree without breaking direction vector? 0 Answers

Determining if my target is standing upright 4 Answers

Rotate non-forward vector to face direction? 0 Answers


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