- Home /
Third Person Cinemachine Camera Jittering,Third Person Camera jittering when moving the gameObject it follows
I am attempting to use a Cinemachine freeLook camera to act as a third person camera for a project. Currently I have multiple custom scripts which handle rotating, selecting and panning the camera. All work fine except for the panning. Currently, I am using raycasts to find the point the cursor is placed on and create a new gameobject there. Then when the player keeps their middle mouse button down and move the mouse, pan the camera towards the mouse. This works except when the camera is moving fast (occurs when the mouse is dragged far from the initial middle mouse click spot) or at certain angles (?) when the camera jitters up and down on the Y axis. I am lost as to why it jitters like this. It looks like the player is riding a motorcycle. Below is my code and the CinemachineFreeLook settings.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
using UnityEngine.EventSystems;
public class MiddleMousePanning : MonoBehaviour
{
public CinemachineFreeLook freeLookCam;
public Transform player;
Transform panCam;
Vector3 mousePos;
public float speed = 50f;
private RaycastHit hit;
private void Awake()
{
panCam = new GameObject("PanCamera").transform;
panCam.position = player.position;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(2))
{
freeLookCam.m_Follow = panCam;
freeLookCam.m_LookAt = panCam;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
mousePos = hit.point;
}
}
if (Input.GetMouseButton(2))
{
Vector3 current = panCam.position;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Debug.Log("Hit:" + hit.point.x);
Debug.Log("Mousepos:" + mousePos.x);
if (hit.point.x > mousePos.x + 25)
{
Debug.Log("hitpointx>:" + (hit.point.x - mousePos.x));
current.x += (hit.point.x - mousePos.x) / speed;
}
if (hit.point.x < mousePos.x - 25)
{
Debug.Log("hitpointx<:" + (hit.point.x - mousePos.x));
current.x += (hit.point.x - mousePos.x) / speed;
}
if (hit.point.z > mousePos.z + 25)
{
Debug.Log("hitpointz>:" + (hit.point.z - mousePos.z));
current.z += (hit.point.z - mousePos.z) / speed;
}
if (hit.point.z < mousePos.z - 25)
{
Debug.Log("hitpointz<:" + (hit.point.z - mousePos.z));
current.z += (hit.point.z - mousePos.z) / speed;
}
}
Camera.main.gameObject.transform.LookAt(panCam);
panCam.position = current;
}
}
}
,I am attempting to get a third person camera working using Cinemachine and some custom scripts. I want the camera to follow the player until they hold the middle mouse button, then it should pan to follow their mouse. At the moment this works but my FreeLook camera seems to move to fast for my main camera to keep up. This results in the main camera tilting upwards and jittering. It looks like I'm riding a motorbike. Here is the code:
public class MiddleMousePanning : MonoBehaviour
{
public CinemachineFreeLook freeLookCam;
public Transform player;
Transform panCam;
Vector3 mousePos;
public float speed = 50f;
private RaycastHit hit;
private void Awake()
{
panCam = new GameObject("PanCamera").transform;
panCam.position = player.position;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(2))
{
freeLookCam.m_Follow = panCam;
freeLookCam.m_LookAt = panCam;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
mousePos = hit.point;
}
}
if (Input.GetMouseButton(2))
{
Vector3 current = panCam.position;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
Debug.Log("Hit:" + hit.point.x);
Debug.Log("Mousepos:" + mousePos.x);
if (hit.point.x > mousePos.x + 25)
{
Debug.Log("hitpointx>:" + (hit.point.x - mousePos.x));
current.x += (hit.point.x - mousePos.x) / speed;
}
if (hit.point.x < mousePos.x - 25)
{
Debug.Log("hitpointx<:" + (hit.point.x - mousePos.x));
current.x += (hit.point.x - mousePos.x) / speed;
}
if (hit.point.z > mousePos.z + 25)
{
Debug.Log("hitpointz>:" + (hit.point.z - mousePos.z));
current.z += (hit.point.z - mousePos.z) / speed;
}
if (hit.point.z < mousePos.z - 25)
{
Debug.Log("hitpointz<:" + (hit.point.z - mousePos.z));
current.z += (hit.point.z - mousePos.z) / speed;
}
}
Camera.main.gameObject.transform.LookAt(panCam);
panCam.position = current;
}
}
}
Thanks in advance.
Your answer
Follow this Question
Related Questions
Camera Clips though Walls 1 Answer
Distribute terrain in zones 3 Answers
Add terrain collision and fix axis of camera 0 Answers
how to have 3rd person camera move like 1st person camera 0 Answers
Multiple Cars not working 1 Answer