- Home /
Question by
romans8710 · Mar 30, 2020 at 04:47 PM ·
movement3dfpscharacterscripting beginner
How do I stop my character from sliding after adding force?
I'm trying to move a character by adding force, however, after I stop pressing the button that moves him, he slides for a bit, how would I stop him from doing this? (I would like to continue to move him by adding force as I want the character to be able to slide when he crouches.)
here is my script: using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMovement : MonoBehaviour { public float speed = 1.5f; public Rigidbody rb; public float jumpforce = 1f; public bool isGrounded = true; public float maxspeed = 50f; public float counterMovement = 1f;
void FixedUpdate()
{
WalkRun();
jump();
}
void WalkRun()
{
float hAxis = Input.GetAxisRaw("Horizontal");
float vAxis = Input.GetAxisRaw("Vertical");
if(rb.velocity.x < maxspeed && rb.velocity.z < maxspeed)
{
rb.AddForce(transform.forward * vAxis * speed);
rb.AddForce(transform.right * hAxis * speed);
}
}
private void jump()
{
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(0, jumpforce, 0, ForceMode.Impulse);
isGrounded = false;
}
}
private void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "Ground")
{
isGrounded = true;
}
}
}
Comment