The code “have” problem only when i click in some parts of the code and when i click away the errors messages disappears but i can’t upload the file on unity because it says that there are errors.
Is the the code or visual studio going crazy?
here is the entire code
ExecuteInEditMode]
public class CameraRig : MonoBehaviour
{
public Transform target;
public bool autoTargetPlayer;
public LayerMask wallayers;
public enum Shoulder
{
Right, Left
}
public Shoulder shoulder;
[System.Serializable]
public class CameraSettings
{
[Header("Positioning")]
public Vector3 camPositionOffsetLeft;
public Vector3 camPostionOffsetRight;
[Header("Camera Options")]
public float mouseXSensitivity = 5.0f;
public float mouseYSensitivity = 5.0f;
public float minAngle = 30.0f;
public float maxAngle = 70.0f;
public float rotationSpeed = 5.0f;
[Header("Zoom")]
public float fieldOfView = 70.0f;
public float zoomFieldOfView = 30.0f;
public float zoomSpeed = 3.0f;
[Header("Visual Options")]
public float hidMeshWhenDistance = 0.5f;
}
[SerializeField]
public CameraSettings cameraSettings;
[System.Serializable]
public class InputSettings
{
public string verticalAxis = "Vertical";
public string horizontalAxis = "Horizontal";
public string aimButton = "Fire2";
public string switchShoulderButton = "Fire4";
}
[SerializeField]
public InputSettings input;
[System.Serializable]
public class MovementSettings
{
public float movementLerpSpeed = 5.0f;
}
[SerializeField]
public MovementSettings movement;
Transform pivot;
Camera mainCamera;
float newX = 0.0f;
float newY = 0.0f;
// Use this for initialization
void Start()
{
mainCamera = Camera.main;
pivot = transform.GetChild(0);
}
// Update is called once per frame
void Update()
{
if (target)
{
if (Application.isPlaying)
{
RotateCamera();
CheckWall();
CheckMeshRenderer();
Zoom(Input.GetButton(input.aimButton));
if (Input.GetButtonDown(input.switchShoulderButton))
{
SwitchShoulder();
}
}
}
}
void LateUpdate()
{
if(!target)
{
TargetPlayer();
}
else
{
Vector3 targetPosition = target.position;
Quaternion targetRotation = target.rotation;
FollowTarget(targetPosition, targetRotation);
}
}
//Finds the player gameobject and set it has target
void TargetPlayer()
{
if (autoTargetPlayer)
{
GameObject player = GameObject.FindGameObjectsWithTag("Player");
if (player)
{
Transform playerT = player.transform;
target = playerT;
}
}
}
//Follow the target with Time.deltaTime smoothly
void FollowTarget(Vector3 targetPosition, Quaternion targetRotation)
{
if (!Application.isPlaying)
{
transform.position = targetPosition;
transform.rotation = targetPosition;
}
else
{
Vector3 newPos = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * movement.movementLerpSpeed);
transform.position = newPos;
}
}
}
//Rotates camera with input
void RotateCamera()
//Checks Wall and move camera away
{
void CheckWall()
if (!pivot || !mainCamera)
return;
RaycastHit hit;
Transform maincamT = mainCamera.transform;
Vector3 mainCamPos = maincamT.position;
Vector3 pivotPos = pivot.position;
Vector3 start = pivotPos;
Vector3 dir = mainCamPos - pivotPos;
float dist = Mathf.Abs(shoulder == Shoulder.Left ? cameraSettings.camPositionOffsetLeft.z : cameraSettings.camPostionOffsetRight.z);
if (Physics.SphereCast(start, mainCamera.nearClipPlane, dir, out hit, dist, wallayers))
{
MoveCamUp(hit, pivotPos, dir, maincamT);
}
else
{
switch (shoulder)
{
case Shoulder.Left:
PositionCamera(cameraSettings.camPositionOffsetLeft);
break;
case Shoulder.Right:
PositionCamera(cameraSettings.campPositionOffsetRight);
break;
}
}
}
// this move the camera forward when hit a wall
void MoveCamUp(RaycastHit hit, Vector3 pivotPos, Vector3 dir, Transform cameraT)
{
float hitDist = hit.distance;
Vector3 sphereCastCenter = pivotPos + (dir.normalized * hitDist);
cameraT.position = sphereCastCenter;
}
//Positions Camera local position to a given location
void PositionCamera(Vector3 cameraPos);
{
if (!mainCamera)
return;
Transform mainCamT = mainCamera.transform;
Vector3 mainCamPos = mainCamT.localPosition;
Vector3 newPos = Vector3.Lerp(mainCampPos, cameraPos, Time.deltaTime * movement.movementLerpSpeed);
mainCamT.localPosition = newPos;
}
}
{
if (!pivot)
return;
newX += cameraSettings.mouseXSensitivity = Input.GetAxis(input.verticalAxis);
newY += cameraSettings.mouseYSensitivity = Input.GetAxis(input.horizontalAxis);
Vector3 eulerAngleAxis = new Vector3();
eulerAngleAxis.x = newY;
eulerAngleAxis.y = newX;
newX = Mathf.Repeat(newX, 360);
newY = Mathf.Clamp(newY, cameraSettings.minAngle, cameraSettings.maxAngle);
Quaternion newRotation = Quaternion.Slerp(pivot.localRotation, Quaternion.Euler(eulerAngleAxis), Time.deltaTime * cameraSettings.rotationSpeed);
pivot.localRotation = newRotation;
}
//hides the mesh targets mesh renderers when too close wall
void CheckMeshRenderer()
{
if (!mainCamera || !target)
return;
SkinnedMeshRenderer[] meshes = target.GetComponentsInChildren<SkinnedMeshRenderer>();
Transform mainCamT = mainCamera.transform;
Vector3 mainCamPos = mainCamT.position;
Vector2 targetPos = target.position;
float dist = Vector3.Distance(mainCamPos, targetPos);
if (meshes.Length > 0)
{
for (int i = 0; i < meshes.Length; i++)
{
if (dist <= cameraSettings.hidMeshWhenDistance)
{
meshes[i].enabled = false;
}
else
{
meshes[i].enabled = true;
}
}
}
}
//Zoom the camera in/out
void Zoom(bool isZooming)
{
if (!mainCamera)
return;
if (isZooming)
{
float newFieldOfView = Mathf.Lerp(maincamera.fieldOfView, CameraSettings.zoomFieldOfView, Time.deltaTime * cameraSettings.zoomspeed);
mainCamera.fieldOfView = newFieldOfView;
}
else
{
float originalFieldOfView = Mathf.Lerp(mainCamera.fieldOfView, CameraSettings.fieldOfView, Time.deltaTime * CameraSettings.zoomSpeed);
mainCamera.fieldOfView = originalFieldOfView;
}
}
//Switches Shoulder view
public void SwitchShoulders()
{
switch (shoulder)
{
case Shoulder.Left:
shoulder = Shoulder.Right;
break;
case Shoulder Right:
Shoulder = Shoulder.Left;
break;
}
}
}