- Home /
Was looking in Unity, the script I had applied to my player prefab defaulted the deceleration rate value to 1 even when I changed it in the script.
Float subtracting in increments of 1 no matter what value it's subtracted by
I'm currently trying to implement movement deceleration on my first person controller, and I am doing that by subtracting the movement speed by a certain deceleration value every frame after the player stops moving. The system is working, however according to the debug console the decelerationSpeed variable is being subtracted by 1 every frame instead of whatever number I declare decelerationRate as.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovementScript : MonoBehaviour
{
public CharacterController controller;
public float speed = 12f;
public float decelerationSpeed;
public float gravity = -9.81f;
public float jumpHeight = 3f;
private float previousY;
public float nextTimeToCheckY;
public float bounceTime = 8;
public float decelerationRate = 0.5f;
public Transform GroundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
public float MovementDampening = 0.25f;
public float decelerationDistance = 0.15f;
public float previousX;
public float previousZ;
Vector3 velocity;
Vector3 previousMove;
Vector3 previousPosition;
bool isGrounded;
bool decelerating = false;
private void Start()
{
decelerationSpeed = speed;
}
// Update is called once per frame
void Update()
{
isGrounded = Physics.CheckSphere(GroundCheck.position, groundDistance, groundMask);
Debug.Log("isGrounded is " + isGrounded);
if(isGrounded && velocity.y < 0)
{
velocity.y = -1f;
}
float x = Input.GetAxisRaw("Horizontal");
float z = Input.GetAxisRaw("Vertical");
if (x != 0 || z != 0)
{
decelerationSpeed = speed;
decelerating = false;
}
Vector3 move = transform.right * x + transform.forward * z;
move.Normalize();
if (isGrounded)
{
if (x != 0 || z != 0)
{
controller.Move(move * speed * Time.deltaTime);
previousMove = move;
}
else if (x == 0 && z == 0 && (previousZ != 0 || previousX != 0))
{
controller.Move(previousMove * decelerationSpeed * Time.deltaTime);
decelerationSpeed = decelerationSpeed - decelerationRate;
decelerating = true;
if (decelerationSpeed == 0)
{
decelerationSpeed = speed;
decelerating = false;
}
}
}
else if (!isGrounded)
{
if ((x == 0 && z == 0) || Vector3.Dot(move, previousMove) < 0)
{
controller.Move(previousMove * speed * Time.deltaTime);
}
else {
Vector3 newMove = previousMove + move;
newMove.Normalize();
controller.Move(newMove * speed * Time.deltaTime);
}
}
previousY = transform.position.y;
if (Input.GetButtonDown("Jump") && isGrounded) {
previousMove = move;
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
if (velocity.y != 0 && previousY == transform.position.y && Time.time >= nextTimeToCheckY)
{
nextTimeToCheckY = Time.time + 1f / bounceTime;
velocity.y = 0;
}
if (!decelerating)
{
previousX = x;
previousZ = z;
}
}
}
Follow this Question
Related Questions
Unity 2*10^-16 == 0 is true!! 1 Answer
subtracting numbers gives a very wrong answer 0 Answers
Why can't I get a floating point value here?... 1 Answer
Why is this Int lagging when its subtracting something over a period of time? 1 Answer
[RESOLVED] Raycast - Calculate Angular Difference from Object 3 Answers