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 /
avatar image
0
Question by johnsmith114 · Jul 25, 2014 at 04:10 PM · positionaddforcemovement script2d-physics

Moving player to the opposite direction of the mouse position

I've already tried to ask this question some days ago but nobody has answered yet, I'll try to ask this again in hope that anyone could help me.

Hello, I've been trying to make it so that my player gets propelled to the opposite direction to where the user makes a mouse click. The following code was applied to the player. I can't get it to work even though the DrawLine debug works and the Debug.log for 2DForceVector works too.

Hopefully you can help me. Thanks in advance.

 var movementMaxVelocity : float = 10;
 var jumpSpeed : float = 3;
 var movementForce : float = 10;
 var mouseVectorPosition : Vector2;
 var playerVectorPosition : Vector2;
 var 2DForceVector : Vector2;
  
 function Update () {
  
     mouseVectorPosition= Camera.main.ScreenToWorldPoint(Input.mousePosition);
     playerVectorPosition = transform.position;
     2DForceVector = (mouseVectorPosition - playerVectorPosition).normalized;
     rigidbody2D.AddForce(2DForceVector * movementForce * -1);
  
 }
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

1 Reply

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

Answer by robertbu · Jul 25, 2014 at 04:48 PM

I could not see anything conceptually wrong with your code, so I tried it out. I had to change the name of 2DForceVector since you cannot have a variable starting with a number, and I added a Input.GetMouseButtonDown() to detect a click. The code then worked. The sprite I picked to test it with happened to be a balloon, and the clicking looks good with this sprite.

 #pragma strict
 
 var movementMaxVelocity : float = 10;
 var jumpSpeed : float = 3;
 var movementForce : float = 10;
 var mouseVectorPosition : Vector2;
 var playerVectorPosition : Vector2;
 var ForceVector : Vector2;
  
 function Update () {
  
      if (Input.GetMouseButtonDown(0)) {
         mouseVectorPosition= Camera.main.ScreenToWorldPoint(Input.mousePosition);
         playerVectorPosition = transform.position;
         ForceVector = (mouseVectorPosition - playerVectorPosition).normalized;
         rigidbody2D.AddForce(ForceVector * movementForce * -1.0);
     }
 }

Note if you reverse the order of the subtraction of the vectors on line 12, you will not need the -1 in line 13.

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 johnsmith114 · Jul 25, 2014 at 06:54 PM 0
Share

Apparently on the player I was already working on, it didn't work, but making a new sprite and applying the code to it worked. Thanks, now I'll just have to track down the problem with the old sprite.

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

2 People are following this question.

avatar image avatar image

Related Questions

Getting final position after rigidbody.addForce 5 Answers

Why can't i addforce to a rigidbody? 2 Answers

Adding force to mouse position adds force relative to screen center 0 Answers

Rigidbody.Addforce against position 0 Answers

tilemap collision not working with sprite 2 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