- Home /
Character Falling Slower When Moving Back And Forth
I have a simple character controller script that works well accept whenever Im falling and moving in any other direction I fall drastically slower. Im not sure if it has to do with Time.deltaTime and my computer slowing down whenever I move (This shouldnt be the case because I have a nice enough computer (with a decent GPU) to play video games on low settings). Any ideas would be gladly accepted, Thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player_Movement_Script : MonoBehaviour
{
public Camera camera;
public GameObject Front_Collider;
public GameObject Back_Collider;
public GameObject Left_Collider;
public GameObject Right_Collider;
float Player_Gravity = 9.81f / 50;
float Player_Speed = 0.1f;
float Sprint_Addition = 0.05f;
float Sprint_Acceleration = 5;
float Jump = 65f;
float Current_Gravity;
float Current_Speed;
float Ground_Distance;
float Ceiling_Distance;
bool Ceiling_Bool = false;
// Start is called before the first frame update
void Start()
{
Current_Gravity = Player_Gravity;
}
// Update is called once per frame
void Update()
{
Ray Floor_Ray = new Ray(transform.position, -transform.up);
RaycastHit Floor_Hit;
if (Physics.Raycast(Floor_Ray, out Floor_Hit))
{
Ground_Distance = Floor_Hit.distance - 1;
}
Ray Ceiling_Ray = new Ray(transform.position, transform.up);
RaycastHit Ceiling_Hit;
if (Physics.Raycast(Ceiling_Ray, out Ceiling_Hit))
{
Ceiling_Distance = Ceiling_Hit.distance - 1;
}
else
{
Ceiling_Distance = 999;
}
if (Ceiling_Distance < 0.2f && Ceiling_Bool == false)
{
Current_Gravity = Player_Gravity;
Ceiling_Bool = true;
}
if (Ceiling_Distance > 0.2f)
{
Ceiling_Bool = false;
}
if (Ground_Distance > 0)
{
if (Ground_Distance > Current_Gravity * Time.deltaTime)
{
transform.position += new Vector3(0, -Current_Gravity * Time.deltaTime, 0);
Current_Gravity += Player_Gravity;
}
else
{
transform.position += new Vector3(0, -Ground_Distance, 0);
}
}
if (Ground_Distance < 0.1f)
{
Current_Gravity = 0;
if (Input.GetKey(KeyCode.LeftShift))
{
Current_Speed = Mathf.Lerp(Current_Speed, Player_Speed + Sprint_Addition, Time.deltaTime * Sprint_Acceleration);
camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, 70, Time.deltaTime * Sprint_Acceleration);
}
else
{
Current_Speed = Mathf.Lerp(Current_Speed, Player_Speed, Time.deltaTime * Sprint_Acceleration);
camera.fieldOfView = Mathf.Lerp(camera.fieldOfView, 60, Time.deltaTime * Sprint_Acceleration);
}
if (Input.GetKey(KeyCode.Space))
{
Current_Gravity = -(Player_Gravity * Jump);
transform.Translate(0, -Current_Gravity * Time.deltaTime, 0);
}
}
if (Input.GetKey(KeyCode.W))
{
if (Front_Collider.GetComponent<Collider_Scipt>().Colliding == false)
{
transform.Translate(0, 0, Current_Speed);
}
}
if (Input.GetKey(KeyCode.S))
{
if (Back_Collider.GetComponent<Collider_Scipt>().Colliding == false)
{
transform.Translate(0, 0, -Current_Speed);
}
}
if (Input.GetKey(KeyCode.D))
{
if (Right_Collider.GetComponent<Collider_Scipt>().Colliding == false)
{
transform.Translate(Current_Speed, 0, 0);
}
}
if (Input.GetKey(KeyCode.A))
{
if (Left_Collider.GetComponent<Collider_Scipt>().Colliding == false)
{
transform.Translate(-Current_Speed, 0, 0);
}
}
}
}
Answer by whityj375 · Nov 24, 2020 at 02:16 AM
ok never mind, I fixed the issue, my ceiling detection was detecting a part of the sprite when moving at high speeds (such as falling) and was triggering a gravity reset. That was causing my fall to be slowed down.
Your answer
Follow this Question
Related Questions
Arcade Vehicle 0 Answers
How to decrease/(have constant) speed of a ball rolling downhill? 0 Answers
How to make Animation and Physics work together? 2 Answers
problem with wheelcollider 0 Answers
HELP PLEASE PROBLEM WITH JOITS 0 Answers