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 /
avatar image
0
Question by KielanT · Dec 12, 2018 at 04:37 PM · rotationscripting problemunity 2dscripting beginnermovement script

Unity 2D movement and rotate sprite to the direction it is moving in

Hi, I am making a top down 2D game and i want my sprite to rotate on the Z-Axis depending on what direction it is moving. This is my code so far. I am still a beginner any help is appreciated.

 public class PlayerController : MonoBehaviour {
     
         public float speed;
     
         private Rigidbody2D rigBod;
         private Vector2 moveVelocity;
     
         void Start ()
         {
             rigBod = GetComponent<Rigidbody2D>(); 
         }
     
         void Update()
         {
             if (GameObject.Find("GameManager").GetComponent<GameManager>().gameState == true)
             {
                 PlayerControls();
                 
             }
         }
     
         void FixedUpdate()
         {
             if (GameObject.Find("GameManager").GetComponent<GameManager>().gameState == true)
             {
                 PlayerMovement();
             }
         }
     
         void PlayerControls()
         {
             Vector2 moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
             moveVelocity = moveInput.normalized * speed;
         }
     
         void PlayerMovement()
         {
             rigBod.MovePosition(rigBod.position + moveVelocity * Time.fixedDeltaTime);
         }
 }
 
 
Comment
Add comment · Show 2
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 hec_91 · Dec 12, 2018 at 07:59 PM 0
Share

Since your question as already been answered, I just want to give you some advice. Calling GameObject.Find() and GetComponent() in any kind of Update (so FixedUpdate and LateUpdate too) is a really bad practice, because they are really slow functions that should never be called every frame. As you did for the Rigidbody, just store your Game$$anonymous$$anager reference in a variable in Start() (let's say you call it game$$anonymous$$anager) and just check if (game$$anonymous$$anager.gameState == true) in your Updates.

avatar image KielanT hec_91 · Dec 12, 2018 at 09:03 PM 0
Share

Thank you mate

1 Reply

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

Answer by VeraDu · Dec 12, 2018 at 07:38 PM

Each time you move into new position, you need to calculate vector, that points from your previous position to new one (myVector = newPosition - previous Position). Then go angle = Vector2.Angle(Vector2.up, myVector ) to find z angle, corresponding to your new direction. Just set your z rotation to that angle and that's all. Hope, it helps.

Comment
Add comment · 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

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

206 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

Related Questions

how to make a detect rotation script for negative rotation too 2 Answers

How can I make particles apeear in exact position? 0 Answers

How Do I rotate a Object Based On the player's location? 1 Answer

Why wont my character stop moving when it collides with the obstacles?,Why doesn't my movement script stop? 0 Answers

How can I make my 2D sprite move in the direction of the most recent key pressed? 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