Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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: 

Follow this Question

Answers Answers and Comments

133 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

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges