My camera is not smoothly following my character
I am creating a 3D game and I have just recently implemented a camera that follows the player. The camera will shift once the player reaches a certain end of the screen, so the camera isn't completely fixed, but rather follows the character around. However, when the camera begins to shift to follow the player, it seems to be a bit laggy and not smooth. I have pasted the scripts I have used for both my Camera setup as well as movement function for my character, just in case the problem might lie there (without the camera setup it seems smooth but just in case).
CameraRigTarget attached to an empty object to set a position for the camera
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CameraRigTarget : MonoBehaviour {
public GameObject focusPoint;
public Vector3 offset;
private void Start()
{
offset = this.transform.position - focusPoint.transform.position;
}
// Update is called once per frame
void Update () {
this.transform.position = focusPoint.transform.position + offset;
}
}
SmoothTargetMovement follows the character
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SmoothTargetMovement : MonoBehaviour {
public Transform target;
public float speed;
public float threshold = 0.01f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Vector3 difference = target.transform.position - this.transform.position;
if (difference.magnitude > threshold) {
Vector3 direction = difference.normalized;
Vector3 movement = direction * speed * Time.deltaTime;
if (movement.magnitude > difference.magnitude)
{
this.transform.position = target.transform.position;
}
else
{
this.transform.position += movement;
}
}
}
}
Walk character movement
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Walk : MonoBehaviour {
public float dSpeed = 1.5f;
public float dAngleSpeed = 5f;
private Rigidbody rbody;
// Use this for initialization
void Start () {
Debug.Log("Walk Start");
rbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update() {
var newvel = new Vector3(0f, 0f, 0f);
var curspeed = dSpeed;
if (Input.GetKey(KeyCode.W)) // move forward
{
if (Input.GetKey(KeyCode.LeftShift)) // run
{
Debug.Log("Sprinting");
curspeed += 2f;
}
newvel += (transform.forward * curspeed);
}
if (Input.GetKey(KeyCode.S)) // move backward
{
newvel -= (transform.forward * curspeed);
}
rbody.velocity = newvel;
if (Input.GetKey(KeyCode.D)) // rotate right
{
transform.Rotate(0, dAngleSpeed, 0);
}
if (Input.GetKey(KeyCode.A)) // rotate left
{
transform.Rotate(0, -dAngleSpeed, 0);
}
}
}
I would really appreciate any help anyone could offer, any insight as to what the problem is, or if there are any decent tutorials that anyone could point me to. Thank you!
You should move you character in FixedUpdate() and try interpolation for RigidBody
Answer by Vadol · Nov 02, 2017 at 03:53 AM
Try Vector3.Lerp
this.transform.position = Vector3.Lerp (this.transform.position, focusPoint.transform.position, speed);