Spelunx Cavern SDK
 
Loading...
Searching...
No Matches
FollowMotion.cs
Go to the documentation of this file.
1using System;
2using UnityEngine;
3
4namespace Spelunx.Vive
5{
6 /*
7 * This script copies the movement of the target with an optional offset (x, y, z)
8 * The default is no offset, moving at the same place as the target
9 */
10 public class FollowMotion : MonoBehaviour
11 {
12 [SerializeField, Tooltip("The target to follow. Usually a vive tracker.")] private Transform target = null; // the target to follow (usually vive tracker)
13 [SerializeField, Tooltip("The offset vector from the target position.")] private Vector3 offset = Vector3.zero;
14
15 private void Start()
16 {
17 SetTarget(this.target);
18 }
19
20 void Update()
21 {
22 if (target == null) return;
23 transform.position = target.position + offset;
24 }
25
26 public void SetTarget(Transform target, bool withOffset)
27 {
28 if (target == null)
29 {
30 Debug.Log(gameObject.name + ": FollowMotion has no target.");
31 return;
32 }
33 this.target = target;
34 offset = withOffset ? (transform.position - target.position) : Vector3.zero;
35 }
36
37 public void SetTarget(Transform target)
38 {
39 SetTarget(target, false);
40 }
41
42 public Transform GetTarget()
43 {
44 return target;
45 }
46 }
47}
void SetTarget(Transform target)
Definition: FollowMotion.cs:37
void SetTarget(Transform target, bool withOffset)
Definition: FollowMotion.cs:26