- Home /
Question by
haimmoshe · Aug 27, 2017 at 04:54 PM ·
c#scripting problemscript.enum
How can i change the camera view when clicking a button ?
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);
}
}
Comment
Best Answer
Answer by Vollmondum · Aug 27, 2017 at 05:34 PM
case views = Views.Backward:
or Just replace entire switch section with number is elifs
I did
case views = Views.Backward:
Got error:
A constant value is expected
Well, that latest Unity switch statement is broken since like february. Just use ifs