- Home /
When moving player[box] camera shakes. Help
Hi everyone, I'm having this problem with my player char. when moving. I have attached a camera to the Player [ a Cube ] and when I walk the camera shakes. I understand why this is happening, because it's a cube and when I walk it doesn't have smoothness or something... but I don't know how to fix this, what should I do ? Here is my code attached to the player but I don't think that there is the problem:
public class TransformFunctions : MonoBehaviour {
public float rotateSpeed;
public float speed;
public float timeLeft;
public int counter;
public float timercount;
private Light myLight;
private float lRange = 2.5f;
private float orange = 8f;
public AudioClip pick;
public AudioClip recharge;
public TextMesh Bateries;
public TextMesh timeLeft2die;
public TextMesh endText;
public TextMesh WinText;
// Called when the game starts.
void Start()
{
myLight = GetComponent<Light> ();
counter = 0;
timeLeft2die.text = "Time: "+timercount;
endText.text = "";
WinText.text = "";
}
// Update is called once per frame !!!!!
void Update ()
{
ColorChange ();
Movement ();
SetCountText ();
lightSubtract ();
TimeLeft ();
OnTriggerExit ();
ExitApp ();
}
/// collision detection
void OnTriggerEnter (Collider other)
{
if (other.gameObject.tag == "End") {
gameObject.GetComponent<TransformFunctions> ().enabled = false;
WinText.text = "You Win\n press ESC to exit";
}
if (other.gameObject.tag == "PickUp")
{
other.gameObject.SetActive(false);
audio.PlayOneShot(pick, 0.8f);
counter = counter + 1;
Bateries.text = "Bateries: "+counter;
}
}
//bateries count
void SetCountText()
{
if (Input.GetKeyDown (KeyCode.R) && counter > 0) {
counter = counter - 1;
myLight.light.intensity = light.intensity + 2.5f;
audio.PlayOneShot(recharge);
}
Bateries.text = "Bateries: " + counter;
while(counter < 0){
counter = 0;
}
}
void TimeLeft()
{
if (timercount > 0) {
timercount -= Time.deltaTime;
}
while (timercount < 0) {
timercount = 0;
}
timeLeft2die.text = "Time: " + timercount;
}
void OnTriggerExit (){
if (timercount == 0 && gameObject.tag == "Player") {
gameObject.GetComponent<TransformFunctions>().enabled = false;
endText.text = "GAME OVER";
}
}
//movement
void Movement(){
if (Input.GetKey (KeyCode.UpArrow)) {
transform.Translate (Vector3.forward * speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.DownArrow)) {
transform.Translate (Vector3.back * speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.LeftArrow)) {
transform.Rotate (Vector3.down * rotateSpeed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.RightArrow)) {
transform.Rotate (Vector3.up * rotateSpeed * Time.deltaTime);
}
if (Input.GetKeyDown (KeyCode.Space)) {
myLight.enabled = !myLight.enabled;
}
if (Input.GetKeyDown (KeyCode.LeftAlt)) {
myLight.type = LightType.Point;
myLight.range = lRange;
} else if (Input.GetKeyDown (KeyCode.LeftShift)) {
myLight.type = LightType.Spot;
myLight.range = orange;
}
}
void ExitApp(){
if (Input.GetKey (KeyCode.Escape)) {
Application.Quit ();
}
}
//flashlight color change
void ColorChange(){
if (Input.GetKey (KeyCode.Keypad0) || Input.GetKey (KeyCode.F1)) {
myLight.light.color = Color.cyan;
}
if (Input.GetKey (KeyCode.Keypad1)|| Input.GetKey (KeyCode.F2)) {
myLight.light.color = Color.red;
}
if (Input.GetKey (KeyCode.Keypad2)|| Input.GetKey (KeyCode.F3)) {
myLight.light.color = Color.yellow;
}
if (Input.GetKey (KeyCode.Keypad3)|| Input.GetKey (KeyCode.F4)) {
myLight.light.color = Color.green;
}
if (Input.GetKey (KeyCode.Keypad4)|| Input.GetKey (KeyCode.F5)) {
myLight.light.color = Color.magenta;
}
}
void lightSubtract ()
{
myLight.light.intensity -=Time.deltaTime * 0.1f;
}
}
How to remove the camera shake when walking with the Player [ cube ] ? Thanks!
There's a bit too much code for me to read through right now, but you might find putting movement code for the camera in LateUpdate() ins$$anonymous$$d of Update() could solve your problem.
Alternately, you could give your camera a unique smoothed follow script that has small enough lag behind player movement for collider juddering to be unnoticeable.
I'd suggest trying something like currentCameraPosition = Vector3.Lerp(currentCameraPosition,idealCameraPosition,lerpValue)
(where lerpValue
is between 0 and 1, ideally as high of a value as you can get away with without juddering).
If the camera lagging too far behind the player becomes an issue, you can make lerpValue
's value dependent on distance to the ideal camera position:
const float $$anonymous$$inLerpValue = 0.3f;
const float $$anonymous$$axDist = 10f;
float curDist = Vector3.Distance(currentCameraPosition,idealCameraPosition);
float lerpValue = $$anonymous$$athf.Clamp01(curDist/$$anonymous$$axDist); //sets lerpValue to 1 if curDist>maxDist
if (lerpValue<$$anonymous$$inLerpValue) lerpValue = $$anonymous$$inLerpValue;
Answer by sanmn19 · Jan 15, 2014 at 08:05 AM
Change from box collider to sphere collider for cube. Also, add a rigidbody to the camera and freeze it rotation in all axes in inspector. The camera shakes because of the friction between cube and the surface.
Thank you mate but how can I freeze the rotation? I've attached a camera in the Player gameObject throught the add component menu. Now I have to make a new camera ? What exactly?
I want to know if you want the sphere to rotate while moving? If the answer is no, then you need to
Create an empty gameObject. Add a sphere collider component approximately equal to the size of the cube.
$$anonymous$$ake your cube a child of the gameObject created in the above step.
For the cube attach a rigidbody and freeze the rotation along x y and z. It can be done in the inspector.
Now attach the main camera in the scene to you cube.
So the heirarchy is as follows:
- EmptygameObject
Cube - mainCamera
If the answer is yes:
Remove box collider for cube. Attach sphere collider of approx. Same size.
$$anonymous$$ake main camera as a child of the cube.
Add rigidbody to the camera. Freeze rotation along x y and z axis from the inspector.
The heirarchy is as follows:
Cube - mainCamera
When I say attach the mainCamera I mean drag and drop the main camera to the desired gameObject to make it its child.
Thanks mate but I just added this to my Update() function :) - transform.eulerAngles = new Vector3(0,transform.eulerAngles.y,0);
And it worked just like I wanted it to ! :) No camera shaking! And I changed the box collider to Sphere collider just like you said! Thanks for all !!!!! I really appreciate IT !!!!!!!
BTW this is the DL link to the game - http://www50.zippyshare.com/v/18838542/file.html
you can check it and see how everything is working :)
You are welcome. If you found answrr to be correct, please mark it as correct.