Problem with changing target of Camera.

Hi, I'm having a problem with "toggling" between targets of my camera. The camera is meant to follow a GameObject, and when certain conditions are filled (not completely ready yet on that part, still needs length and direction if-statements) it should go from my 3rd Person Controller to a car, and also hide the character. The problem is, that it won't toggle between targets, or it will toggle one time (if i choose the car in the beginning as target, i can toggle to the 3ps controller, however if i choose 3ps controller in the beginning, it won't toggle at all.

Example: Target: catamount, i start the game, i drive around, i press "e", camera starts following the 3ps controller and Debug.Logs "E-key pressed while in car". Example2: Target: 3ps controller, i start the game, i run around, i press "e", camera won't change and Debug.Logs "E-key pressed while in car".

Here's the code, it's C#:

using UnityEngine;
using System.Collections;

public class HackNSlashCamera : MonoBehaviour {
    public Transform target;
    public float walkDistance;
    public float runDistance;
    public float height;    
    public float xSpeed = 250.0f;
    public float ySpeed = 120.0f;
    public float heightDamping = 2.0f;
    public float rotationDamping = 3.0f;
    public static bool goCar = false;

    private Transform _myTransform;
    private float x;
    private float y;
    private int pos;
    private GameObject nextTarget;

    void Awake() {
        _myTransform = transform;
    }

    // Use this for initialization
    void Start () {
        if(target == null)
            Debug.LogWarning("We do not have a target");
        else {
            CameraSetup();  
        }
    }
    void Update() {
        if (goCar == false) {
            if (Input.GetKeyDown (KeyCode.E)) {
                Debug.Log("E-key pressed");
                //var pos = target.transform.position;
                //pos.x = 1000;
                //target.transform.position = pos;
                nextTarget = GameObject.Find("catamount");
                target = nextTarget.transform;
                goCar = true;
            }
        }
        if (goCar == true) {
            if (Input.GetKeyDown (KeyCode.E)) {
                Debug.Log("E-key pressed while in car");
                nextTarget = GameObject.Find("Character");
                target = nextTarget.transform;
                goCar = false;
            }
        }
    }

    void LateUpdate() {
        if(Input.GetMouseButton(1) && target != null && goCar == false) {
            Debug.Log("Right Button is down");

            x += Input.GetAxis("Mouse X") * xSpeed * 0.02f;
            y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02f;

            Quaternion rotation = Quaternion.Euler(y, x, 0);
            Vector3 position = rotation * new Vector3(0.0f, 0.0f, -walkDistance) + target.position;

            transform.rotation = rotation;
            transform.position = position;
        }
        else {
            x = 0;
            y = 0;

            // Calculate the current rotation angles
            float wantedRotationAngle = target.eulerAngles.y;
            float wantedHeight = target.position.y + height;

            float currentRotationAngle = _myTransform.eulerAngles.y;
            float currentHeight = _myTransform.position.y;

            // Damp the rotation around the y-axis
           currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

            // Damp the height
            currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);

            // Convert the angle into a rotation
            Quaternion currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);

            // Set the position of the camera on the x-z plane to:
            // distance meters behind the target
            _myTransform.position = target.position;
            _myTransform.position -= currentRotation * Vector3.forward * walkDistance;

            // Set the height of the camera
            _myTransform.position = new Vector3(_myTransform.position.x, currentHeight, _myTransform.position.z);

            // Always look at the target
            _myTransform.LookAt (target);
        }
    }

    public void CameraSetup() {
        _myTransform.position = new Vector3(target.position.x, target.position.y + height, target.position.z - walkDistance);
        _myTransform.LookAt(target);
    }       
}

Help would be really appreciated (the main problem with the camera in first place, how to hide the character instead of sending it to x-positon 1000 is not necessary)

1 Answer

1

If-else problem in your Update. Change the second `if(goCar==true)` to just `else`.

Both ifs are triggering, so the 2nd always snaps it back to the player.

I was just telling my guys, when a robot riding an N-dimensional cyborg chicken makes a PB&J, it still needs to watch the peanut butter doesn't tear the bread. Past all the LookAt's and transform.up's, it's all just regular programming.