- Home /
Jittering effect when moving character and camera
Hey!
I want to make a game and I wrote some code that really suits my needs but I'm having a problem. When I'm moving the character and the camera at the same time I get this really weird jitter effect. When I'm moving only the camera or the character I don't get this. Here is my code for the camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float speedH = 2.0f;
public float speedV = 2.0f;
private float yaw = 0.0f;
private float pitch = 0.0f;
public Transform tran;
//This tran is the character transform
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
yaw += speedH * Input.GetAxis("Mouse X");
pitch -= speedV * Input.GetAxis("Mouse Y");
transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
tran.rotation = Quaternion.Euler(0, yaw, 0);
}
}
And this is the character code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour {
public float speed;
private Rigidbody rb;
private Transform tr;
void Start()
{
rb = GetComponent<Rigidbody>();
tr = GetComponent<Transform>();
}
void FixedUpdate()
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
rb.MovePosition(tr.position + (tr.right * moveHorizontal * (speed * 0.01f) + (tr.forward * moveVertical * (speed * 0.01f))));
}
}
So I tried a lot of stuff but nothing works. How do I make this work without jittering? EDIT: Here is the gif of the jittering. Look at the cube. You don't see it when the gif is small. Just zoom in the page. http://imgur.com/6yChVjI
it seems to work fine for changing Update to LateUpdate.
void LateUpdate()
{
yaw += speedH * Input.GetAxis("$$anonymous$$ouse X");
pitch -= speedV * Input.GetAxis("$$anonymous$$ouse Y");
transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
tran.rotation = Quaternion.Euler(0, yaw, 0);
}
rotate eulerAngles using Time.deltaTime
void Update()
{
yaw += speedH * Input.GetAxis("$$anonymous$$ouse X");
pitch -= speedV * Input.GetAxis("$$anonymous$$ouse Y");
transform.eulerAngles = new Vector3(pitch, yaw* Time.deltaTime, 0.0f);
tran.rotation = Quaternion.Euler(0, yaw, 0);
}
Now it doesn't rotate the camera and when it does it shakes like crazy.
Answer by 1234filip · Jun 20, 2017 at 04:55 PM
I found a solution. Instead of rotating the camera on the y axis and then rotating the character, I rotated the character directly on the y axis and camera on the x axis. Of course the camera must be a child of the character.
Answer by GilbertoBitt · Jun 20, 2017 at 05:41 PM
for what i see i'm pretty sure that u r setting the rotation, location of the camera directly on it! u should use some sort of lerp to smooth the movement of the camera to where player is, or to the specific rotation that u desire and this will create a smooth transition and remove the jittering.
here some link that can help u!
I lerped like you said but it's still jittering. Here's my look script now:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class $$anonymous$$ouseLook : $$anonymous$$onoBehaviour
{
public float speedH = 2.0f;
public float speedV = 2.0f;
private float yaw = 0.0f;
private float pitch = 0.0f;
public Transform tran;
float lerpTime = 1f;
float currentLerpTime;
//This tran is the character transform
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
yaw += speedH * Input.GetAxis("$$anonymous$$ouse X");
pitch -= speedV * Input.GetAxis("$$anonymous$$ouse Y");
//reset when we press spacebar
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
{
currentLerpTime = 0f;
}
//increment timer once per frame
currentLerpTime += Time.deltaTime;
if (currentLerpTime > lerpTime)
{
currentLerpTime = lerpTime;
}
//lerp!
float perc = currentLerpTime / lerpTime;
transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, new Vector3(pitch, yaw, 0.0f), perc);
tran.eulerAngles = Vector3.Lerp(tran.eulerAngles, new Vector3(tran.rotation.x , yaw, tran.rotation.z), perc);
}
}
$$anonymous$$ake the camera movement on LateUpdate, this also helps to remove this kinds os problems.
Your answer
Follow this Question
Related Questions
Set rotation is jittering back to original rotation 2 Answers
How to account for situation using RotateAround 0 Answers
fixed rotation around object 1 Answer
Drag Rotation Objet Snapping to 0,0,0 on new drag. 0 Answers
Object won't rotate 2 Answers