- Home /
Player gets stuck into object after collision
Hi, my player gets stuck into an object when it collides with it. The player has a rigidbody and the object only has a box collider. When I move the player it has a kind of blocky movement and I want it that way but when it collides with an object it gets stuck Please help me because I'm new to unity and I've been stuck on this for weeks. (Here is the script attached to the player)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour {
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);
}
}
Answer by Cornelis-de-Jager · Jul 05, 2017 at 04:29 AM
If your are using RigidBodies on your player use:
RigidBody.Move ()
Or instead of Vector3.MoveTowards use Vector3.Lerp
transform.position = Vector3.lerp (transform.positon, transform.position + transpositionVariable * Time.deltaTime * speed, 1f);
Could you please show me the exact code I need to use because I'm new to Unity?
@Cornelis-de-Jager Could you please write the exact code I should use? I really new to Unity so I don't know much.
transform.position = Vector3.lerp(transform.position, transform.position + pos, Time.deltaTime * speed);
Answer by Alex_Randelovic · Jul 19, 2017 at 10:19 AM
@Cornelis-de-Jager I have a new question could you please answer it? Check my account
Your answer
Follow this Question
Related Questions
Weird Collisions Between Paddle and Ball (Breakout) 0 Answers
Unity3D: Get Velocity after collision 0 Answers
Sider Joint 2D in 3D 0 Answers
Player getting stuck in ground (3D) player has Rigidbody, and Box Collider, world is Mesh Colliders 0 Answers
How to stop sphere from clipping through cube edges aside from lowering Time.FixedDeltaTime? 1 Answer