Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
1
Question by God_of_Koolaid · Jul 13, 2021 at 11:25 PM · movementsnappingsmoothdamp

How to apply smooth damp to movement?

Hi, my current movement when rapidly pressing left, right, forward, backward feels too snappy. How do I make it so that the speed builds up to its full potential speed, when you hold the button long enough? I'm a beginner, so could you explain why and how it would work?

Here's my code:

public class CharacterMovement : MonoBehaviour {

 public CharacterController controller;

 public Transform cam;

 public float speed = 6f;

 // Start is called before the first frame update
 void Start()
 {
     
 }

 // Update is called once per frame
 void Update()
 {
     float horizontal = Input.GetAxisRaw("Horizontal");
     float vertical = Input.GetAxisRaw("Vertical");
     Vector3 direction = new Vector3(horizontal, 0, vertical).normalized;

     if (direction.magnitude >= 0.1f)
     {

         float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;

         Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
         controller.Move(moveDirection.normalized * speed * Time.deltaTime);

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Pangamini · Jul 13, 2021 at 11:51 PM

Create a speed or velocity member variable and increase / decrease that when you press the button. For example

 float speed; // make this a member of the class
 float forwardInput = keyPressed? 1 : 0;
 speed = Mathf.MoveTowards(speed, forwardInput * walkSpeed, Time.deltaTime * acceleration);

Make sure it's the class member variable, not a local variable, so its value is preserved. Then, simply move the character by the speed / velocity in every Update

 position += Transform.forward * speed * Time.deltaTime;

Extend this code to add negative speed when backwards button is pressed (or add strafing as well) The difference between the speed and velocity could be that speed is a scalar while velocity is a vector. In the example above, you accumulate speed that you move forward with, even after your character changes facing (rotates / turns). You could hold a velocity variable instead of a speed, expressing the movement over time in world space. In such case it could look like this

 Vector3 velocity; //again, a member of the class
 float forwardInput = keyPressed? 1 : 0;
 velocity = Vector3.MoveTowards(velocity, Transform.forward * forwardInput * walkSpeed, Time.deltaTime * acceleration);
 position += velocity * Time.deltaTime;

The difference here is that once you accumulate velocity, you will continue moving in that same direction even after your character turns / rotates

Comment
Add comment · Show 4 · Share
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
avatar image God_of_Koolaid · Jul 14, 2021 at 01:34 AM 0
Share

I'm having trouble understanding. There is a problem with keyPressed, is it a function? Should I have "speed = Mathf.MoveTowards(speed, forwardInput walkSpeed, Time.deltaTime acceleration);" as the body of that function?

Here's my current Code: public class CharacterMovement : MonoBehaviour {

 public CharacterController controller;

 public Transform cam;

 //public float speed = 6f;

 float speed;
 public float walkSpeed = 100f;
 public float acceleration = 100f;

 // Update is called once per frame
 void Update()
 {
     float horizontal = Input.GetAxisRaw("Horizontal");
     float vertical = Input.GetAxisRaw("Vertical");
     Vector3 direction = new Vector3(horizontal, 0, vertical).normalized;

     float forwardInput = keyPressed ? 1 : 0;
     speed = Mathf.MoveTowards(speed, forwardInput * walkSpeed, Time.deltaTime * acceleration);

     if (direction.magnitude >= 0.1f)
     {

         float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;

         Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
         controller.Move(moveDirection.normalized * speed * Time.deltaTime);



     }

 }

}

avatar image Pangamini God_of_Koolaid · Jul 14, 2021 at 08:40 AM 0
Share

Consider it a pseudocode. Replace keyPressed with your input method - GetAxisRaw

avatar image God_of_Koolaid Pangamini · Jul 14, 2021 at 09:24 PM 0
Share

There's another problem for whatever reason. "W" works fine for moving forward, however, "S" also makes the player move forward. Sometimes both inputs have problem where the player will move back and then move forward again. The strafe buttons don't work unless I'm using them with the forward or back input, resulting in moving diagonally, so at least moving diagonally works. I added some other functions while trying to fix things, but I don't think they interfere with the movement code. Thanks for helping, I really appreciate it.

Here's the Code: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class CharacterMovement : MonoBehaviour {

 public CharacterController controller;

 public Transform cam;

 public float speed;

 public float walkSpeed = 100f;
 public float acceleration = 10f;

 public float gravity = -9.81f;

 public Transform groundCheck;
 public float groundDistance = 0.4f;
 public LayerMask groundMask;

 Vector3 velocity;

 bool isGrounded;

 // Start is called before the first frame update
 void Start()
 {

 }

 // Update is called once per frame
 void Update()
 {

     CalculateGravity();
     CalculateGrounded();
     CalculateMovement();

 }

 private void CalculateMovement()
 {

     float strafeInput = Input.GetAxisRaw("Horizontal");
     float forwardInput = Input.GetAxisRaw("Vertical");


     //float horizontal = Input.GetAxisRaw("Horizontal");
     //float vertical = Input.GetAxisRaw("Vertical");


     Vector3 direction = new Vector3(strafeInput, 0, forwardInput).normalized;

     speed = Mathf.MoveTowards(speed, forwardInput * walkSpeed, Time.deltaTime * acceleration);


     if (direction.magnitude >= 0.1f)
     {
         float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;

         Vector3 moveDirection = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
         controller.Move(moveDirection.normalized * speed * Time.deltaTime);
     } 
     
 }

 private void CalculateGravity()
 {
     velocity.y += gravity * Time.deltaTime;

     controller.Move(velocity * Time.deltaTime);
 }

 private void CalculateGrounded()
 {
     isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

     if(isGrounded && velocity.y < 0)
     {
         velocity.y = -2f;
     }
 }
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

177 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 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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

NPC jittery/stuttery Movement 2 Answers

Making a bubble level (not a game but work tool) 1 Answer

Camera Movement 1 Answer

Smooth movement when input is pressed 1 Answer


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