0
\$\begingroup\$

I added enum with all the views options. But when i click/press on V nothing happen with the game is running.

using UnityEngine;

public class FollowTargetCamera : MonoBehaviour
{
    public enum Views
    {
        Forward, Backward, Left, Right
    }

    public Transform Target;
    public float PositionFolowForce = 5f;
    public float RotationFolowForce = 5f;

    public Views views;

    void Start()
    {

    }

    void FixedUpdate()
    {
        Vector3 vector = new Vector3(0, 0, 0);
        Vector3 dir = new Vector3(0, 0, 0);

        if (Input.GetKeyDown(KeyCode.V))
        {
            switch (views)
            {
                case Views.Backward:
                    vector = Vector3.back;
                    dir = Target.rotation * Vector3.back;
                    break;
                case Views.Forward:
                    vector = Vector3.forward;
                    dir = Target.rotation * Vector3.forward;
                    break;
                case Views.Left:
                    vector = Vector3.left;
                    dir = Target.rotation * Vector3.left;
                    break;
                case Views.Right:
                    vector = Vector3.right;
                    dir = Target.rotation * Vector3.right;
                    break;
            }
        }
        dir.y = 0f;
        if (dir.magnitude > 0f) vector = dir / dir.magnitude;

        transform.position = Vector3.Lerp(transform.position, Target.position, PositionFolowForce * Time.deltaTime);
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(vector), RotationFolowForce * Time.deltaTime);
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ You are not initialising views variable, the code will not fall into any cases of your switch statement. \$\endgroup\$
    – Tolga Şen
    Aug 27, 2017 at 11:42
  • \$\begingroup\$ A not-initialized enum equals 0, thar's not the problem \$\endgroup\$
    – cortvi
    Aug 28, 2017 at 2:17
  • \$\begingroup\$ Fixed Update is no the place you want to check Input. Try it in Update \$\endgroup\$
    – cortvi
    Aug 28, 2017 at 2:19

1 Answer 1

1
\$\begingroup\$

You don't change the value of variable views anywhere (in the given code), that's why it doesn't change.

It will be Views.Backward all the time, for example.

If you want it to be looping through all the views, you could try something like

if (views < 3 ) ++views
else views = 0

This can be achieved in more optimal ways, but check if that simply works.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .