0
\$\begingroup\$

On my player I have the component Animator , Rigidbody , Capsule Collider , Third Person User Control script , Third Person Character script.

Player Settings

On the Main Camera there are two scripts but only one is activated for now : Camera Follow and Mouse Orbit With Zoom :

Main Camera Settings

The Camera Follow script is working fine. The problem is when I'm activating the Mouse Orbit With Zoom script if I move the player with the keys WASD and also rotate orbit the camera with the mouse the player lose focus. He does change his direction according to the mouse rotation but also lost focus :

Player lost focus when rotation camera

And also for some reason I can make the camera rotation to be diagonal:

diagonal

The reason Main Camera is not child of the player is that it's making the camera stuttering and rotating very fast on place. That is why I added the Camera Follow script.

The Camera Follow script :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    public Transform player;
    public Vector3 offset;

    // Update is called once per frame
    void Update()
    {
        transform.position = player.position + offset;
    }
}

The orbit script :

using UnityEngine;
using System.Collections;

[AddComponentMenu("Camera-Control/Mouse Orbit with zoom")]
public class MouseCameraOrbit : MonoBehaviour
{
    public Transform target;
    public float speed;

    private void Update()
    {
        if (Input.GetMouseButton(0))
        {
            transform.RotateAround(target.position,
                                            transform.up,
                                            -Input.GetAxis("Mouse X") * speed);

            transform.RotateAround(target.transform.position,
                                            transform.right,
                                            -Input.GetAxis("Mouse Y") * speed);
        }
    }
}

My game is adventure game type and the main goal is to make player movement to be with the keys and also to be able to look around like orbiting with the mouse because only following the player will not show where to go , that is why I'm trying to add orbiting, but the orbiting part is just not working good.

\$\endgroup\$
1
  • \$\begingroup\$ Never mind I will use Cinemachine. \$\endgroup\$
    – Daniel Lip
    Aug 6, 2020 at 0:01

1 Answer 1

1
\$\begingroup\$

You might be able address the "camera stuttering" issue by replacing Update with LateUpdate in CameraFollow.cs

This uses LateUpdate() which ensures the camera positioning occurs after any transforms in Player.Update() and any physics effects

public class CameraFollow : MonoBehaviour
{
    public Transform player;
    public Vector3 offset;


    void LateUpdate()  
    {
        transform.position = player.position + offset;
    }

...
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .