- Home /
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.
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.
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
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
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.
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.
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
Follow this Question
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