- Home /
[SOLVED] Object keeps sliding?
My object keeps sliding after transforming it but only on one axis, can someone help me find out why?
https://i.gyazo.com/9ddf7e7fedba70237cc7d956d30ff573.mp4
moving left and right makes it slide continious in the direction it was going and going up and down does not..
public class Player : MonoBehaviour
{
private float turnSpeed = 15f;
private float moveSpeed = 1f;
private Rigidbody rigidbody;
private Collider collider;
public void Awake()
{
rigidbody = GetComponent<Rigidbody>();
collider = GetComponentInChildren<Collider>();
}
private void FixedUpdate()
{
float v = Input.GetAxisRaw("Vertical");
float h = Input.GetAxisRaw("Horizontal");
if (
Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.W) ||
Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.DownArrow) || Input.GetKey(KeyCode.LeftArrow) || Input.GetKey(KeyCode.RightArrow))
{
Move(h, v);
}
}
private void Move(float horizontal, float vertical)
{
//Rotating(horizontal, vertical);
MovePlayer(horizontal, vertical);
}
void MovePlayer(float horizontal, float vertical)
{
Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
transform.position += targetDirection * moveSpeed * Time.deltaTime;
}
void Rotating(float horizontal, float vertical)
{
Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);
Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);
Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSpeed * Time.deltaTime);
rigidbody.MoveRotation(newRotation);
}
}
Answer by xainulabdeen · Nov 14, 2018 at 11:19 AM
add physics material to ground and object .sets its friction value to something like 0.6 and set the property rigidbody > interpolate to none
Answer by NightLucididty · Oct 27, 2015 at 10:54 AM
As toromano said, in your comment, Making your rigidbody kinetic should solve the problem, If you dont know where to find the option it on the inspector when you select your player, and look towards the rigidbody part there should be a bool saying is kinetic, or you could enable kinetic rigid body by code using:
public class ExampleClass : MonoBehaviour {
public Rigidbody rb;
void Start() {
rb = GetComponent<Rigidbody>();
}
void EnableRagdoll() {
rb.isKinematic = false;
rb.detectCollisions = true;
}
void DisableRagdoll() {
rb.isKinematic = true;
rb.detectCollisions = false;
}
}
I took the code from the unity scripting API, wasn't too hard to find, Good luck with your game, NightLucidity
Hey.
I've just been very stupid I didn't realise it till now but I had the character as a child of an object taht would follow it around but just a little slower making it move relative to the parented object..
I've just been stupid sorry.
Jan
Change your title to solved so others know that was your issue.
This is too try-hard. You can simply add a high amount of linear drag to rigidbody, so it will stop slowly and will not disable physics interactions.