Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 rykonvolta · Dec 15, 2021 at 09:16 PM · rotationmovementbug-perhapseulerangles

How to make a 2d player sprite rotate without messing up movement with C#,

Here is the buggy code. When I apply the conditionals that allow the character to rotate, rotation works just fine. However, up and down cause movement up on the y-axis, while left and right cause movement down on the y-axis. x-axis, or horizontal movement, are impossible. Movement is normal when I comment out the if{}else if{} blocks containing transform.eulerAngles = new Vector3(0, 0, rotation); to disable them, so the conclusion is that eulerAngles are doing something to Vector3. Resetting Vector3 to the Input.GetAxisRaw values does not fix the problem.,So when I use the following C# script on my player, I rotate the character but the movement gets jacked up. This is a 2D Sprite to rotate on the z-axis in relation to it's movement direction. It's set to move at 90 and 45 degree angles so it can turn with corners as well:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class player : MonoBehaviour { private BoxCollider2D boxcollider; private Vector3 moveDelta; private Quaternion rotationDelta;

 // Start is called before the first frame update
 private void Start()
 {
     boxcollider = GetComponent<BoxCollider2D>();
 }

 // Update is called once per frame
 private void FixedUpdate()
 {
     float x = Input.GetAxisRaw("Horizontal");
     float y = Input.GetAxisRaw("Vertical");

     //reset moveDelta
     moveDelta = new Vector3(x, y, 0);

     //Make character move
     transform.Translate(moveDelta * Time.deltaTime);

     //make player rotate with direction
     if (moveDelta.x>0 && moveDelta.y==0)
     {
         transform.eulerAngles = new Vector3(0, 0, 90);
     }
     else if (moveDelta.x<0 && moveDelta.y==0)
     {
         transform.eulerAngles = new Vector3(0, 0, 270);
     }
     else if (moveDelta.y > 0 && moveDelta.x==0)
     {
         transform.eulerAngles = new Vector3(0, 0, 180);
     }
     else if (moveDelta.y < 0 && moveDelta.x==0)
     {
         transform.eulerAngles = new Vector3(0, 0, 360);
     }
     else if (moveDelta.y > 0 && moveDelta.x > 0)
     {
         transform.eulerAngles = new Vector3(0, 0, 135);
     }
     else if (moveDelta.y > 0 && moveDelta.x < 0)
     {
         transform.eulerAngles = new Vector3(0, 0, 225);
     }
     else if (moveDelta.y < 0 && moveDelta.x > 0)
     {
         transform.eulerAngles = new Vector3(0, 0, 45);
     }
     else if (moveDelta.y < 0 && moveDelta.x < 0)
     {
         transform.eulerAngles = new Vector3(0, 0, 315);
     }

     
 }

}

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

0 Replies

· Add your reply
  • Sort: 

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

262 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 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 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

Different rotation behaviour in regular playmode vs. step-by-step? 0 Answers

i'm using a code to make my cube walk with character controller but when i play the cube spins. 0 Answers

copy rotation script doesent copy the full 360 degrees? 0 Answers

GameObject not looking at me.. 1 Answer

Quaternion AngleAxis on pitch give random small value on others axis 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