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 /
  • Help Room /
avatar image
0
Question by wenson16 · Jan 28, 2017 at 03:16 AM · movement scripttop-downturningvector3.angle

Gameobject not turning in the right direction

So I have a 2d top down scene with XY axis, what I want to happen is to make the gameobject face the position of my mouse input and then move forward. With my current code it turns right then move forward when the position of the mouse is greater than its transform position in X axis(transform.position.x) which is good. However, if the position of the mouse is less than its transform.position.x it doesn't turn and moves forward right away. I want the gameObject to turn left first then move if this condition happens.

Here's my code:

         // waypPoint is the Input.mousePosition
         float angle = Vector3.Dot (transform.right, (wayPoint - transform.position).normalized);

         if (angle >= 0.05f) {
             // Do some turn function here...
         } else {
             // Do some move forward here...
         }

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 AR_Rizvi · Jan 28, 2017 at 05:54 AM 0
Share

Can you please tell me how you are turning you player

avatar image wenson16 AR_Rizvi · Jan 28, 2017 at 06:53 AM 0
Share

sorry here's my code for turning and moving forward to target

 // Turning function
 // $$anonymous$$ouse click point as target
 void TurnToWayPoint (Vector3 target) {
      Vector3 diff = target - myTransform.position;
      diff.Normalize ();
      rotZ = $$anonymous$$athf.Atan2 (diff.y, diff.x) * $$anonymous$$athf.Rad2Deg;
      rotationDirection = Quaternion.Euler (0, 0, rotZ - 90f);
      transform.rotation = Quaternion.RotateTowards (myTransform.rotation, rotationDirection, rotateSpeed * Time.deltaTime);
 
 // $$anonymous$$ove Function
 void $$anonymous$$ove () {
      transform.position = Vector3.$$anonymous$$oveTowards (transform.position, wayPoint, speed * Time.deltaTime * 0.25f);
 }




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

62 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

Related Questions

How to give the player a limited amount of moves? 1 Answer

Player keeps spinning after collision 0 Answers

Movement Flipped in Game View? 0 Answers

How to get the angle between two objects with regard to a face side 2 Answers

Sprite moves at an angle that is 2x the rotation of the sprite. 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