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
1
Question by LukaKotar · May 06, 2012 at 08:56 PM · objectsmoveobjects-movement

Amnesia-like object moving?

Hi.

I ran into a few problems with Unity's DragRigidbody script. First off, I would like the object being dragged to move faster, I'd like the object to always move as the player moves, and to rotate with the player, so it is always in the view.

Here is my modified DragRigidbody script that I'd like to add these features to:

     var damper = 5.0;
 var drag = 10.0;
 var angularDrag = 5.0;
 var distance = 0.2;
 var throwForce = 5.5;
 var attachToCenterOfMass = false;
 var isPressed = false;
 
 static var springJoint : SpringJoint;
 
 function Update ()
 {
     // Make sure the user pressed the mouse down
     if (!Input.GetButtonDown ("Fire1"))
         return;
     if (Input.GetButtonDown ("Fire1")) {
         isPressed = true;
         Debug.Log("isPressed = true;");
     }
 
     var mainCamera = FindCamera();
         
     // We need to actually hit an object
     var hit : RaycastHit;
     if (!Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition),  hit, Mathf.Infinity))
         return;
     // We need to hit a rigidbody that is not kinematic
     if (!hit.rigidbody || hit.rigidbody.isKinematic)
         return;
     
     if (!springJoint)
     {
         var go = new GameObject("Rigidbody dragger");
         var body : Rigidbody = go.AddComponent ("Rigidbody") as Rigidbody;
         springJoint = go.AddComponent ("SpringJoint");
         body.isKinematic = true;
     }
     
     springJoint.transform.position = hit.point;
     if (attachToCenterOfMass)
     {
         var anchor = transform.TransformDirection(hit.rigidbody.centerOfMass) + hit.rigidbody.transform.position;
         anchor = springJoint.transform.InverseTransformPoint(anchor);
         springJoint.anchor = anchor;
     }
     else
     {
         springJoint.anchor = Vector3.zero;
     }
     
     springJoint.spring = spring;
     springJoint.damper = damper;
     springJoint.maxDistance = distance;
     springJoint.connectedBody = hit.rigidbody;
     
     StartCoroutine ("DragObject", hit.distance);
 }
 
 function DragObject (distance : float)
 {
     var oldDrag = springJoint.connectedBody.drag;
     var oldAngularDrag = springJoint.connectedBody.angularDrag;
     springJoint.connectedBody.drag = drag;
     springJoint.connectedBody.angularDrag = angularDrag;
     var mainCamera = FindCamera();
     while (isPressed) //(Input.GetButton ("Fire1"))
     {
         var ray = mainCamera.ScreenPointToRay (Input.mousePosition);
         springJoint.transform.position = ray.GetPoint(distance);
         yield;
         while (Input.GetButtonDown("Fire2")) {
         springJoint.rigidbody.MovePosition(transform.position + transform.forward * throwForce);
         yield WaitForSeconds(0.01);
         isPressed = false;
         Debug.Log("Thrown? DragRigidbody.js");
         }
     }
     
     
     if (springJoint.connectedBody)
     {
         springJoint.connectedBody.drag = oldDrag;
         springJoint.connectedBody.angularDrag = oldAngularDrag;
         springJoint.connectedBody = null;
     }
 }
 
 function FindCamera ()
 {
     if (camera)
         return camera;
     else
         return Camera.main;
 }



If you could help me, I would greatly appreciate it!

Thanks!

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 DGArtistsInc · May 06, 2012 at 09:02 PM 1
Share

im sorry to tell you but when i see extremely long pieces of code i tend to over look them. Narrow the question down to what is necessary for the community to answer your question.

avatar image LukaKotar · May 06, 2012 at 09:09 PM 0
Share

Oh, sorry. But I don't know how I can edit it to look better. :/ Thanks for the response anyway.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · May 06, 2012 at 09:07 PM

I'm not sure what kind of setup you have, a 3D firstperson view or a 2D side scrolling view, but if you don't like the springy nature of joints, you could simply parent the object to your player. That way it moves along with the player without any delay and it rotates with the player.

edit
Well in this case just reduce the drag / damper value of the DragScript. That would make the dragging more responsive. But be careful, the spring joint will be more springy then.

The rotation have to be handled manually i guess. Just save the rotation of the object relative to your player. I guess this could be done by multiplying the objects rotation (quaternion) with the inverse of your players quaternion. No you just need to do the reverse in the coroutine with the stored rotation to get the objets rotation.

Another way would be a temporary empty parent gameobject which is rotated like the player.

I haven't tested those things, but it should work.

Comment
Add comment · Show 3 · 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 LukaKotar · May 06, 2012 at 09:12 PM 0
Share

Thanks, but that's not what I'm trying to do. It's a 3D game in first person. I am only trying to speed it up and make it to always rotate with the player, with a little delay.

avatar image LukaKotar · May 07, 2012 at 01:38 PM 0
Share

Thanks. But if I change the settings, the object moves a little bit faster, but not enough, plus it starts bouncing. Also I am lost with these Quaterions, not quite sure how to use them. I found a script called DragObject on Google, and it works well enough, the only two problems are: I can't move the object up and down, and it does not work with locked constrains in Rigidbody component, for example, I want the door to only rotate on the Y axis and not to move on any axis. You can see the script on the site I got it from: http://unifycommunity.com/wiki/index.php?title=DragObject and I hope you, or anybody else can help me with it.

avatar image LukaKotar · May 07, 2012 at 01:55 PM 0
Share

^^and it also ignores collision.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Orbiting objects appear stretched 1 Answer

Make Objects to ricochet 1 Answer

Move object in array of objects 0 Answers

Gather objects in one point 1 Answer

Moving two objects and avoiding overlap with each other 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