Created
May 17, 2024 04:23
-
-
Save Nova840/32866f7ce17341a9a47bd0480ab7f418 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.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
public class Follow : MonoBehaviour { | |
[SerializeField] | |
private Transform follow = null; | |
private Vector3 originalLocalPosition; | |
private Quaternion originalLocalRotation; | |
private void Awake() { | |
originalLocalPosition = follow.localPosition; | |
originalLocalRotation = follow.localRotation; | |
} | |
private void Update() { | |
if (Input.GetKeyDown(KeyCode.Keypad1) || Input.GetKeyDown(KeyCode.Keypad0)) { | |
//move the parent to child's position | |
transform.position = follow.position; | |
} | |
if (Input.GetKeyDown(KeyCode.Keypad2) || Input.GetKeyDown(KeyCode.Keypad0)) { | |
//HAS TO BE IN THIS ORDER | |
//sort of "reverses" the quaternion so that the local rotation is 0 if it is equal to the original local rotation | |
follow.RotateAround(follow.position, follow.forward, -originalLocalRotation.eulerAngles.z); | |
follow.RotateAround(follow.position, follow.right, -originalLocalRotation.eulerAngles.x); | |
follow.RotateAround(follow.position, follow.up, -originalLocalRotation.eulerAngles.y); | |
} | |
if (Input.GetKeyDown(KeyCode.Keypad3) || Input.GetKeyDown(KeyCode.Keypad0)) { | |
//rotate the parent | |
transform.rotation = follow.rotation; | |
} | |
if (Input.GetKeyDown(KeyCode.Keypad4) || Input.GetKeyDown(KeyCode.Keypad0)) { | |
//moves the parent by the child's original offset from the parent | |
transform.position += -transform.right * originalLocalPosition.x; | |
transform.position += -transform.up * originalLocalPosition.y; | |
transform.position += -transform.forward * originalLocalPosition.z; | |
} | |
if (Input.GetKeyDown(KeyCode.Keypad5) || Input.GetKeyDown(KeyCode.Keypad0)) { | |
//resets local rotation, undoing step 2 | |
follow.localRotation = originalLocalRotation; | |
} | |
if (Input.GetKeyDown(KeyCode.Keypad6) || Input.GetKeyDown(KeyCode.Keypad0)) { | |
//reset local position | |
follow.localPosition = originalLocalPosition; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment