Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
Fix pickup events (OnPickupUseDown and OnPickupUseUp)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyanLaser committed May 5, 2021
1 parent 885e0d2 commit d303896
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
47 changes: 31 additions & 16 deletions CyanEmu/Scripts/CyanEmuPickupHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@ namespace VRCPrefabs.CyanEmu
[AddComponentMenu("")]
public class CyanEmuPickupHelper : MonoBehaviour, ICyanEmuInteractable
{
// If the user releases the mouse button before this time, it will not fire on use up.
private const float INITIAL_PICKUP_DURATION_ = 0.5f;
private const float MAX_PICKUP_DISTANCE_ = 0.25f;
private static Quaternion GRIP_OFFSET_ROTATION_ = Quaternion.Euler(0, 0, -90);
private static Quaternion GUN_OFFSET_ROTATION_ = Quaternion.Euler(-90, 0, -90);

[HideInInspector]
public Rigidbody rigidbody;

private Rigidbody rigidbody_;
private VRC_Pickup pickup_;

private bool isHeld_;

private Vector3 positionOffset_;
private Quaternion rotationOffset_ = Quaternion.identity;

private bool initialGrab_;
private float grabActionStartTime_;
private float dropActionStartTime_;

public static void InitializePickup(VRC_Pickup pickup)
Expand Down Expand Up @@ -64,9 +66,14 @@ public static VRC_Pickup.PickupHand GetPickupHand(VRC_Pickup pickup)
private void SetPickup(VRC_Pickup pickup)
{
pickup_ = pickup;
rigidbody = GetComponent<Rigidbody>();
rigidbody_ = GetComponent<Rigidbody>();
}

public Rigidbody GetRigidbody()
{
return rigidbody_;
}

public float GetProximity()
{
return pickup_.proximity;
Expand Down Expand Up @@ -94,7 +101,7 @@ public void Interact()

public void UpdatePosition(Transform root, bool force = false)
{
if (rigidbody.isKinematic || force)
if (rigidbody_.isKinematic || force)
{
transform.position = root.transform.position + root.TransformDirection(positionOffset_);
transform.rotation = root.transform.rotation * rotationOffset_;
Expand All @@ -111,16 +118,21 @@ public void UpdateUse()
{
dropActionStartTime_ = Time.time;
}

if (Input.GetMouseButtonDown(0))
{
this.Log("Pickup Use Down");
gameObject.OnPickupUseDown();
}
if (Input.GetMouseButtonUp(0))

float grabDuration = Time.time - grabActionStartTime_;
if (grabDuration > INITIAL_PICKUP_DURATION_)
{
this.Log("Pickup Use Up");
gameObject.OnPickupUseUp();
if (Input.GetMouseButtonDown(0) || (initialGrab_ && Input.GetMouseButton(0)))
{
this.Log("Pickup Use Down");
gameObject.OnPickupUseDown();
initialGrab_ = false;
}
if (Input.GetMouseButtonUp(0))
{
this.Log("Pickup Use Up");
gameObject.OnPickupUseUp();
}
}
}

Expand All @@ -138,6 +150,8 @@ public void Pickup()
}

isHeld_ = true;
grabActionStartTime_ = Time.time;
initialGrab_ = true;

gameObject.OnPickup();

Expand Down Expand Up @@ -200,6 +214,7 @@ public void Drop()

this.Log("Dropping object " + name);
isHeld_ = false;
initialGrab_ = false;

gameObject.OnDrop();

Expand All @@ -212,14 +227,14 @@ public void Drop()
player.DropObject(this);

// Calculate throw velocity
if (!rigidbody.isKinematic)
if (!rigidbody_.isKinematic)
{
float holdDuration = Mathf.Clamp(Time.time - dropActionStartTime_, 0, 3);
if (holdDuration > 0.2f)
{
Transform rightArm = player.GetArmTransform();
Vector3 throwForce = rightArm.forward * (holdDuration * 500 * pickup_.ThrowVelocityBoostScale);
rigidbody.AddForce(throwForce);
rigidbody_.AddForce(throwForce);
Debug.Log("Adding throw force: "+ throwForce);
}
}
Expand Down
18 changes: 7 additions & 11 deletions CyanEmu/Scripts/CyanEmuPlayerController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ public void PickupObject(CyanEmuPickupHelper pickup)

pickup.UpdatePosition(rightArmPosition_.transform, true);
FixedJoint fixedJoint = rightArmPosition_.AddComponent<FixedJoint>();
fixedJoint.connectedBody = pickup.rigidbody;
fixedJoint.connectedBody = pickup.GetRigidbody();
}

public void DropObject(CyanEmuPickupHelper pickup)
Expand All @@ -538,8 +538,8 @@ public void DropObject(CyanEmuPickupHelper pickup)
DestroyImmediate(fixedJoint);
}

Rigidbody rigidbody = pickup.rigidbody;
rigidbody.velocity = (rightArmPosition_.transform.position - prevousHandPosition_) * (0.3f / Time.fixedDeltaTime);
Rigidbody rigidbody = pickup.GetRigidbody();
rigidbody.velocity = (rightArmPosition_.transform.position - prevousHandPosition_) * (0.5f / Time.deltaTime);
rigidbody.angularVelocity = (rightArmPosition_.transform.rotation.eulerAngles - prevousHandRotation_);
}
}
Expand Down Expand Up @@ -613,11 +613,15 @@ private void Update()
if (currentPickup_ != null)
{
currentPickup_.UpdatePosition(rightArmPosition_.transform);
currentPickup_.UpdateUse();
}

UpdateStance();
UpdateMenu();
UpdateCameraProxyPosition();

prevousHandPosition_ = rightArmPosition_.transform.position;
prevousHandRotation_ = rightArmPosition_.transform.rotation.eulerAngles;
}

private void FixedUpdate()
Expand All @@ -631,14 +635,6 @@ private void FixedUpdate()
#if UDON
HandleUdonInput();
#endif

if (currentPickup_ != null)
{
currentPickup_.UpdateUse();
}

prevousHandPosition_ = rightArmPosition_.transform.position;
prevousHandRotation_ = rightArmPosition_.transform.rotation.eulerAngles;

if (currentStation_ != null && !currentStation_.CanPlayerMoveWhileSeated(input.magnitude))
{
Expand Down

0 comments on commit d303896

Please sign in to comment.