Last active
March 23, 2020 21:30
-
-
Save cabbibo/59bf1532af2a0a0513f68850e0138e51 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
// This class lives on the avatar root | |
public class Blarpatar : MonoBehaviour{ | |
private RealtimeView _realtimeView; | |
public Human human; | |
public PullTowardsHand blarpL; | |
public PullTowardsHand blarpR; | |
public GameObject blarpPrefab; | |
void Start() { | |
_realtimeView = GetComponent<RealtimeView>(); | |
if (_realtimeView.isOwnedLocally) { | |
// Local player avatar, let's create a blarp! | |
GameObject playerBlarpGameObject = Realtime.Instantiate(blarpPrefab.name, true, true, true); | |
blarpL = playerBlarpGameObject.GetComponent<PullTowardsHand>(); | |
blarpL.rightHand = false; | |
blarpL.human = human; | |
blarpL.initialize(); | |
playerBlarpGameObject = Realtime.Instantiate(blarpPrefab.name, true, true, true); | |
blarpR = playerBlarpGameObject.GetComponent<PullTowardsHand>(); | |
blarpR.rightHand = true; | |
blarpR.human = human; | |
blarpR.initialize(); | |
} | |
} | |
void Update() { | |
if (blarpL != null) { | |
blarpL.VerifyOwnedByLocalPlayer(); | |
blarpL.ApplyFoces(human.RightHand , human.); | |
} | |
if (blarpR != null) { | |
blarpR.VerifyOwnedByLocalPlayer(); | |
blarpR.ApplyFoces(); | |
} | |
} | |
} | |
// This lives on blarp prefab | |
public class PullTowardsHand : MonoBehaviour | |
{ | |
public Transform hand; | |
public Human human; | |
public bool rightHand; | |
public LineRenderer lr; | |
private Rigidbody rb; | |
public float force; | |
private RealtimeTransform _realtimeTransform; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
lr = GetComponent<LineRenderer>(); | |
rb = GetComponent<Rigidbody>(); | |
_realtimeTransform = GetComponent<RealtimeTransform>(); | |
} | |
public void initialize(){ | |
hand = human.LeftHand; | |
if( rightHand ){ | |
hand = human.RightHand; | |
} | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
lr.SetPosition( 0 , transform.position ); | |
lr.SetPosition( 1 , hand.position ); | |
} | |
public void VerifyOwnedByLocalPlayer() { | |
if (_realtimeTransform.isOwnedLocally) | |
return; // We're already owned locally, so we're good. | |
_realtimeTransform.RequestOwnership(); | |
} | |
public void ApplyFoces() { | |
float triggerVal = human.LeftTrigger; | |
if( rightHand ){ triggerVal = human.RightTrigger;} | |
rb.AddForce( -(transform.position - hand.position) * force * triggerVal ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment