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 SSEthanCray · Jul 22, 2018 at 09:24 PM · 2drotationrigidbody2dworldspacelocalspace

Idk what's wrong with my code..

So, I made a script that would make my player look at the mouse.position if you held down left click, and that function works fine. I also made it so when you lifted the mouse button it would launch. No matter what I do, it will keep moving up in world space, but I need it in local space. Heres my code:

     private void LookAtMouse() {
 
         if (_charging) {
 
             Vector3 _mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 
             direction = new Vector3(_mousePosition.x - transform.position.x, _mousePosition.y - transform.position.y,0);
             transform.up = direction;
 
         } else {
 
             //transform.rotation = Quaternion.Euler(0,0,0);
 
         }
 
     }
 
     private void Launch() {
 
         Debug.Log(_launchMultiplier);
 
         if (Input.GetMouseButtonDown(0)) {
 
             _anim.SetTrigger("Charge");
             _charging = true;
             StartCoroutine(PowerLaunch());
 
 
         } else if (Input.GetMouseButtonUp(0)) {
 
             _anim.SetTrigger("Launch");
             _rb.AddForce(transform.up * launchSpeed * _launchMultiplier);
             _charging = false;
             _launchMultiplier = 0;
 
         }
 
     }
Comment
Add comment · Show 1
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 Glurth · Jul 22, 2018 at 10:09 PM 0
Share

I think you may need to rephrase that last sentence, I just don't understand what you mean.

1 Reply

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

Answer by Bunny83 · Jul 22, 2018 at 10:33 PM

Well this boils down to some important counter questions:

  • Do you use a perspective camera or an ortographic camera? If you use a perspective camera your "ScreenToWorldPoint" would always return the camera position since you implicitly pass 0 as z distance

  • Where and when do you actually call your "LookAtMouse" method? Inside your Launch method you use the current up vector to add a force to the rigidbody. Keep in mind that once the force has been added any change of the object direction won't have any effect on the direction it's moving. Rigidbody physics is always simulated in worldspace. So it's velocity is a worldspace vector that is independent of the orientation of the object.

  • Finally what have you actually done yourself to debug your issue? Have you tried printing the vector that you use when you add the force? What direction do you get?

Comment
Add comment · Show 1 · 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
avatar image SSEthanCray · Jul 22, 2018 at 11:23 PM 0
Share

Hi, thanks so much! I didn't know tat rb physics are in worldspace. I ended up not using addforce, and I just changed the transform.position directly. Now it works great. Thanks!

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

210 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

Related Questions

Unity 2D Apply force in Z Direction 0 Answers

Projetil rotation in straight line trajectory 2 Answers

Clamping rotation doesn't work when local axis is different from global 0 Answers

How to rotate an RigidBody2D object to an angle and get back to the previous position in the smooth way? 1 Answer

Transform.rotation is setting local rotatoin 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