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 EricLuskJ · Jun 16, 2018 at 03:39 AM · rotation2d gameplayerupdatetranslate

Movement/Rotation Methods Don't Work Together

Hello everyone, I am very new to Unity and I am building a 2d top-down game. I have some code that allows me to point and click on the gameboard and have my player character walk towards the point at a constant speed while rotating to face the clicked location. To do this I call the following method "MoveClick" in Update:

 void MoveClick()
 {

     bool RMB = Input.GetMouseButton(1);
     if (RMB)
     {
         Vector3 clickPosition = Input.mousePosition;
         clickPosition = Camera.main.ScreenToWorldPoint(clickPosition);
         newPosition = new Vector3(clickPosition.x, clickPosition.y, transform.position.z);
         moveDirection = newPosition - transform.position;
         moveDirection.Normalize();
     }

     float targetAngle = Mathf.Atan2(moveDirection.y, moveDirection.x) * Mathf.Rad2Deg;
     transform.rotation =
     Quaternion.Slerp(transform.rotation,
                     Quaternion.Euler(0, 0, targetAngle),
                     clickRotation * Time.deltaTime);

     transform.position = Vector3.MoveTowards(transform.position, newPosition, moveSpeed * Time.deltaTime);

}

My understanding is that by calling this in Update, it will check for a right mouse click and if so, will move the character to that point. So far this works great.

My issue is that since this is a shooter, I will want to shoot things. The way my character moves makes shooting very accurate, but not so much while the player is stationary. So I need to edit or add a method that allows me to turn my player character in the direction of a mouse click (left mouse click) in addition to the one that already turns and moves them on a right mouse click. To solve this problem I basically copied my above method and changed some variable names. This is what I wrote:

 void RotateClick()
 {
     bool LMB = Input.GetMouseButton(0);
     if (LMB)
     {
         Vector3 lclickPosition = Input.mousePosition;
         lclickPosition = Camera.main.ScreenToWorldPoint(lclickPosition);
         lookPosition = new Vector3(lclickPosition.x, lclickPosition.y, transform.position.z);
         rotDirection = lookPosition - transform.position;
         rotDirection.Normalize();
     }
     float targetRotation = Mathf.Atan2(rotDirection.y, rotDirection.x) * Mathf.Rad2Deg;
     transform.rotation =
     Quaternion.Slerp(transform.rotation,
                     Quaternion.Euler(0, 0, targetRotation),
                     clickRotation * Time.deltaTime);
 }

Now, this also works well in Update. BUT ONLY WHEN I COMMENT OUT MY OTHER MOVEMENT METHOD. My update function looks like this:

  void Update()
     {
         //MoveKeys(); 
         MoveClick();
         RotateClick();
     }


Basically, the above code won't work properly if I call both MoveClick and RotateClick in Update, despite the fact that they each work just fine on their own. I encountered a similar issue with my method MoveKeys and have to keep it commented out. I've been stuck on this for several days now so any advice you guys have to give would be super appreciated!

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

196 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

Related Questions

Aim at my cursor 1 Answer

How to translate an object in an unknown angle ? 1 Answer

Player's rotation gets stuck when pressing two movement keys together, Player's Rotation breaks when pressing two movement buttons together 0 Answers

How would you create a 2D endless runner with objects moving in the background? 0 Answers

2D Sprite rotation 180 degrees 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