Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SocialPlatforms;
- public class CameraController : MonoBehaviour
- {
- public Transform sonic;
- public Transform cam;
- public Vector3 offset;
- public LayerMask cameraMask;
- // Start is called before the first frame update
- void Start()
- {
- }
- // Update is called once per frame
- void Update()
- {
- Vector2 look = new Vector2();
- look.x = Input.GetAxis("Mouse X");
- look.y = Input.GetAxis("Mouse Y");
- transform.position = sonic.position - (transform.forward * 0.5f + sonic.up * 2);
- float cameraDistance = 8;
- if (Physics.Raycast(transform.position, -transform.forward, out RaycastHit hit, 8.3f, cameraMask))
- {
- float distance = Vector3.Distance(transform.position, hit.point);
- cameraDistance = distance - 0.3f;
- }
- float ratio = cameraDistance / 8;
- cam.localPosition = new Vector3(0, 5 * ratio, -cameraDistance);
- Look(look);
- }
- private float xRotation;
- public void Look(Vector2 lookDirection)
- {
- transform.Rotate(Vector2.up, lookDirection.x*5);
- xRotation -= lookDirection.y; //Subtract mouse movement from xRotation(Subtract for invert movement)
- xRotation = Mathf.Clamp(xRotation, -30, 15); //Clamp rotation to prevent backflip
- cam.localRotation = Quaternion.Euler(xRotation, transform.localRotation.y, transform.rotation.z); //Apply xRotation to camera rotation
- }
- }
Add Comment
Please, Sign In to add comment