|
Post by edgirl on Mar 9, 2024 0:41:03 GMT 1
Help! I'm trying to emulate the script that makes the camera move around the player and starts at my desired position and rotation and the player moves where the camera is facing but I don't know how to do it here is the script: using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
[SerializeField]
private float _mouseSensitivity = 8.0f;
private float _rotationY;
private float _rotationX;
[SerializeField]
private Transform _target;
[SerializeField]
private float _distanceFromTarget = 4.5f;
private Vector3 _currentRotation;
private Vector3 _smoothVelocity = Vector3.zero;
[SerializeField]
private float _smoothTime = 0.2f;
[SerializeField]
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * _mouseSensitivity;
float mouseY = Input.GetAxis("Mouse Y") * _mouseSensitivity;
_rotationY += mouseX;
_rotationX += mouseY;
// Apply clamping for x rotation
_rotationX = Mathf.Clamp(_rotationX, -90, 90);
Vector3 nextRotation = new Vector3(_rotationX, _rotationY);
// Apply damping between rotation changes
_currentRotation = Vector3.SmoothDamp(_currentRotation, nextRotation, ref _smoothVelocity, _smoothTime);
transform.localEulerAngles = _currentRotation;
// Substract forward vector of the GameObject to point its forward vector to the target
transform.position = _target.position - transform.forward * _distanceFromTarget;
}
}
|
|