Hey there everyone, I’m trying to work on a simple script for movement. However I am running into an issue where when I move the character forward, they move but when you turn the other direction and try to move that way, it slides in the previous direction I was moving and only stops a few seconds after before moving the other way. I kind of have an idea of what it could be but I’m not exactly sure how to get around this. I am mainly a 3d artist but since I’ve done some programming before I took the task to go ahead and code basic movement for the moment before recruiting a programmer later.
To sum it all up, I just want to know how I can get rid of this sliding or minimize it. I guess I can do that through some of the parameters like the linear damping but I think it might be the acceleration that makes it slide faster regardless. Perhaps its better for me to go with a Kinematic body instead to just control everything through my own gravity? The game is suppose to be a 2.5d side scroller view, action game so there is only moving left, right, and up when I try to implement the jump later. My assumptions on what might be the culprit is either something to do with my moveSpeed variable or it’s just the rigidbody has acceleration and it just doesn’t stop sooner. I don’t mind a tiny bit of sliding for some realism but I rather the character be at a constant speed when running versus it changing over time. I’m also not using a character controller, doing this all through code since the controller has changed alot over the years and it’s a bit more complicated.
CODE
Note: This is the movement script I have attached to the rigidbody character. I use a input manager instance to determine if right or left is pressed and to do the specified actions. The IM is another script and I have a keyboard input script to where it will trigger if the IM is on or off to move right/left via A or D keys pressed. Any help is appreciated and I’m sorry for such a shallow post, I hope to just get things running and later I can focus on my art to hopefully allow us to gather interest to get a programmer later.
Movement Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace inputTower
{
public class MovementCharacter : MonoBehaviour
{
public Animator anim;
public Rigidbody rb;
private int moveSpeed;
private Vector3 velocity;
void Start()
{
anim = gameObject.GetComponent<Animator>();
rb = GetComponent<Rigidbody>();
moveSpeed = 1;
}
// Update is called once per frame
void FixedUpdate()
{
if(VirtualInputManagement.Instance.moveRight && VirtualInputManagement.Instance.moveLeft)
{
rb.velocity = Vector3.zero;
anim.Play("Idle");
return;
}
if (VirtualInputManagement.Instance.moveRight)
{
velocity += transform.forward * moveSpeed * Time.fixedDeltaTime;
rb.velocity = velocity;
//this.gameObject.transform.Translate(Vector3.forward * .8f * Time.deltaTime);
this.gameObject.transform.rotation = Quaternion.Euler(0f, 0f, 0f);
anim.Play("RunCycle");
if(moveSpeed > 2)
{
moveSpeed = 1;
}
}
if (VirtualInputManagement.Instance.moveLeft)
{
velocity -= transform.forward * -1 * moveSpeed * Time.fixedDeltaTime;
rb.velocity = velocity;
//this.gameObject.transform.Translate(Vector3.forward * .8f * Time.deltaTime);
this.gameObject.transform.rotation = Quaternion.Euler(0f, 180f, 0f);
anim.Play("RunCycle");
if (moveSpeed > 2)
{
moveSpeed = 1;
}
}
if (!VirtualInputManagement.Instance.moveRight && !VirtualInputManagement.Instance.moveLeft)
{
rb.velocity = Vector3.zero;
anim.Play("Idle");
}
}
}
}