• 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
Question by RyanAchtenSoma · May 19, 2016 at 09:38 AM · rigidbodyraycastcolliderpositioning

Smooth rigidbody movement

Hi Unity Community,

I have written a script to move an object via ray cast to positions on a terrain, however I can’t for the life of me seem to make this movement smooth. I have tried all manner of t$$anonymous$$ngs trying to figure this out (as you can probably tell by the amount of commented-out code below); disabling various rigidbody variables, however to no avail.

One approach that (kind of) worked was disabling the collider attached to the object, however this lead to the object inadvertently sinking below the terrain.

Can anyone please advise me as to the best approach to go about this would be? I feel like this should be very simple but I am over complicating it.

Many thanks in advance, Ryan

         //Move
         if ( Input.GetKey(KeyCode.E))
         {    
 //            if (Input.GetKeyDown(KeyCode.E))
 //            {
 //                modObj.GetComponent(BoxCollider).enabled = false;                
                 initPos = modObj.transform.position;
                 var initRotation = modObj.transform.rotation;    
 //            }
 //            
 //            modObj.GetComponent(Rigidbody).isKinematic = true;
 //            modObj.GetComponent(Rigidbody).useGravity = false; 
     
             moveObject(modObj, initPos, initRotation);
         }
         else{
 //            modObj.GetComponent(BoxCollider).enabled = true;
 //            modObj.GetComponent(Rigidbody).isKinematic = false;
 //            modObj.GetComponent(Rigidbody).useGravity = true; 
         }


 function moveObject(modObj : GameObject, initPos : Vector3, initRotation : Quaternion)
 {
     //Debug.Log("Moving Object");
 
     var $$anonymous$$t : RaycastHit;
     var foundHit : boolean = false;
     
     foundHit = Physics.Raycast(transform.position, transform.forward, $$anonymous$$t);
     //Debug.DrawRay(transform.position, transform.forward, Color.blue);
     
     if(foundHit && $$anonymous$$t.transform.tag == "Terrain") 
     {
         modifyObjGUIscript.activateMoveDisplay(initPos, $$anonymous$$t.point);        
         
 //        var meshHalfHeight = modObj.GetComponent.<MeshRenderer>().bounds.size.y /2; //helps account for large and small objects
         
         modObj.transform.position = $$anonymous$$t.point; //***method 01***
 //        modObj.transform.position = Vector3.Lerp(initPos, $$anonymous$$t.point, speed); //***method 02***
 //        modObj.transform.position = Vector3.SmoothDamp(initPos, $$anonymous$$t.point, velocity, smoothTime); //***method 02***
 
 //        modObj.transform.position.y =  modObj.transform.position.y + meshHalfHeight + hoverHeight;
         
         modObj.transform.rotation = initRotation;
     }
 }
Comment

People who like this

0 Show 4
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 e4lime · May 19, 2016 at 11:06 AM 0
Share

have you tried to move the gameobject via the rigidbody component? You shouldn't move a gameobject via its transform if you have a rigidbody attached to it

avatar image RyanAchtenSoma e4lime · May 20, 2016 at 06:09 AM 0
Share

Hi @Klockrent, I have made the changes you suggested to use rigidbody position (see code below); I think this made the movement smoother but it is still 'juddering' quite a bit - any further suggestions?

 //Move
 if ( Input.GetKey(KeyCode.E))
 {    
     if (Input.GetKeyDown(KeyCode.E))
     {
         modObj.GetComponent(BoxCollider).enabled = false;                
         initPos = modObj.transform.position;
         var initRotation = modObj.transform.rotation;    
     }
     
 //            modObj.GetComponent(Rigidbody).isKinematic = true;
     modObj.GetComponent(Rigidbody).useGravity = false;
     modObj.GetComponent(Rigidbody).freezeRotation = true; //***NEW*** 
 
     moveObject(modObj, initPos, initRotation);
 }
 if (Input.GetKeyUp(KeyCode.E)){
 //            Debug.Log("E Key is up");
     modObj.GetComponent(BoxCollider).enabled = true;
 //            modObj.GetComponent(Rigidbody).isKinematic = false;
     modObj.GetComponent(Rigidbody).useGravity = true; 
 }
 
 function moveObject(modObj : GameObject, initPos : Vector3, initRotation : Quaternion)
 {
     //Debug.Log("Moving Object");
 
     var hit : RaycastHit;
     var foundHit : boolean = false;
     
     foundHit = Physics.Raycast(transform.position, transform.forward, hit);
     //Debug.DrawRay(transform.position, transform.forward, Color.blue);
     
     if(foundHit && hit.transform.tag == "Terrain") 
     {
         modifyObjGUIscript.activateMoveDisplay(initPos, hit.point);        
         
         var meshHalfHeight = modObj.GetComponent.<MeshRenderer>().bounds.size.y /2; //helps account for large and small objects
         
 //        modObj.transform.position = hit.point; //***method 01***
 //        modObj.transform.position = Vector3.Lerp(initPos, hit.point, speed); //***method 02***
 //        modObj.transform.position = Vector3.SmoothDamp(initPos, hit.point, velocity, smoothTime); //***method 03***
         
 //        modObj.transform.position.y =  modObj.transform.position.y + meshHalfHeight + hoverHeight;
 //        modObj.transform.rotation = initRotation;
         
         var movRb = modObj.GetComponent.<Rigidbody>(); //***NEW - method 04***
         movRb.position = Vector3.Lerp(initPos, hit.point, speed); 
 //        movRb.position.y =  modObj.transform.position.y + meshHalfHeight + hoverHeight;
 //        movRb.rotation = initRotation;
     }
 }
avatar image e4lime RyanAchtenSoma · May 21, 2016 at 07:56 AM 0
Share

Try setting interpolate to interpolate or extrapolate on the rigidbody. And make sure you are moving the rigidbody from FixedUpdate and not Update.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by valaskasz · May 19, 2016 at 11:59 AM

use Vector3.Lerp to interpolate between two positions. ex: modObj.transform.position =Vector3.Lerp(modObj.transform.position ,$$anonymous$$t.point,Time.deltatime*smootValue)

Comment
tanoshimi

People who like this

-1 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 RyanAchtenSoma · May 20, 2016 at 04:06 AM 0
Share

Hi @ valaskasz, thanks for your response; have tried this in the code above modObj.transform.position = Vector3.Lerp(initPos, hit.point, speed); //***method 02*** however the 'juddering' issue persists.

Any other ideas?

avatar image

Answer by MuratKul · May 20, 2016 at 11:49 AM

Did you tried to decrease fixed timestep? (Project Settings>>Time)

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

66 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

Related Questions

How to limit a ray to go downwards to infinity, how can I make it down only 1 float. 0 Answers

Raycast doesn't register Rigidbody 1 Answer

Raycast Problem. CharacterJoint/Rigibodies 1 Answer

Extend the Collider When Picking Up Objects 0 Answers

Trouble with RayCast, rigid body and the colliders tag 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