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 Spellking · Feb 23, 2021 at 09:31 AM · 2d gamelookrotation

how to make 2d sprite flip vertically while looking at the mouse

I've got a 2d player sprite for a top-down shooter game that is supposed to flip vertically to face the mouse, and he is supposed to "hold" a gun sprite that is supposed to constantly point directly at the mouse. However, they both don't work properly at some angles and I can't figure out why. This is the code for the player:

void Start() { localScale = transform.localScale; }

 void Update()
 {
     pMovement.x = Input.GetAxisRaw("Horizontal");
     pMovement.y = Input.GetAxisRaw("Vertical");

     animator.SetFloat("Horizontal", pMovement.x);
     animator.SetFloat("Vertical", pMovement.y);
     animator.SetFloat("Speed", pMovement.sqrMagnitude);

     mousePos = cam.ScreenToWorldPoint(Input.mousePosition);

 }

 void FixedUpdate()
 {
     playerRB.MovePosition(playerRB.position + pMovement * moveSpeed * Time.fixedDeltaTime);

     Vector2 lookDir = mousePos - playerRB.position;
     float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg - 90f;
     

     if (angle > 0)
     {
         transform.localScale = new Vector3(localScale.x, localScale.y, localScale.z);
     }
     else if (angle < 0)
     {
         transform.localScale = new Vector3(-localScale.x, localScale.y, localScale.z);
     }
 }

and this is the code so far for the gun:

void FixedUpdate() { rb.MovePosition(rb.position + movement moveSpeed Time.fixedDeltaTime);

     Vector2 lookDir = mousePos - rb.position;
     float angle = Mathf.Atan2(lookDir.y, lookDir.x) * Mathf.Rad2Deg;
     if (transform.position.x <= 0)
     {
         rb.rotation = angle;
     }
     else if (transform.position.x > 0)
     {
         rb.rotation = angle;
     }
     
 }
 
 void Update()
 {
     movement.x = Input.GetAxisRaw("Horizontal");
     movement.y = Input.GetAxisRaw("Vertical");

     mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
 }

}

Any help would be greatly appreciated! I am kind of a noob and learned C# for this project, so it's still all very new to me. Thanks for any help!

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

191 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

Related Questions

Help with trying to make a 2D character move in all four directions via the arrow keys 2 Answers

I want to alternate between gameobjects by using transforms in c# 1 Answer

2D Enemy Script Rotate to look at player. 0 Answers

Scale sprite-animated character in 2D to fake distance (Monkey Island 3 style) 0 Answers

Looking for 2d filters: glow, dropshadow, outline, etc. 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