Created
April 16, 2020 01:49
-
-
Save planaria/b91f9b8cc04a8fbb15e6054ba98e6852 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UdonSharp; | |
using UnityEngine; | |
using UnityEngine.UI; | |
using VRC.SDKBase; | |
using VRC.Udon; | |
public class TakeMyHand : UdonSharpBehaviour | |
{ | |
VRCPlayerApi[] players = new VRCPlayerApi[0]; | |
VRCPlayerApi connectedPlayerRH = null; | |
VRCPlayerApi connectedPlayerLH = null; | |
void Start() | |
{ | |
// Networking.LocalPlayer.SetJumpImpulse(8.0f); | |
} | |
float Max(float x1, float x2) | |
{ | |
return x1 < x2 ? x2 : x1; | |
} | |
void Update() | |
{ | |
var localPlayer = Networking.LocalPlayer; | |
var handRH = Input.GetAxis("Oculus_CrossPlatform_SecondaryHandTrigger"); | |
var indexRH = Input.GetAxis("Oculus_CrossPlatform_SecondaryIndexTrigger"); | |
var inputRH = Max(handRH, indexRH); | |
var handLH = Input.GetAxis("Oculus_CrossPlatform_PrimaryHandTrigger"); | |
var indexLH = Input.GetAxis("Oculus_CrossPlatform_PrimaryIndexTrigger"); | |
var inputLH = Max(handLH, indexLH); | |
if (inputRH == 0.0f && connectedPlayerRH != null) | |
{ | |
connectedPlayerRH = null; | |
localPlayer.SetVelocity(new Vector3()); | |
} | |
else if (inputRH == 1.0f && connectedPlayerRH == null) | |
{ | |
var pRH = localPlayer.GetBonePosition(UnityEngine.HumanBodyBones.RightHand); | |
VRCPlayerApi nearestPlayer = null; | |
double nearestDistance = 0.0; | |
for (int i = 0; i < players.Length; ++i) | |
{ | |
var player = players[i]; | |
if (player.isLocal) | |
{ | |
continue; | |
} | |
var pLH = player.GetBonePosition(UnityEngine.HumanBodyBones.LeftHand); | |
var distance = Vector3.Distance(pRH, pLH); | |
if (nearestPlayer == null || distance < nearestDistance) | |
{ | |
nearestPlayer = player; | |
nearestDistance = distance; | |
} | |
} | |
if (nearestDistance < 0.3) | |
{ | |
connectedPlayerRH = nearestPlayer; | |
} | |
} | |
if (inputLH == 0.0f && connectedPlayerLH != null) | |
{ | |
connectedPlayerLH = null; | |
localPlayer.SetVelocity(new Vector3()); | |
} | |
else if (inputLH == 1.0f && connectedPlayerLH == null) | |
{ | |
var pLH = localPlayer.GetBonePosition(UnityEngine.HumanBodyBones.LeftHand); | |
VRCPlayerApi nearestPlayer = null; | |
double nearestDistance = 0.0; | |
for (int i = 0; i < players.Length; ++i) | |
{ | |
var player = players[i]; | |
if (player.isLocal) | |
{ | |
continue; | |
} | |
var pRH = player.GetBonePosition(UnityEngine.HumanBodyBones.RightHand); | |
var distance = Vector3.Distance(pRH, pLH); | |
if (nearestPlayer == null || distance < nearestDistance) | |
{ | |
nearestPlayer = player; | |
nearestDistance = distance; | |
} | |
} | |
if (nearestDistance < 0.3) | |
{ | |
connectedPlayerLH = nearestPlayer; | |
} | |
} | |
if (connectedPlayerRH != null) | |
{ | |
var p0 = localPlayer.GetBonePosition(UnityEngine.HumanBodyBones.RightHand); | |
var p1 = connectedPlayerRH.GetBonePosition(UnityEngine.HumanBodyBones.LeftHand); | |
var v1 = connectedPlayerRH.GetVelocity(); | |
localPlayer.SetVelocity(Vector3.ClampMagnitude(p1 - p0, 0.3f) * 50.0f + v1); | |
} | |
else if (connectedPlayerLH != null) | |
{ | |
var p0 = localPlayer.GetBonePosition(UnityEngine.HumanBodyBones.LeftHand); | |
var p1 = connectedPlayerLH.GetBonePosition(UnityEngine.HumanBodyBones.RightHand); | |
var v1 = connectedPlayerLH.GetVelocity(); | |
localPlayer.SetVelocity(Vector3.ClampMagnitude(p1 - p0, 0.3f) * 50.0f + v1); | |
} | |
} | |
public override void OnPlayerJoined(VRCPlayerApi player) | |
{ | |
var newPlayers = new VRCPlayerApi[players.Length + 1]; | |
players.CopyTo(newPlayers, 0); | |
newPlayers[players.Length] = player; | |
players = newPlayers; | |
} | |
public override void OnPlayerLeft(VRCPlayerApi player) | |
{ | |
VRCPlayerApi[] newPlayers = new VRCPlayerApi[players.Length - 1]; | |
int src, dst; | |
for (src = 0, dst = 0; src < players.Length; ) | |
{ | |
if (players[src].playerId != player.playerId) | |
{ | |
newPlayers[dst] = players[src]; | |
++dst; | |
} | |
++src; | |
} | |
players = newPlayers; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment