• 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 danksky · Jul 06, 2017 at 12:44 AM · vrsteammanipulation

Grab and Throw Script changes velocity without reason

When I release the selected GameObject, the object flies off in a completely different direction. The log seems to suggest that the velocity value itself is completely different from what it was released at.

Here's my Grab.cs: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Grab : MonoBehaviour {
 
     private GameObject releasedObj;
     private GameObject selectedObj;
     private GameObject grabbableEnemy;
     public Transform gripTransform;
     private SteamVR_TrackedObject trackedObject;
     private SteamVR_Controller.Device _controllerDevice;
     private SteamVR_TrackedController _controller;
 
     private Vector3[] positions = new Vector3[2]; // positions[0] last frame, positions[1] this frame;
 
     private bool gripGripped;
 
     void Awake()
     {
         trackedObject = GetComponent<SteamVR_TrackedObject>();
     }
 
     // Use this for initialization
     void Start () {
         _controllerDevice = SteamVR_Controller.Input((int)trackedObject.index);
         _controller = GetComponent<SteamVR_TrackedController>();
         _controller.Gripped += HandleGripGripped;
         _controller.Ungripped += HandleGripUngripped;
         positions[0] = Vector3.zero;
         positions[1] = Vector3.zero;
 
         releasedObj = null;
         selectedObj = null;
         grabbableEnemy = null;
         gripGripped = false;
     }
 
     // Update is called once per frame
     void FixedUpdate () {
         if (selectedObj)
         {
             Debug.Log("Updating velocity");
             positions[0] = positions[1];
             positions[1] = selectedObj.transform.position;
         }
        
         if (releasedObj)
         {
             Debug.Log("Released V: " + releasedObj.GetComponent<Rigidbody>().velocity);
         }
         if (_controllerDevice.GetPressDown(SteamVR_Controller.ButtonMask.Grip))
         {
             Debug.Log("Gripped");
             if (grabbableEnemy)
             {
                 Debug.Log("Grab sequence begins");
                 selectedObj = grabbableEnemy;
                 Debug.Log(selectedObj.name);
                 selectedObj.transform.position = gripTransform.position;
                 var joint = gameObject.AddComponent<FixedJoint>();
                 joint.connectedBody = selectedObj.GetComponent<Rigidbody>();
             }
         }
         if (_controllerDevice.GetPressUp(SteamVR_Controller.ButtonMask.Grip))
         {
             Debug.Log("Ungripped");
             if (gameObject.GetComponent<FixedJoint>() && selectedObj)
             {
                 Debug.Log("Launch sequence begins");
                 Vector3 velocity = (positions[1] - positions[0]) / Time.deltaTime;
                 foreach (FixedJoint joint in gameObject.GetComponents<FixedJoint>()) {
                     Destroy(joint);
                     //Debug.Log("Fixed Joint value: " + joint + " & Component value: " + gameObject.GetComponent<FixedJoint>());
                 }
                 releasedObj = GameObject.Instantiate(selectedObj);
                 releasedObj.GetComponent<Rigidbody>().velocity = velocity;
                 Debug.Log("Released @ release: " + releasedObj.GetComponent<Rigidbody>().velocity);
 
                 // For garbage collection?
                 Destroy(selectedObj);      
             }
         }
     }
 
     private void LateUpdate()
     {
         //if (!_controllerDevice.GetPress(SteamVR_Controller.ButtonMask.Grip))
         //    selectedObj = null;
     }
 
     private void HandleGripGripped(object sender, ClickedEventArgs e)
     {
         gripGripped = true;
        
     }
 
     private void HandleGripUngripped(object sender, ClickedEventArgs e)
     {
         gripGripped = false;
     }
 
     private void OnTriggerStay(Collider other)
     {
         Debug.Log("Triggered");
         if (other.gameObject.tag == "Enemy")
         {
             grabbableEnemy = other.gameObject;
         }
     }
 
     private void OnTriggerExit(Collider other)
     {
         grabbableEnemy = null;
     }
 }
 

Here's my Log:

 Triggered
 UnityEngine.Debug:Log(Object)
 Grab:OnTriggerStay(Collider) (at Assets/Grab.cs:106)
 Gripped
 UnityEngine.Debug:Log(Object)
 Grab:FixedUpdate() (at Assets/Grab.cs:54)
 Grab sequence begins
 UnityEngine.Debug:Log(Object)
 Grab:FixedUpdate() (at Assets/Grab.cs:57)
 Sphere (The name of grabbed)
 UnityEngine.Debug:Log(Object)
 Grab:FixedUpdate() (at Assets/Grab.cs:59)
 Updating velocity
 UnityEngine.Debug:Log(Object)
 Grab:FixedUpdate() (at Assets/Grab.cs:43)
 Ungripped
 UnityEngine.Debug:Log(Object)
 Grab:FixedUpdate() (at Assets/Grab.cs:67)
 Launch sequence begins
 UnityEngine.Debug:Log(Object)
 Grab:FixedUpdate() (at Assets/Grab.cs:70)
 (-3.1, 4.0, -1.6)
 UnityEngine.Debug:Log(Object)
 Grab:FixedUpdate() (at Assets/Grab.cs:76)
 Released @ release: (-3.1, 4.0, -1.6)
 UnityEngine.Debug:Log(Object)
 Grab:FixedUpdate() (at Assets/Grab.cs:79)
 ----------------- H E R E ---------------------------
 Released V: (11.9, 3.7, -1.7)
 UnityEngine.Debug:Log(Object)
 Grab:FixedUpdate() (at Assets/Grab.cs:50)
 Released V: (11.9, 3.5, -1.7)
 UnityEngine.Debug:Log(Object)
 Grab:FixedUpdate() (at Assets/Grab.cs:50)
 Released V: (11.9, 3.4, -1.7)
 UnityEngine.Debug:Log(Object)
 Grab:FixedUpdate() (at Assets/Grab.cs:50)
 Released V: (11.9, 3.3, -1.7)
 UnityEngine.Debug:Log(Object)
 Grab:FixedUpdate() (at Assets/Grab.cs:50)
 ...etc
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

0 Replies

· Add your reply
  • Sort: 

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

129 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

Related Questions

Drag UI Slider in VR with Vive Controllers 1 Answer

Steam VR one object interaction using both controllers 0 Answers

Steam VR Hands stop colliding after Throwable is picked up 0 Answers

How to use SteamVR longbow and arrow? 1 Answer

How to get GameObject of steamvr controller components? 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