• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Mar 22, 2020 at 04:37 AM by Fonics for the following reason:

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.

avatar image
0
Question by Fonics · Mar 22, 2020 at 06:28 PM · mathfloatfloatingpointsubtractingfloating point

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;
         }
     }
 }

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

  • Sort: 

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

134 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

subtracting numbers gives a very wrong answer 0 Answers

Unity 2*10^-16 == 0 is true!! 1 Answer

Float check not returning the right value. 1 Answer

how to keep fraction number ??? 1 Answer

Why do those two calculations return different values? 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges