- Home /
Camera judders when I press jump in midair
I'm a total beginner at Unity but I managed to frankenstein together a mostly functional 3D Player Controller script including jumping, camera following the player and the player moving and rotating relative to the camera position. However if I hit the jump (space) button while the player gameobject is in midair the camera suddenly jumps upwards. Every fix I tried prevented the player from jumping at all while a direction is pushed. Could someone please help?
void Move(float lh, float lv, float jmp)
{
movement.Set(lh, jmp, lv);
movement = Camera.main.transform.TransformDirection(movement);
bool isGrounded = CheckGrounded();
// Check if the player is pressing the jump key
if (jmp > 0f)
{
// Make sure we've not already jumped on this key press
if (!pressedJump && isGrounded)
{
// We are jumping on the current key press
pressedJump = true;
// Jumping vector
Vector3 jumpVector = new Vector3(0f, jumpSpeed, 0f);
// Make the player jump by adding velocity
playerRigidBody.velocity = playerRigidBody.velocity + jumpVector;
}
}
else
{
// Update flag so it can jump again if we press the jump key
pressedJump = false;
}
$$anonymous$$ight need a bit more info to help out. Where is your camera in the hierarchy? Where is this $$anonymous$$ove() function being called?
Answer by Diemx · Mar 13, 2018 at 10:05 PM
Move is being called in FixedUpdate (I'm not 100% sure whether that's the right option compared to just Update):
void FixedUpdate()
{
float lh = Input.GetAxisRaw("Horizontal");
float lv = Input.GetAxisRaw("Vertical");
float jmp = Input.GetAxis("Jump");
Move(lh, lv, jmp);
}
For the camera I'm using a script grabbed from elsewhere:
using UnityEngine;
using System.Collections;
public class SmoothFollow : MonoBehaviour
{
// The target we are following
public Transform target;
// The distance in the x-z plane to the target
public float distance = 10.0f;
// the height we want the camera to be above the target
public float height = 5.0f;
// How much we
public float heightDamping = 2.0f;
public float rotationDamping = 3.0f;
// Place the script in the Camera-Control group in the component menu
[AddComponentMenu("Camera-Control/Smooth Follow")]
void LateUpdate()
{
// Early out if we don't have a target
if (!target) return;
// Calculate the current rotation angles
float wantedRotationAngle = target.eulerAngles.y;
float wantedHeight = target.position.y + height;
float currentRotationAngle = transform.eulerAngles.y;
float currentHeight = transform.position.y;
// Damp the rotation around the y-axis
currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height
currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation
var currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
// Set the position of the camera on the x-z plane to:
// distance meters behind the target
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
// Set the height of the camera
transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);
// Always look at the target
transform.LookAt(target);
}
}
The only change I made to the MainCamera in the Hierarchy is adding this script component and its variables and setting the tag to MainCamera
Your answer
Follow this Question
Related Questions
Does anyone know why this simple player camera lock script is not working? 0 Answers
Infinite jumping problem: Jump function (kinda) stops calling when camera follow script applied 0 Answers
Problems with Gyroscope VR Car Game 0 Answers
How to get the camera's size in world units 1 Answer
Camera rotating for rotating object 0 Answers