Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class CameraMultiTarget : MonoBehaviour
- {
- public GameObject TargetA;
- public GameObject TargetB;
- GameObject ActiveTarget;
- Vector3 StartOffset = Vector3.zero;
- void Start()
- {
- //Make Target A the first target
- ActiveTarget = TargetA;
- //When we start we grab the distance to the target as a vector
- StartOffset = this.transform.position - ActiveTarget.transform.position;
- }
- void Update()
- {
- //When we press a button it changes the target
- if (Input.GetButtonUp("Fire2")))
- {
- //If target is A switch to B and return out of code, just switch to A if already B
- if (ActiveTarget == TargetA)
- {
- ActiveTarget = TargetB;
- return;
- }
- ActiveTarget = TargetA;
- }
- }
- private void FixedUpdate()
- {
- //We make the camera follow the object during the physics step
- this.transform.position = ActiveTarget.transform.position + StartOffset;
- //Then we make the camera look at the target
- this.transform.LookAt(ActiveTarget.transform);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement