-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChangeBasedOnDistance.cs
59 lines (50 loc) · 2.76 KB
/
ChangeBasedOnDistance.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
using UnityEngine.XR.iOS;
/* The prefab in the scene changes size, color and rotation based on the distance between it and the camera. */
public class ChangeBasedOnDistance : MonoBehaviour {
public GameObject prefabToChange;
private Transform mainCamera;
private Quaternion originalRotation;
private Quaternion maxRotation;
// Start is called before the first frame update
void Start() {
mainCamera = Camera.main.gameObject.transform;
originalRotation = prefabToChange.transform.rotation; //we save the original rotation to interpolate rotation correctly
maxRotation = Quaternion.identity; //make new rotation object
maxRotation.eulerAngles = new Vector3(180f, 0f, 0f); //set new rotation object's rotation to 180
}
// Update is called once per frame
void Update() {
// Get distance between main camera and prefab
float dist = Vector3.Distance(mainCamera.position, prefabToChange.transform.position);
//Debug.Log("This is the distance : " + dist);
// Below is code to change the color of the cube from red to blue
float newRedValue = map(dist, 0.5f, 0.1f, 0f, 1f); //note that the "start" of the original values is larger than the "stop" - this inverts the lerping
float newBlueValue = map(dist, 0.1f, 0.5f, 0f, 1f);
prefabToChange.GetComponent<Renderer>().material.color = new Color(newRedValue, 0f, newBlueValue);
//// Below is code to change the rotation of the cube based on distance;
//// it's commented out so the demo scene will be more clear, but try uncommenting it!
// float newRotation = map(dist, 0.5f, 0.1f, 0f, 1f);
// Quaternion newQuat = Quaternion.Slerp(originalRotation, maxRotation, newRotation);
// prefabToChange.transform.rotation = newQuat;
// Below is the code to change the size of the cube
float newScale = map(dist, 0.5f, 0.05f, 0.05f, 0.5f);
prefabToChange.transform.localScale = new Vector3(prefabToChange.transform.localScale.x, newScale, prefabToChange.transform.localScale.z);
}
// Simple value mapping function
private float map(float value, float start1, float stop1, float start2, float stop2) {
// Map the value
float res = start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1));
// Below we clamp the return value to be within the new start and stop
// start2 isn't necessarily less than stop2
float new_min = Mathf.Min(start2, stop2);
float new_max = Mathf.Max(start2, stop2);
res = Mathf.Max(new_min, res);
res = Mathf.Min(new_max, res);
return res;
}
}