So I am making my first 3D video game in Unity3D. I want this player gameobject with a rigidbody and a capsule collider to have collision with the environment ie the player should not be clipping through walls. I've attached images of the player's components and properties. The player has two colliders: the box collider, and the capsule collider. The box collider is disabled as of right now because if I activate the box collider instead of the capsule collider, there seems to be no collision. Aside from that, the current capsule collider looks like so. (It is the one light green line through the center of the player model)
In the scene, there is a house next to the player to which the player should collide with the walls of the house instead of clipping through. The house has 3 different box colliders on 3 sides of the rectangle building shown below. There is no collider on the far side of the image. Inspector for the box colliders on the house It is important to note that there is no rigidbody component on the house and it is set as a static game object.
Here is a gif of the issue I am talking about with the above settings. From that gif it is visible that there is collision on the z-axis of the house(both sides) but there is absolutely nothing on the x-axis as I am able to clip through the house and move inside.
Initially I thought the fact that the radius of the main player gameobject's capsule collider is 0 could be the issue however there seems to only be collision with the radius set to 0(at least with the collision script I am using which is attached below). As a result, I increased the radius to where it encompassed the entire player object. Here is an image with the inspector properties of the collider included as well. Here is the gif in play mode. From the gif any collision that was there before is completely removed and the player can now clip on all sides.
At the point, I thought it could be because I 3 different box colliders on the house and using only 1 would solve the issue. As a result, I changed the house's box collider to look like so: Even with this house box collider, the player was able to clip through all sides of the house with both settings of the capsule collider(0 radius and radius that encompasses the player's body).
The box collider yielded the same results.
Here is my collision script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class collision : MonoBehaviour
{
public bool sendTriggerMessage = false;
public LayerMask layerMask = -1; //make sure we aren't in this layer
public float skinWidth = 0.1f; //probably doesn't need to be changed
private float minimumExtent;
private float partialExtent;
private float sqrMinimumExtent;
private Vector3 previousPosition;
private Rigidbody myRigidbody;
private Collider myCollider;
//initialize values
void Start()
{
myRigidbody = GetComponent<Rigidbody>();
myCollider = GetComponent<Collider>();
previousPosition = myRigidbody.position;
minimumExtent = Mathf.Min(Mathf.Min(myCollider.bounds.extents.x, myCollider.bounds.extents.y), myCollider.bounds.extents.z);
partialExtent = minimumExtent * (1.0f - skinWidth);
sqrMinimumExtent = minimumExtent * minimumExtent;
}
void FixedUpdate()
{
//have we moved more than our minimum extent?
Vector3 movementThisStep = myRigidbody.position - previousPosition;
float movementSqrMagnitude = movementThisStep.sqrMagnitude;
if (movementSqrMagnitude > sqrMinimumExtent)
{
float movementMagnitude = Mathf.Sqrt(movementSqrMagnitude);
RaycastHit hitInfo;
//check for obstructions we might have missed
if (Physics.Raycast(previousPosition, movementThisStep, out hitInfo, movementMagnitude, layerMask.value))
{
if (!hitInfo.collider)
return;
if (hitInfo.collider.isTrigger)
hitInfo.collider.SendMessage("OnTriggerEnter", myCollider);
if (!hitInfo.collider.isTrigger)
myRigidbody.position = hitInfo.point - (movementThisStep / movementMagnitude) * partialExtent;
}
}
previousPosition = myRigidbody.position;
}
}
I am using this free animation pack to animate the player.
Not sure if this is a Unity bug because the weird thing is that the collisions work for certain houses but not for others. Sometimes theres collision on all axes and sides with a single box collider and sometimes with two different box colliders and sometimes nothing works and there is only collision on one axis.
2019.2.0b
(Beta) or2019.3.0a
(Alpha) versions ... they are for testing new features only - not stable for production and not unlikely to be full of errors and bugs ... that's the whole purpose of Beta and Alpha versions. Maybe try to stay with the latest stable version2019.1.8
and see if the error goes away. (Make Backups before switching versions) – derHugo2017
or2018
... the only thing that changed is that they ship it now with the installer and as additional Package in the PackageManager instead of having to install it via the VisualStudio installer – derHugo