Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Baroni · Aug 24, 2010 at 01:39 PM · physicsrigidbodymovedrag

Moving a rigidbody onto exact mouse position using rigidbody.MovePosition?

Hello there,

my situation: I get the mouse position in 3D space while dragging the object, and it should move to this position. The script works very well so far, but the rigidbody object don't move to the exact mouse position. It recognises the mouse position, and flies to that direction... and it won't stop continue flying around, unless we release the mouse button.

And the important part of my drag script:

function FixedUpdate () {

if (dragging == true) {

rigidbody.isKinematic = false; var p: Vector3;

var mainCamera = FindCamera();
// Position on the near clipping plane of the camera in world space p = mainCamera.ScreenToWorldPoint(Vector3 (Input.mousePosition.x, Input.mousePosition.y, mainCamera.nearClipPlane )); // Position relative to the eye-point of the camera p -= mainCamera.transform.position;

//[...] ...further distance calculation not necessary here... [...]

//finally, p holds the mouse position

//rigidbody.position = p;

var collisionMoveFactor = 0.1; p = p.normalized*collisionMoveFactor;

//the questionable part of this script: rigidbody.MovePosition(rigidbody.position + p);

// only move in its x,z coordinates, rememberedYPos = 1 rigidbody.position.y = rememberedYPos;
} }

So, I think the mouse position will always be added to the object position... How to avoid this, and only move it to the mouse position?

Edit For the complete script see my other question

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 tylo · Aug 26, 2010 at 06:39 AM

Try calculating a heading and using the rigidbody.AddForce method.

Vector3 heading = (p - transform.position).normalized;
rigidbody.AddForce( heading * someThrustValue * Time.deltaTime);

Sadly I've never tried to do anything with the camera and the mouse just yet, so I could be completely off base.

EDIT:

When you're obejct has reached the desired position, raise the rigidbody's drag value to stop it from moving.

if( transform.position == p )) //I think p is what you are using to determine the point in space you want the object to move too.
{
    rigidbody.drag = 1000;
}

Give that a shot.

EDIT 2: Electric Boogaloo

You've shown that your results do not have the cube staying with the cursor as it moves, but rather it tries its best to keep up with it. Hopefully this code block will fix that with a different method of moving objects.

transform.position = new Vector3(p.x, transform.position.y, p.z);

I believe that will cause your cube to stay precisely with the mouse pointer as it moves around the screen without it flying in the air.

Comment
Add comment · Show 6 · 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 Baroni · Aug 26, 2010 at 12:37 PM 0
Share

thanks for the reply! I have tried your script, and the object moves a bit more controlable than with my $$anonymous$$ovePosition code. Now the new behavior is also problematic. The object try to move in the direction of the mouse, thats fine, but it accelerates to it, moves beyond the mouse, slows down, turns, accelerates to the mouse and so on. That's only when I not change the mouse position, otherwise, it circles around the mouse until it finally crashes into a wall and then accelerates again. Any suggestions to fix this or to slow the object down if it gets near the mouse?

avatar image tylo · Aug 27, 2010 at 04:07 AM 0
Share

Sorry for the late reply. Yes I do have a solution for this. You can retard the velocity by raising the rigidbody's drag value.

So, when your object is now where your mouse is pointing, raise the rigidbody drag value to a value that will stop it, like 999999 or something less ridiculous.

avatar image Baroni · Aug 27, 2010 at 11:44 AM 0
Share

Thanks for that, I gave you an upvote for your efforts ;) I used the mouse over+exit functions. The drag value fixes the problem if I move the object in a straight line, but if I change the mouse position more often it keeps circling. $$anonymous$$aybe this http://baronium.bplaced.net/Unity/rigiddrag2.html will say more than I can express.

avatar image Baroni · Aug 27, 2010 at 11:56 AM 0
Share

Sorry for my rethink, but I think it would look better if the object is always under the mouse, and not moving. This would solve this problem in addition. $$anonymous$$aybe this is a step into another direction of scripting, but I also tried the rigidbodydrag asset script to include the collisions I need, without the success I've wished, and the objects are moving with this script too. Also I tried to set the object position directly using transform.position, that gave me the result I wanted, but as you surely know, that does not include the walls very well. Thanks very much in advance

avatar image tylo · Aug 27, 2010 at 09:55 PM 0
Share

Well, I am glad to see that at the very least you have a functioning script going! Yes, I can see why the object is doing that. Forces may not be the best solution here, then. $$anonymous$$y suggestion is that you directly update the cube's transform to the coordinates of where the mouse is pointing, then. This should keep the cube with the mouse pointer at all times. I will try and update my answer with a code block.

Show more comments

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

No one has followed this question yet.

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Physics update rate problems 1 Answer

RigidBody Script Conflict 1 Answer

Question on Physic Materials, Rigibodies, and Acelleration 0 Answers

Is there any way to apply drag to a 3d rigidbody in only 1 direction? 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