- Home /
How can i get my player to slow down when i stop moving?
My code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))] public class PlayerController : MonoBehaviour {
public Rigidbody Player;
public Camera Camera;
public Vector3 jump;
public float jumpForce = 2.0f;
public bool isGrounded;
Rigidbody rb;
void Start(){
rb = GetComponent<Rigidbody>();
jump = new Vector3(0.0f, 2.0f, 0.0f);
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
void OnCollisionStay(){
isGrounded = true;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey("d"))
{
Player.AddRelativeForce(4, 0, 0);
}
if (Input.GetKey("a"))
{
Player.AddRelativeForce(-4, 0, 0);
}
if (Input.GetKey("w"))
{
Player.AddRelativeForce(0, 0, 4);
}
if (Input.GetKey("s"))
{
Player.AddRelativeForce(0, 0, -4);
}
if (Input.GetKeyDown(KeyCode.Space) && isGrounded){
rb.AddForce(jump * jumpForce, ForceMode.Impulse);
isGrounded = false;
} } }
Answer by DinoMax0112 · Nov 30, 2020 at 02:41 PM
Hello, I hope this helps.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float speed = 10.0f;
public Rigidbody rb;
public Vector2 movement;
// Use this for initialization
void Start()
{
rb = this.GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
movement = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));
}
void FixedUpdate()
{
moveCharacter(movement);
}
void moveCharacter(Vector2 direction)
{
rb.MovePosition((Vector2)transform.position + (direction * speed * Time.deltaTime));
}
if (Input.GetKeyDown(KeyCode.Space) && isGrounded){
rb.AddForce(jump * jumpForce, ForceMode.Impulse);
isGrounded = false;
}
}
What this is doing instead of add force is it is moving the rigid body instead of the rigid body getting a separate source and having to wait to slow down.
If you have any problems with it leave a coment and I'll come back to troubleshoot.
Assets\PlayerController.cs(36,2): error CS1022: Type or namespace definition, or end-of-file expected
Assets\PlayerController.cs(34,27): error CS1519: Invalid token '=' in class, struct, or interface member declaration
Assets\PlayerController.cs(33,64): error CS1519: Invalid token ';' in class, struct, or interface member declaration
Assets\PlayerController.cs(33,27): error CS1519: Invalid token '(' in class, struct, or interface member declaration
Assets\PlayerController.cs(31,52): error CS1519: Invalid token ')' in class, struct, or interface member declaration
Assets\PlayerController.cs(31,39): error CS1519: Invalid token '&&' in class, struct, or interface member declaration
Assets\PlayerController.cs(31,37): error CS8124: Tuple must contain at least two elements.
Assets\PlayerController.cs(31,23): error CS1026: ) expected
Assets\PlayerController.cs(31,23): error CS8124: Tuple must contain at least two elements.
Assets\PlayerController.cs(31,3): error CS1519: Invalid token 'if' in class, struct, or interface member declaration
Answer by CmdrZin · Dec 05, 2020 at 08:40 PM
Two possible ways.
1. Increase the Drag parameter for the Player RigidBody in the Inspector to 0.8 or so to add drag.
2. Simplify your movement control like this.
// Player movement control
void FixedUpdate()
{
Vector3 currVelocity = playerRb.velocity;
velocity.y = Input.GetAxis("Vertical") * speed * Time.deltaTime;
velocity.x = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
// transform.Translate(velocity.x, 0, velocity.y);
playerRb.AddRelativeForce(new Vector3(velocity.x, 0f, velocity.y) * speed);
if(playerRb.velocity.magnitude > 1)
{
// Don't modify.
playerRb.velocity = currVelocity;
}
}