- Home /
Translating Player Movement and Clipping Through Walls
Hello!
The Problem: I've been recently working on a pet project that involves the player moving in a grid-like manner. Using the normal forces method did not bring good results, and I landed on simply translating the player instead. For some reason, despite the fact that both the player and the environmental elements have Rigidbodies, they don't interact at all. My player just clips straight through the wall. I've provided a couple pictures to show this, as well as my code.
The Goal: allowing my player to move as-is, and be able to detect when they would collide with a wall. If they would collide with said wall, then they will move a set distance (let's say 0.5 units), then revert back to their original position. That way, there's visual feedback for the player to tell them that they can not collide with that wall.
Things I vaguely understand: I've seen other posts mention setting RB collision detection to "continuous dynamic", checking to make sure I have the proper colliders on both objects, editing the colliders to be wider, etc. These methods have not worked in my specific use case, and I predict that this is due to not using forces to move my player. I've seen one tutorial on Youtube mention raycasting, but that video's specific method did not work out.
Would greatly appreciate some answers/guides! Thank you! :)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class Movement : MonoBehaviour
{
public GameObject Player;
Vector3 up = Vector3.zero;
Vector3 nextPos, destination, direction;
Vector3 currentDirection = Vector3.zero;
public float Speed = 5f;
bool canMove;
private void Start()
{
currentDirection = up;
nextPos = Vector3.forward;
destination = transform.position;
}
// Update is called once per frame
void Update()
{
// PLAYER MOVEMENT IS HERE
if (Input.GetKeyDown(KeyCode.W))
{
transform.position += transform.forward; //MOVE FORWARD
}
else if (Input.GetKeyDown(KeyCode.S))
{
transform.position -= transform.forward; //MOVE BACKWARD
}
else if (Input.GetKeyDown(KeyCode.A))
{
transform.position -= transform.right; //STRAFE LEFT
}
else if (Input.GetKeyDown(KeyCode.D))
{
transform.position += transform.right; //STRAFE RIGHT
}
// THESE CONTROLS DETERMINE WHICH DIRECTION THE PLAYER IS FACING; CAMERA IS CURRENTLY A CHILD OF THE PLAYER, SO IT ALSO MOVES ACCORDING TO THESE CONTROLS
if (Input.GetKeyDown("q"))
{
Player.transform.rotation *= Quaternion.Euler(0, -90, 0); //TURN CAMERA/PLAYER LEFT
}
if (Input.GetKeyDown("e"))
{
Player.transform.rotation *= Quaternion.Euler(0, 90, 0); //
}
}
}
Answer by GunMetalGames · Aug 01, 2020 at 10:26 PM
So, for anyone who finds this in the future, here is what I ended up doing (for the time being). The person above, while not entirely helpful, at least gave me a decent place to start. If transform.translate doesn't interact with colliders, then you have to use a Rigidbody. I ended up working with Vector 3, because using rb.MovePosition didn't operate within local space (forward was always the same direction, no matter which way the player was facing). Here's my current code. Be aware that the longer it's been since this comment was made, the less likely it is that I'm still using it for my purposes.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public GameObject player;
public float speed = 0.5f; //this variable tells Unity how many units we want to move per input. In this case, we move half of 1 unit.
public float rotationSpeed = 100.0f;
private Rigidbody rb; //establishes a variable for this code, "rb" for "RigidBody"
private void Start()
{
rb = GetComponent<Rigidbody>(); //tells Unity that "rb" means we're using the RigidBody component
}
void Update()
{
// PLAYER MOVEMENT IS HERE
Vector3 move = Vector3.zero;
if (Input.GetKeyDown(KeyCode.W)) //move forward
{
move = transform.forward * speed;
}
else if (Input.GetKeyDown(KeyCode.S)) //move backward
{
move = -transform.forward * speed;
}
if (Input.GetKeyDown(KeyCode.A)) //move left
{
move = -transform.right * speed;
}
else if (Input.GetKeyDown(KeyCode.D)) //move right
{
move = transform.right * speed;
}
//rb.MovePosition(transform.position + move); (this comment is just me keeping a copy of the rb.MovePosition command, in case I need it later.)
transform.position += move; //STRAFE RIGHT
if (Input.GetKeyDown("q"))
{
transform.Rotate(0f, -90f, 0f);
}
if (Input.GetKeyDown("e"))
{
transform.Rotate(0f, 90f, 0f);
}
}
}
Answer by itstimetomakelol · Jul 30, 2020 at 03:34 PM
transform.translate doesn't interact with colliders. Instead use rb.MovePosition or rb.AddForce.
So, I've tried using rb.$$anonymous$$ovePosition, and it still doesn't work. Collision is still not occurring between the wall and my player object.