- Home /
The question is answered, right answer was accepted
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; } }
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.
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.
...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
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 ;)
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.
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
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