using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraSc : MonoBehaviour
{
public float sens;
public Transform body;
public Transform head;
float xRot = 0;
void Start()
{
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
void Update()
{
float x = Input.GetAxisRaw("Mouse X") * sens * Time.deltaTime;
float y = Input.GetAxisRaw("Mouse Y") * sens * Time.deltaTime;
xRot -= y;
xRot = Mathf.Clamp(xRot,-80f,80f);
transform.localRotation = Quaternion.Euler(xRot,0f,0f);
body.Rotate(Vector3.up, x);
transform.position = head.position;
//transform.rotation = head.rotation;
}
}
I have a body and I want my camera to follow my head. it follows of course but it vibrates, its like its not moving , its teleporting to head every second. I tried using FixedUpdate but it was worse. I tried Lerp too but lerp makes camera follow slow, when I move my mouse quick it takes a lot of time to follow.
CodePudding user response:
If you don't need any acceleration or advanced movement for your camera, you can simply make your camera gameobject a child of your head gameobject in the scene hierarchy (or prefab). This will make your camera copy the position and rotation of the head gameobject without any coding.
If you wish to make it third person view, you can simply modify the child position which will always be local to the parent's one.
Hopefully this helps.
CodePudding user response:
It's can be complex problem. Maybe one of these solutions can help.
Why you use
Input.GetAxisRaw()
- this function return value without smoothing filtering, tryInput.GetAxis()
as alternative."Vibration" effect can be if camera position updates later than your body position. Try setup position for you body object in
LateUpdate()
.Problem can be in rotation calculation, in your case euler angles will be enough. Try to use something like this:
public float sens = 100.0f; public float minY = -45.0f; public float maxY = 45.0f; private float rotationY = 0.0f; private float rotationX = 0.0f; private void Update() { rotationX = Input.GetAxis("Mouse X") * sens * Time.deltaTime; rotationY = Input.GetAxis("Mouse Y") * sens * Time.deltaTime; rotationY = Mathf.Clamp(rotationY, minY, maxY); transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0); }