- Home /
How to add Countermovement?
Hi, In the moment I am making a very simple 3D platformer with a Rigidbody 3D. And I want to add Countermovement to get a much more snappy movement. How can I do that?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player3DMovement : MonoBehaviour
{
private Rigidbody rb;
public float speed;
public float maxSpeed;
public float counterMovement;
private float applySpeed;
Vector3 move;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
if(rb.velocity.magnitude > maxSpeed)
{
rb.velocity = Vector3.ClampMagnitude(rb.velocity, maxSpeed);
}
else
{
rb.AddForce(move * speed);
}
}
}
Answer by N-8-D-e-v · Aug 21, 2020 at 03:16 AM
There are a couple ways you could do this
First off, you could just have 0 velocity on each axis when not pressing certain keys, this will get you incredibly snappy movement
if (Input.GetButtonUp("Horizontal")
{
rb.velocity = new Vector3(0, rb.velocity.y, rb.velocity.z);
}
if (Input.GetButtonUp("Vertical")
{
rb.velocity = new Vector3(rb.velocity.x, rb.velocity.y, 0);
}
Or, you could do some (somewhat) simple trig math and calculate the direction the player is facing with cosine and sine https://www.youtube.com/watch?v=-dGi2Ffdiuk (this tutorial will help with that, but will not have the scripts you are looking for.) and then just add force in the opposite direction the player is travelling in.
Possibly the simplest method (yes even simpler than the first one) Would be to apply a physics material to the ground that has a high amount of friction
I hope this helps you, feel free to ask any questions if you are confused and want more help!
Your answer
Follow this Question
Related Questions
Animation 101: How does an Animation component work with a hierarchy? 0 Answers
Animation event triggering step on frame 0 Answers
How do I edit this to make my character move based on the Camera Axis instead of World Axis? 0 Answers
3rd person controller using GUI.button Windows Standalone 0 Answers