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
Your answer
Follow this Question
Related Questions
HDRP with VR: Double Image Issue 0 Answers
Steam VR Snap Turn while holding Throwable object not working 2 Answers
SteamVR_LaserPointer Color changes from Unity Editor to Game Build 1 Answer
Steam VR longbow Arrow wont fire 0 Answers
Steam VR controllers becoming hidden after steam button pressed 1 Answer