Hello, I built my game after just testing stuff in the editor after a while, and a strange issue arose. When I move the camera in-game, the character moves slower.
Here’s what I’m talking about:
In-Editor:
https://media.giphy.com/media/L0AnuLX33Xb8Pqvaz0/giphy.gif (Good)
Built:
https://media.giphy.com/media/WnCDwYhj4pu5Auzv96/giphy.gif (Bad)
I have no Idea why it’d do this, as the editor one works fine. The physics start behaving weirdly too. There’s no “#if UNITY_EDITOR” block anywhere in the code, so I’m honestly clueless.
Any theories would be great
Here’s my code for movement:
#region References
[SerializeField]
Camera CAM; //Def FOV = 60, Sprint = 75
[SerializeField]
Keybinds KB;
Rigidbody RB;
#endregion
[SerializeField]
float RunSpeed, SprintSpeed, JumpForce, MaxJumpCount;
float Horizontal, Vertical;
public float JumpCount = 0;
public bool CanMove;
public bool CanDoubleJump;
public bool OnGround;
Vector3 DirectionVector;
private void Start()
{
CanMove = true;
RB = GetComponent<Rigidbody>();
}
void Update()
{
if ( CanMove ) { Move(); }
}
private void LateUpdate()
{
Jump();
}
void Move()
{
Horizontal = Input.GetAxis("Horizontal"); Vertical = Input.GetAxis("Vertical");
DirectionVector.x = Horizontal; DirectionVector.z = Vertical;
DirectionVector = Vector3.ClampMagnitude(DirectionVector, 1f);
if ( !Input.GetKey(KB.Sprint) )
{
CAM.fieldOfView = Mathf.Lerp(CAM.fieldOfView, 60, 0.1f);
RB.MovePosition(transform.position + transform.TransformDirection(DirectionVector * RunSpeed * Time.deltaTime));
}
else
{
CAM.fieldOfView = Mathf.Lerp(CAM.fieldOfView, 90, 0.1f);
RB.MovePosition(transform.position + transform.TransformDirection(DirectionVector * SprintSpeed * Time.deltaTime));
}
}
void Jump()
{
if ( Input.GetKeyDown(KB.Jump) && JumpCount < MaxJumpCount)
{
if (DirectionVector.sqrMagnitude == 0 && OnGround) //Jump extra high when not moving
{
RB.AddForce(((transform.up * 2) + transform.TransformDirection(DirectionVector)) * JumpForce, ForceMode.Impulse);
JumpCount++;
}
else
{
if ( OnGround ) //Extra boost when double jumping
{
RB.AddForce((transform.up + transform.TransformDirection(DirectionVector)) * JumpForce, ForceMode.Impulse);
JumpCount++;
}
else
{
RB.AddForce((transform.up + transform.TransformDirection(DirectionVector)) * JumpForce * 1.5f, ForceMode.Impulse);
JumpCount++;
}
}
}
}
private void OnCollisionEnter( Collision Collision )
{
if (Collision.collider.gameObject.layer == 8 )
{
OnGround = true; JumpCount = 0;
RB.velocity = Vector3.zero;
}
}
private void OnCollisionExit( Collision Collision )
{
if ( Collision.collider.gameObject.layer == 8 )
{
OnGround = false;
}
}
}
And Here’s my Code for the Camera control:
#region References
[SerializeField]
Keybinds KB;
#endregion
[SerializeField]
Transform Target, Player;
//Transform Obstruction = null;
[SerializeField]
float Sensitivity, CameraMaxAngle, CameraMinAngle;
float MouseX, MouseY; //ZoomSpeed;
public bool CanRotate;
private void Start()
{
CanRotate = true;
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
private void Update()
{
CameraControl();
//ViewObstructed();
}
void CameraControl()
{
MouseX += Input.GetAxis("Mouse X") * Sensitivity * Time.deltaTime;
MouseY -= Input.GetAxis("Mouse Y") * Sensitivity * Time.deltaTime;
MouseY = Mathf.Clamp(MouseY, CameraMinAngle, CameraMaxAngle);
transform.LookAt(Target);
Target.rotation = Quaternion.Slerp(Target.rotation, Quaternion.Euler(MouseY, MouseX, 0), 0.5f);
if ( !Input.GetKey(KB.LockPlayerRotation) && CanRotate )
{
Player.rotation = Quaternion.Slerp(Player.rotation, Quaternion.Euler(0, MouseX, 0), 0.5f);
}
}
If you have any clue, but need more details, please say so.