Question by
AaronKanaron · Aug 17, 2020 at 07:54 AM ·
movementmovement scriptplayer movement
How do i lerp player movement?,How to lerp movment
Hello, I have made a third person controller and when I stop walking, I stop immediately. How could I lerp the player's movement, making them slowly stop instead of just stopping right away. This is my code right now. I am very new to c# and unity so I did watch a tutorial but made some things myself. Any help is appreciated. The print stuff is just for debugging.
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Threading;
using UnityEngine;
public class ThirdPersonMovement : MonoBehaviour
{
public CharacterController controller;
public Transform cam;
public float speed = 6;
public float gravity = -9.81f;
public float jumpHeight = 3;
Vector3 velocity;
bool isGrounded;
public Transform groundCheck;
public float groundDistance = 0.4f;
public LayerMask groundMask;
float turnSmoothVelocity;
public float turnSmoothTime = 0.1f;
// Update is called once per frame
void Update()
{
//jump
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
if (Input.GetButtonDown("Jump") && isGrounded)
{
print("jumping");
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}
if (Input.GetButtonDown("Jump"))
{
print("jump");
}
//gravity
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
//walk
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
if(direction.magnitude >= 0.1f)
{
print("walking");
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * speed * Time.deltaTime);
}
if (direction.magnitude == 0f && isGrounded == true)
{
print("idle");
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Moving forward seems to be specific to the original direction of the object being controlled 0 Answers
Dash Movement Platformer 0 Answers
Player Movement Freezes in Unity3D [Please help, no answers!] 1 Answer
SteamVR Movement based on controller direction 0 Answers
Vertical movement is greater than horizontal and diagonal movement 0 Answers