Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
0
Question by RealMTG · Jul 16, 2015 at 01:06 AM · vector3charactercontrollersmooth

Smoothly move with Character Controller

Hi

I am trying to make a player controller script using the built in Character Controller and I need the player to smoothly move. Right now the player movement is very stiff. It instantly gets the speed I've set it to and instantly stops. I tried to pull some code from another rigidbody player I made before to smooth the movement but no luck. I have been searching all over the web and still no luck. So this is my last resort. I know this question have been asked tons of times but I can't find anything that really works for me.

Here's my current code at a attempt to smooth the movement.

 cc = CharacterController.
 moveDirection = Vector3.
 -----
 moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
 moveDirection = transform.TransformDirection(moveDirection);
 moveDirection *= movementSpeed;
 Vector3 targetMoveAmount = moveDirection * movementSpeed;
 moveDirection = Vector3.SmoothDamp(moveDirection, targetMoveAmount, ref smoothMoveVelocity, .15f);
                 
 if (Input.GetKeyDown(movementSettings.jumpKey))
 {
     if (movementSettings.enableJumping && cc.isGrounded)
     {
         moveDirection.y = movementSettings.jumpForce;
     }
 }
 
 moveDirection.y -= movementSettings.gravity * Time.deltaTime;
 cc.Move(moveDirection * Time.deltaTime);

As I said before, this code moves the player but does not move it smoothly.

Any help is appreciated!

Also, if you have some extra time, I also need some help with the jumping. Right now if I try and jump is moves me back to the ground really fast. I followed the Unity docs for that so I have no idea why it would do that.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Jeepjump · Aug 22, 2015 at 11:28 PM

You can try using Input.GetAxis instead of Input.GetAxisRaw that will make movement smoother since I think GetAxisRaw just checks if input's either on or off, whereas GetAxis transitions from off to on. You can also have a look at Mathf.lerp . You can use that to transition from one value to another smoothly. Just in the example on the code reference they use Time.time for the transition time where you probably want to use Time.deltaTime.

Hopefully that helps somewhat.

Comment
Add comment · 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
0

Answer by Wolfshadow · Aug 23, 2015 at 01:32 AM

It really is simple. Add a rigidbody to the camera. Then use the addforce method. My code is short, and very effective.

 using UnityEngine;
 using System.Collections;
 
 public class moveEnact : MonoBehaviour 
 
 {
     public float mvar = 2000;
      void Start()
 
     {
         Debug.Log ("success ME");
         GetComponent<Rigidbody>().AddForce (transform.forward * mvar);
     }


I just used void start for an example

Comment
Add comment · Show 1 · 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 Wolfshadow · Aug 23, 2015 at 01:33 AM 0
Share

This method really does work

avatar image
0

Answer by ashulgach · Aug 03, 2017 at 05:30 AM

@RealMTG Hi,

I'm having some issues getting this smooth movement to work on my code too. The suggestions above don't make sense to me. Would you be willing to chat really quick and help me with this since it seems you have figured it out?

Thanks, Andrei

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 ashulgach · Aug 09, 2017 at 11:53 PM 0
Share

@Real$$anonymous$$TG can you please help?

avatar image RealMTG ashulgach · Aug 10, 2017 at 08:55 AM 0
Share

It depends a lot on what you want to do. As the answer says, the first thing you can do is to not use 'Input.GetAxisRaw' but ins$$anonymous$$d use 'Input.GetAxis' whenever you get the input. If that doesn't work, you can smooth the raw value yourself. I would recommend taking a look at $$anonymous$$athf.SmoothDamp is that's the case.

avatar image ashulgach · Aug 10, 2017 at 04:32 PM 0
Share

Yes...I tried that. $$anonymous$$y question is HOW I would use $$anonymous$$athf.SmoothDamp because the FPS controller is very confusing with it's use of external classes.

avatar image RealMTG ashulgach · Aug 10, 2017 at 05:01 PM 1
Share

Here's how I used SmoothDamp.

 Vector2 movementInput = Vector2.zero;
 float forwardVelocity = 0;
 float sidewaysVelocity = 0;
 
 public float movementSmoothing = 0.15f;
 
 public void GetInput()
 {
     movementInput .x = $$anonymous$$athf.SmoothDamp(movementInput .x, Input.GetAxisRaw("Horizontal"), ref forwardVelocity, movementSmoothing );
     movementInput .y = $$anonymous$$athf.SmoothDamp(movementInput .y, Input.GetAxisRaw("Vertical"), ref sidewaysVelocity, movementSmoothing );
 }

Then your player can move on the X axis with movementInput.x and on the Z axis with movementInput.y.

Hope this helps you!

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

24 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

Related Questions

C# Smoothing Out transform.Translate 0 Answers

Fire Knockback 3 Answers

Move CharacterController a specified distance away from starting position. 1 Answer

Vector3.SmoothDamp has some invalid arguments 0 Answers

Unity smooth following 2D camera not working properly on different resolutions 0 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