Hi!
I made a post about this problem of mine before, but got no answers. ![]()
So I decided to try my best to dig into the problem with the help of Google and I think I’ve made some significant progress! ![]()
I’ve simplified my camera rotation code and have made the hierarchy much simpler than it was before. (Which was probably scaring off people from my previous post.
)
The just of my system includes an object and camera. These objects are NOT parented in any way:
The object has a script attached that creates player movement (I feel that this script may not be the problem, but I included it anyways just in case):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour {
static Animator anim;
public float speed = 1.07f;
public float runMultiplier = 1.5f;
public float jumpHeight = 0.91f;
public float distanceFromGroundUntilNextJump = 0.28f;
public float maxSpeed = 1f;
public Rigidbody rigidBody;
enum Speed { Slow, Regular };
Speed playerSpeed = Speed.Regular;
float varyingMaxSpeed;
float varyingSpeed;
bool running = false;
bool isGrounded = false;
bool wasHoldingShift = false;
float animX;
float animY;
void Start () {
// I made the player visual invisible and the player controller visible for the sake of simplicity.
anim = GameObject.Find("PlayerVisual").GetComponent<Animator>();
varyingMaxSpeed = maxSpeed;
varyingSpeed = speed;
}
void FixedUpdate()
{
Animation();
CheckRunning();
Movement();
checkGround();
}
void Animation()
{
if (running)
{
animX = Input.GetAxis("Horizontal4Anim");
animY = Input.GetAxis("Vertical4Anim");
}
else
{
animX = Input.GetAxis("Horizontal4Anim") * 0.5f;
animY = Input.GetAxis("Vertical4Anim") * 0.5f;
}
anim.SetFloat("MoveX", animX, 0.1f, Time.deltaTime);
anim.SetFloat("MoveY", animY, 0.1f, Time.deltaTime);
}
void Movement()
{
if (!keyDown())
{
varyingMaxSpeed = 0.0f;
}
switch (playerSpeed)
{
case Speed.Regular:
rigidBody.AddRelativeForce(new Vector3(Input.GetAxis("Horizontal") * varyingSpeed, 0.0f, Input.GetAxis("Vertical") * varyingSpeed) * Time.deltaTime);
if (running)
{
varyingMaxSpeed = maxSpeed * runMultiplier;
varyingSpeed = speed * runMultiplier;
}
else
{
varyingMaxSpeed = maxSpeed;
varyingSpeed = speed;
}
break;
case Speed.Slow:
rigidBody.AddRelativeForce(new Vector3(Input.GetAxis("Horizontal") * varyingSpeed * 0.17f, 0.0f, Input.GetAxis("Vertical") * varyingSpeed * 0.17f) * Time.deltaTime);
if (running)
{
varyingMaxSpeed = maxSpeed * runMultiplier;
varyingSpeed = speed * runMultiplier;
}
else
{
varyingMaxSpeed = maxSpeed;
varyingSpeed = speed;
}
break;
}
float yCache = rigidBody.velocity.y;
rigidBody.velocity = new Vector3(rigidBody.velocity.x, 0.0f, rigidBody.velocity.z);
rigidBody.velocity = Vector3.ClampMagnitude(rigidBody.velocity, varyingMaxSpeed);
rigidBody.velocity = new Vector3(rigidBody.velocity.x, yCache, rigidBody.velocity.z);
if (isGrounded == true)
{
if (!keyDown())
{
float curSpeed = rigidBody.velocity.magnitude;
float newSpeed = curSpeed - 53f * Time.deltaTime;
if(newSpeed < 0.0f)
{
newSpeed = 0.0f;
}
rigidBody.velocity = rigidBody.velocity.normalized * newSpeed;
}
playerSpeed = Speed.Regular;
if (Input.GetButton("Jump"))
{
anim.SetBool("Jumping", true);
rigidBody.velocity = new Vector3(rigidBody.velocity.x, 0.0f, rigidBody.velocity.z);
rigidBody.AddForce(0.0f, jumpHeight / 4, 0.0f);
if (!keyDown())
{
rigidBody.velocity = new Vector3(rigidBody.velocity.x * 0.7f, rigidBody.velocity.y, rigidBody.velocity.z * 0.7f);
}
}
}
else
{
playerSpeed = Speed.Slow;
}
}
void checkGround()
{
if (Physics.Raycast(gameObject.transform.position, Vector3.down, distanceFromGroundUntilNextJump))
{
rigidBody.velocity = new Vector3(rigidBody.velocity.x, 0.0f, rigidBody.velocity.z);
isGrounded = true;
if (anim.GetBool("Jumping"))
{
anim.SetBool("Jumping", false);
}
}
else
{
isGrounded = false;
}
}
bool keyDown()
{
return (Input.GetAxis("Vertical") > 0.2f || Input.GetAxis("Vertical") < -0.2f || Input.GetAxis("Horizontal") > 0.2f || Input.GetAxis("Horizontal") < -0.2f);
}
void CheckRunning()
{
if (Input.GetButton("RunCont"))
{
running = true;
}
if (Input.GetButton("Run"))
{
running = true;
wasHoldingShift = true;
}
else if (!Input.GetButton("Run") && wasHoldingShift)
{
running = false;
wasHoldingShift = false;
}
}
}
The camera has a script attached that deals with the camera’s movement (This is where I believe the problem lies.):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TPC : MonoBehaviour {
//The object that the camera will be looking at. (The Player.)
public Transform lookAt;
//The minimum distance that the camera can go. (For camera collisions.)
public float minimumDistance = 1.0f;
//The maximum distance that the camera can go. (For camera collisions.)
public float maximumDistance = 5.0f;
//How smooth the transition is between distances.
public float distanceSmoothing = 10.0f;
//How far the player can rotate the camera up.
public float minimumAngle = -80.0f;
//How far the player can rotate the camera down.
public float maximumAngle = 80.0f;
//How much your mouse movement influences
public float sensitivity = 1.0f;
//Determines whether the mouse controls return opposite values.
public bool invertedLook = false;
//The paramater that will determine the final distance value for the camera.
private float distance = 0.0f;
//Returns the updated calculated distance value. (I have 2 values for distance in order to add a smoothing function.)
private float newDistance = 0.0f;
//The mouse positions on the x and y axes.
private float currentX = 0.0f;
private float currentY = 0.0f;
private void Start()
{
//Makes sure that the cursor doesn't move.
Cursor.lockState = CursorLockMode.Locked;
//Makes sure the cursor is invisible.
Cursor.visible = false;
}
private void Update()
{
//Inverted Mouse mode on/off.
if (invertedLook)
{
currentX += -Input.GetAxis("Mouse X") * sensitivity;
currentY += Input.GetAxis("Mouse Y") * sensitivity;
} else
{
currentX += Input.GetAxis("Mouse X") * sensitivity;
currentY += -Input.GetAxis("Mouse Y") * sensitivity;
}
//Limit how much the player can see up and down.
currentY = Mathf.Clamp(currentY, minimumAngle, maximumAngle);
}
private void LateUpdate()
{
//Make a rotation value corresponding to the mouse movement that will be used for a couple calculations.
Quaternion rotation = Quaternion.Euler(currentY, currentX, 0);
//Gets the position of the maximum distance that the camera can go from the player.
Vector3 maxDisCheck = new Vector3(0, 0, -maximumDistance);
Vector3 desiredCameraPos = lookAt.position + rotation * maxDisCheck;
RaycastHit hit;
//Makes a raycast between the camera's current position and the farthest position.
if (Physics.Linecast(lookAt.position, desiredCameraPos, out hit))
{
//If there's something in between those points, set the new distance to the distance between the player and where the raycast hit.
newDistance = Mathf.Clamp(hit.distance * 0.6f, minimumDistance, maximumDistance);
}
else
{
// If there isn't, then just set the new distance to as far as it can go, because theres nothing stopping it from going that far.
newDistance = maximumDistance;
}
//Make the real distance variable equal to the new distance variable, but with a lerp to make it a smooth transition.
distance = Mathf.Lerp(distance, newDistance, Time.deltaTime * distanceSmoothing);
//Set the position of the camera much similar to the desiredCameraPos variable, but instead of using the maximum distance, use the calculated distance variable.
Vector3 direction = new Vector3(0, 0, -distance);
transform.position = lookAt.position + rotation * direction;
transform.LookAt(lookAt.position);
//Allow the "MouseX" value to determine the y rotation of the player.
lookAt.transform.rotation = Quaternion.Euler(lookAt.transform.rotation.x, currentX, lookAt.transform.rotation.z);
}
}
I cannot provide a video of whats happening because the frame rate of the video would have to be EXACTLY the same as the game to see the effect. (Which is impossible for me.)
The camera is sort of stuttering when it moves without turning the camera, but is most noticeable when I circle objects.
I’ve been on this problem for about half a month now and it really puts a roadblock in my game’s development. If anyone has the slightest idea of whats going on, ANY help will be much appreciated! ![]()
Thank you! ![]()
