Hello everybody, I have one problem, and need help! I create camera like diablo camera, but the camera is not good. When player jump or move with hight speed, it seems like the camera is lagging…
Here is my script :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DiabloCamera : MonoBehaviour
{
public GameObject player;
//The offset of the camera to centrate the player in the X axis
public float offsetX = 0;
//The offset of the camera to centrate the player in the Z axis
public float offsetZ = -7;
//The offset of the camera to centrate the player in the Y axis
public float offsetY = 3;
//The maximum distance permited to the camera to be far from the player, its used to make a smooth movement
public float maximumDistance = 2;
//The velocity of your player, used to determine que speed of the camera
public float playerVelocity = 10;
// Hight of character
public float heroHeight = 2.0f;
public int collisionLayers = 9;
private float _movementX;
private float _movementZ;
private float _movementY;
void Start() {
}
// Update is called once per frame
void Update()
{
_movementX = ((player.transform.position.x + offsetX - this.transform.position.x))/maximumDistance;
_movementZ = ((player.transform.position.z + offsetZ - this.transform.position.z))/maximumDistance;
_movementY = ((player.transform.position.y + offsetY - this.transform.position.y))/maximumDistance;
this.transform.position += new Vector3((_movementX * playerVelocity * Time.deltaTime),
_movementY,
(_movementZ * playerVelocity * Time.deltaTime));
}
}
Hei, guys, it doesn’t depend on the method Update/LateUpdate or FixedUpdate.
If you want to achieve smooth moving you should not increment or decrement camera’s position without Lerp() function!
Could you please explain to me why http://docs.unity3d.com/430/Documentation/ScriptReference/MonoBehaviour.LateUpdate.html literaly states “For example a follow camera should always be implemented in LateUpdate because it tracks objects that might have moved inside Update.” ? Because I’m wondering why it fixed things for me when I had similar problems.
All the object positions are updated every frame, i.e. inside Update.
If the camera follows an object, the camera transform should be updated after the object has moved. Moving the camera inside LateUpdate guarantees that.
The problems usually happen if physics is involved, because physics calculations are not synchronized with the frame rate.
Thanks all! I’ve already resolved this problem! Here is my new script. Hope this help for new members like me
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class DiabloCamera : MonoBehaviour
{
public GameObject player;
//The offset of the camera to centrate the player in the X axis
public float offsetX = 0;
//The offset of the camera to centrate the player in the Z axis
public float offsetZ = -7;
//The offset of the camera to centrate the player in the Y axis
public float offsetY = 3;
//The maximum distance permited to the camera to be far from the player, its used to make a smooth movement
public float maximumDistance = 2;
//The velocity of your player, used to determine que speed of the camera
public float playerVelocity = 10;
// Hight of character
public float heroHeight = 2.0f;
private float _movementX;
private float _movementZ;
private float _movementY;
void Start() {
hiddenObjects = new Dictionary<Transform, Shader>();
}
// This function is called every fixed framerate frame, if the MonoBehaviour is enabled.
void FixedUpdate()
{
_movementX = ((player.transform.position.x + offsetX - this.transform.position.x))/maximumDistance;
_movementZ = ((player.transform.position.z + offsetZ - this.transform.position.z))/maximumDistance;
_movementY = ((player.transform.position.y + offsetY - this.transform.position.y))/maximumDistance;
// Next position of camera
Vector3 targetPos = this.transform.position + new Vector3((_movementX * playerVelocity * Time.deltaTime),
_movementY,
(_movementZ * playerVelocity * Time.deltaTime));
// Distance between old position and new position
float distanceVec = Vector3.Distance(this.transform.position, targetPos);
// Linearly interpolates between two vectors.
this.transform.position = Vector3.Lerp(this.transform.position, targetPos, distanceVec);
}
}
P/S : I’ve also tried with LateUpdate, but it couldn’t resolved this problem. But FixedUpdate can do it, may be because my character control used FixedUpdate