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 /
This question was closed Jun 09, 2017 at 08:17 PM by THE-Scientist34 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by THE-Scientist34 · Jun 09, 2017 at 06:43 AM · 2d game2d-physics2d sprites2d rotation

My player is not rotating upwards when it moves upwards. Any advice?

I'm making a 2D, top-down stealth game. I'm having trouble with my player rotating up wards when it is moving upwards, but it does rotate towards all the other directions when it is moving towards them. I've never encountered a problem like this before, and none of my friends that i'm working with to solve this haven't been of much help.

using UnityEngine; using System.Collections; using UnityEngine.SceneManagement; public class CompletePlayerController : MonoBehaviour { public Rigidbody2D rb; float constantSpeed = 5f; float sprintSpeed = 10f; private bool a1 = Input.GetKeyUp("UpArrow"); private bool a2 = Input.GetKeyUp("DownArrow"); private bool a3 = Input.GetKeyUp("RightArrow"); private bool a4 = Input.GetKeyUp("LeftArrow"); private bool A = Input.GetKeyUp("A"); private bool WK = Input.GetKeyUp("W"); private bool S = Input.GetKeyUp("S"); private bool D = Input.GetKeyUp("D"); Vector3 prevpos; Vector3 curpos; void Awake() { // prevpos is the postion of the gameobject in the previous frame prevpos = transform.position; } void FixedUpdate() { Rigidbody2D rb = GetComponent<Rigidbody2D>(); //curpos is the current position of the gameobject Vector3 curpos= (transform.position - prevpos); // rv stands for rotation value float rv = Mathf.Atan2(curpos.x, curpos.y) * Mathf.Rad2Deg; rv = 360 - rv; Quaternion p = Quaternion.identity; p.eulerAngles = new Vector3 (0, 0, 0); Quaternion q = Quaternion.identity; q.eulerAngles = new Vector3 (0, 0, rv); if (a1 && a2 && a3 && a4 && A && WK && S && D){ transform.rotation = p; } else if(rv != 0 && rv != 360) transform.rotation = q; prevpos = transform.position; float moveHorizontal = Input.GetAxisRaw ("Horizontal"); float moveVertical = Input.GetAxisRaw ("Vertical"); Vector2 movement = new Vector2 (moveHorizontal, moveVertical); if (Input.GetKey ("space")) rb.velocity = sprintSpeed * movement; else rb.velocity = constantSpeed * movement; } }

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 Zablon · Jun 09, 2017 at 03:33 PM 0
Share

The way you are showing the code is almost unreadable for someone, please format it in the correct way, with a line for each line of code, and indexed in a proper way.

avatar image Vicarian · Jun 09, 2017 at 03:46 PM 0
Share

There are a number of issues with this script that don't include the formatting issues. The conditional you have detecting your input expects all the inputs to be active at the same time, you have a class-scope reference to your Rigidbody2D overwritten by a local scope reference of the same name, a redundant assignment to Quaternion q in your FixedUpdate() method (Quaternion.identity and Quaternion.eulerAngles(Vector3.zero) are equivalent), and finally, the entire script is rendered invalid by assigning an Input value to a $$anonymous$$onoBehaviour field initializer.

avatar image sh_code Vicarian · Jun 09, 2017 at 03:58 PM 0
Share

...and the funny thing is that none of the issues you named (which I all agree with) are the cause of the original question's problem :-D

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by sh_code · Jun 09, 2017 at 03:57 PM

"upwards" (y+, increase on the y axis) is "euler angle z with value 0" (OR 360, OR anything that gives 0 when modulo-d by 360). therefore your

 else if(rv != 0 && rv != 360) transform.rotation = q;

will be skipped for upwards (eulerAngles = Vector3(0, 0, rv ==0) direction.

also, interesting thing with the keypress bools being

 bool a1 = Input.GetKeyUp();

defined in class head... wouldn't expect it to work, as afaik, assignment at declaration happens only once and bools are byval, not byref, so... I'd say you're missing these assignments in your update... but if it works for you like this, why not... your problem and solution are already described in the first part of my comment ;)

Comment
Add comment · Show 2 · 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 sh_code · Jun 09, 2017 at 04:03 PM 0
Share

also, while I mentioned the modulo thing - if you insist on working with eulerAngles (which is mathematically unwieldy, but comprehensible to most humans, unlike quaternions), don't use "if euler angle is value whatever", use "if euler angle modulo 360 is value whatever", that little change will take care of all the angles wrapping around, when angle x == x+ 2 times 360 == x + 3 times 360, etc... yes, those euler angles sometimes give values like that. it can give you 725 meaning 5 degrees (to which we got rotating around the whole circle 2 times and then some), or it can even give you -725 (i think) meaning the same 5 degrees, but we got to them by rotating around the whole circle 2 times the other direction... it's kind of a mess, so i recommend: 1. using the aforementioned modulo technique in comparisons 2. very quickly learning quaternions and direction vectors and start avoiding eulerAngles whenever possible.

avatar image sh_code · Jun 09, 2017 at 04:08 PM 0
Share

there's even functions on fransform that are "Rotate" and "RotateAround", and Quaternion has static factory methods in the likes of "Rotation from euler angles", "Rotation from direction vector"... i would recommend first creating new, clean "rotation from euler angles" that holds the delta rot, and then do player.transform.rotation += thatDeltaRotQuaternion.

...but now I'm not sure if with quaternions you add or multiply to get their composite. try it out, one of them might give you syntax error, that's not the right one. or if they both work, well... test which gives the correct resulting euler angle.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Shooter 2D - Problem with rotation of bullet to face direction (velocity) 2 Answers

Slow Sprite Rotation Toward Movement Direction - Top-Down 2D Game 0 Answers

Bullet follows player mouvement when It instintiates. 1 Answer

How to Continue Enemy Movement Without Updating the Position 0 Answers

Can anybody help me with this? Character bodying is falling apart. 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