- Home /
Player gets stuck into object when it collides with it
Hi, when I my player collides with an object it gets stuck and has a kind of jittery movement. The player does not have normal movement, it has a kind of blocky movement and I want it that way. The player has a rigidbody and a box collider the object only has a box collider. Please write the exact code I should use because I'm new to Unity. Here is my code below which is attached to the player \/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
[SerializeField]
float speed = 2.0f;
Vector3 pos;
Transform tr;
void Start() {
pos = transform.position;
tr = transform;
}
void Update() {
if (Input.GetKey(KeyCode.D) && tr.position == pos)
{
pos += Vector3.back;
}
else if (Input.GetKey(KeyCode.A) && tr.position == pos)
{
pos += Vector3.forward;
}
else if (Input.GetKey(KeyCode.W) && tr.position == pos)
{
pos += Vector3.right;
}
else if (Input.GetKey(KeyCode.S) && tr.position == pos)
{
pos += Vector3.left;
}
transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * speed);
}
}
Your answer
Follow this Question
Related Questions
(RigidBody) Velocity at an Angle 0 Answers
Why is my character controller moving in unrelated directions? 1 Answer
Player not affected by moving platform? 0 Answers
Player getting stuck in ground (3D) player has Rigidbody, and Box Collider, world is Mesh Colliders 0 Answers
How to Have Two Child Objects of One Parent Detect Collisions Between Eachother. 1 Answer