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 /
  • Help Room /
avatar image
0
Question by Tameranian · May 14 at 06:26 AM · rigidbodyvelocitypick up object

How to preserve velocity when dropping object

I have a pickup script to pick up, throw, and drop rigid bodies, the problem I'm having is that when dropping an object while moving it quickly it doesn't preserve its velocity and just drops to the floor, I am unsure about how to achieve what I'm looking for and clear help would be appreciated.

Here's the pick up script in question.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PickUp : MonoBehaviour
 {
     public float pickUpRange = 5;
     public Transform holdParent;
     private GameObject heldObject;
     public float moveForce = 250f;
     public float Thrust = 2f;
 
     public Transform target;
 
     // Update is called once per frame
 
 
     void Update()
     {
 
         if (Input.GetKeyDown(KeyCode.E))
         {
             if (heldObject == null)
             {
                 RaycastHit hit;
                 if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), out hit, pickUpRange))
                 {
                     PickUpObject(hit.transform.gameObject);
                 }
             }
             else
             {
                 DropObject();
             }
         }
 
         if (heldObject != null)
         {
             MoveObject();
 
         }
 
         if (Input.GetMouseButtonDown(0))
         {
             ThrowObject();
         }
     }
 
     void MoveObject()
     {
         if (Vector3.Distance(heldObject.transform.position, holdParent.position) > 0.1f)
         {
             Vector3 moveDirection = (holdParent.position - heldObject.transform.position);
             heldObject.GetComponent<Rigidbody>().AddForce(moveDirection * moveForce);
         }
     }
 
     void PickUpObject(GameObject pickObject)
     {
         if (pickObject.GetComponent<Rigidbody>())
         {
             Rigidbody Rigid = pickObject.GetComponent<Rigidbody>();
 
             //Rigid.constraints = RigidbodyConstraints.FreezeRotation;
             Rigidbody objectRig = pickObject.GetComponent<Rigidbody>();
             objectRig.useGravity = false;
             objectRig.drag = 10;
 
             objectRig.transform.parent = holdParent;
             heldObject = pickObject;
         }
     }
 
     void DropObject()
     {
         Rigidbody Rigid = heldObject.GetComponent<Rigidbody>();
         Rigid.constraints = RigidbodyConstraints.None;
         Rigid.freezeRotation = false;
         Rigidbody heldRig = heldObject.GetComponent<Rigidbody>();
         heldRig.useGravity = true;
         heldRig.drag = 1f;
 
         heldObject.transform.parent = null;
         heldObject = null;
 
 
     }
 
     void ThrowObject()
     {
         Rigidbody Rigid = heldObject.GetComponent<Rigidbody>();
         Rigid.constraints = RigidbodyConstraints.None;
         Rigid.AddForce(transform.forward * Thrust);
         Rigid.freezeRotation = false;
         Rigidbody heldRig = heldObject.GetComponent<Rigidbody>();
         heldRig.useGravity = true;
         heldRig.drag = 1f;
 
         heldObject.transform.parent = null;
         heldObject = null;
 
 
 
     }
 }

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
0

Answer by alexanderlarsen · May 23 at 07:30 AM

If your parent object has a Rigidbody, I think you can just set the held object's Rigidbody velocity to the parent object's Rigidbody velocity on drop (or calculate the parent's velocity manually and apply it to the held object).

Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

237 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 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 set velocity from connected Rigidbody? 0 Answers

How to have 2 objects share the same x (position) value, player can push any of them and another will move too? 1 Answer

Rotate GameObject based on RigidBody Velocity 1 Answer

Torque applies to one wheel, but inconsistently to the other wheel 0 Answers

Make a "Balloon" that floats upwards but slows as it gets higher 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