- Home /
How to create cube collision from the inside?
I want to create a cube-shaped arena with the player inside of it. To that end I've done the following:
1. Stuck six planes together, each having a Box Collider facing outwards.
2. Created a sphere which will serve as the player. The player has a Sphere Collider, a Rigidbody (non-kinematic, no gravity), and has the main camera attached to him (attached below is the only script attached to the player).
I have the following problems:
1. If I hit just one wall head-on I have a little bit of a bounce-back, which I'd like to eliminate. 2. If I hit a corner where two walls connect then the player seemingly starts to roll away from one of the walls as if it's bouncing off of it pretty hard. This was very hard to diagnose since it seemed at first as if the player started making random movements.
Things I've tried:
1. Freezing the rotations of the player's Rigidbody. This helps with the rotation part, but the player still "rolls away", just without spinning.
2. Freezing the positions of the player's Rigidbody. Now the player just passes through the colliders, I guess collision is dependent on enabled movement in the Rigidbody component. 3. Changing the colliders of the walls. I've tried resizing their Box Colliders, and I've tried using Mesh Colliders instead. They all yield the same result.
4. Implementing movement using Rigidbody.MovePosition instead of transform.Translate. Again, same result.
5. Using a Physics Material with 0 bounciness. Again, no help.
Any help would be greatly appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float m_MoveSpeed;
public float m_TurnSpeed;
// Start is called before the first frame update
void Start()
{
m_MoveSpeed = 10f;
m_TurnSpeed = 100f;
}
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal");
float y = Input.GetAxis("Vertical");
transform.Translate(Vector3.forward * y * m_MoveSpeed * Time.deltaTime);
transform.Rotate(Vector3.up * x * m_TurnSpeed * Time.deltaTime);
}
}