- Home /
RigidBody clips into corners
Hello! I'm trying to make a simple character controller for my main character, but I'm having a problem where my character's rigidbody will simple clip through any corners I run into. On top of this, if the character is falling, but presses into a wall, they will stop falling for a second. Here is the script I am using. Any help is greatly appreciated!
using UnityEngine;
using System.Collections;
public class SimpleCharControl : MonoBehaviour {
public float moveSpeed;
// Use this for initialization
void Start ()
{
moveSpeed = 5f;
}
// Update is called once per frame
void FixedUpdate () {
transform.Translate(moveSpeed*Input.GetAxis("Horizontal")*Time.deltaTime,0f,moveSpeed*Input.GetAxis("Vertical")*Time.deltaTime);
}
}
Answer by ray2yar · Jan 29, 2019 at 01:51 AM
I think this is because of how you're using translate.
The Collider will generate some friction against the walls and stutter and or stop in some cases. A quick work around is to make the walls slippery with a physics material.
Answer by Cornelis-de-Jager · Jan 29, 2019 at 09:58 PM
There are two things that you can do that will give you better results when moving. They are two different things and can't be used together.
Using Linear Interpolation To Move
// Instead of FixedUpdate use LasteUpdate (the physics update)
void LateUpdate () {
/* Get the direction we should move in */
var x = Input.GetAxis("Horizontal"); // horizontal input
var z = Input.GetAxis("Vertical"); // vertical input
var move = new Vector3(x, 0 ,z); // new Move Direction
/* Do linear interpolation on the characters position */
var currentPos = transform.position;
var lerpPos = transform.position + move * moveSpeed * Time.DeltaTime;
transform.position = Vector3.Lerp (transform.position, lerpPos, 1f);
}
Using Rigidbody Movement This is probally the best way to move to ensure there is no clipping. But it becomes harder to controll.
Answer by Karbonic · Feb 04, 2019 at 08:29 PM
I tried Linear Interpolation, but now the character is clipping through all walls, not just corners. Do you have any ideas as to why this could be happening? The character has a capsule collider.