In my game, I have a third person character. The player moves with a rigidbody and has isKinematic set off with interpolate on. In the script, it moves with Rigidbody.MovePosition with the y position froze. Here’s my player script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed;
public Rigidbody rb;
public Transform camPos;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
camPos = Camera.main.transform;
}
private void FixedUpdate()
{
if (Input.GetKey("w"))
{
rb.MovePosition(transform.position + (camPos.forward * speed * Time.fixedDeltaTime));
}
if (Input.GetKey("s"))
{
rb.MovePosition(transform.position + (-camPos.forward * speed * Time.fixedDeltaTime));
}
if (Input.GetKey("d"))
{
rb.MovePosition(transform.position + (camPos.right * speed * Time.fixedDeltaTime));
}
if (Input.GetKey("a"))
{
rb.MovePosition(transform.position + (-camPos.right * speed * Time.fixedDeltaTime));
}
rb.velocity = Vector3.zero;
}
}
For my camera, I have it lookat the player and lag behind smoothly. Here’s that script:
//Hakeem Thomas
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamPos : MonoBehaviour
{
public Transform target;
public float smoothTime = 0.5F;
private Vector3 velocity = Vector3.zero;
public GameObject player;
private float mouseX, mouseY;
public int mouseX_Speed, mouseY_Speed;
//mouseSensitivity
public int turnSpeed;
void getMouseXY()
{
mouseX += Input.GetAxis("Mouse X") * smoothTime;
mouseY -= Input.GetAxis("Mouse Y") * smoothTime;
}
void LateUpdate()
{
getMouseXY();
// Define a target position above and behind the target transform
Vector3 targetPosition = target.TransformPoint(new Vector3(0, 1.5f, -2.5f));
// Smoothly move the camera towards that target position
transform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, smoothTime);
transform.LookAt(target);
target.rotation = Quaternion.Euler((mouseY * mouseY_Speed), (mouseX * mouseX_Speed), 0);
player.transform.rotation = Quaternion.Euler(0, (mouseX * turnSpeed), 0);
}
}
My issue is that whenever the player moves either the camera/player jitters. I have debugged the player transform y position to make sure it wasn’t moving up or down(it doesn’t). To help with this issue I have either turned down the Fixed Timestep under Time in the project settings or put the code in the camera script is fixed update instead of late update(which I have researched is an issue). I know MovePosition for the player isn’t the right way to move, but I don’t know how to make the player move in the direction the camera is facing with rb.velocity. I suspect the issue may lay in the player script with variable camPos. Either way, I would like to stop the jittering.