MovementSystem.CollisionChecker - robblofield/TomeboundDocs GitHub Wiki
Action.CollisionChecker
Author: Youngju Yun
Last Updated: 01/01/2024
Overview
This script contains its collision-related variables, controls the raycast to check collision with any or specific object, and gets data on the collided objects. It forms part of the "MovementSystem".
Dependencies
Contents
- Breakdown of code
- Variables
- CheckCollidingObject()
- Full Code Reference
Breakdown of code
Variables
public float rayLength = 1.25f;
public GameObject collidingObject;
public GameObject grabbingObject;
public bool ignoreCollider = false;
public bool ignorePlayer = false;
public bool ignoreMovable = false;
public float rayLength: This is the length of the ray that is cast during Physics.Raycast() to determine whether a collision will happen in the requested direction.
public GameObject collidingObject: This stores the reference of the last GameObject that collided with its raycast.
public GameObject grabbingObject: This stores the reference of the GameObject that the target object is grabbing now.
public bool ignoreCollider This boolean decides whether or not the target object can phase through any collider.
public bool ignoreCollider This boolean decides whether the target object can phase through any collider with the tag "Player".
public bool ignoreMovable This boolean decides whether the target object can phase through any collider with the tag "Movable" and "Block".
public bool CheckCollidingObject()
This is a public boolean method that spawns raycast from the target object to a specific direction and gets a reference of a collided object if it has collided. Now it can also look for the GameObject with a specific Name and Tag as the grabbingObject.
N.B - This method is called outside of this script.
public bool CheckCollidingObject(Transform trans, Vector3 direction, string grabObjName = null, string grabObjTag = null){
RaycastHit hit = new RaycastHit();
Debug.DrawRay(trans.position, direction * rayLength, Color.green);
if (Physics.Raycast(trans.position, direction, out hit, rayLength))
{
Debug.DrawRay(trans.position, direction * rayLength, Color.red);
collidingObject = hit.collider.gameObject;
Debug.Log(hit.collider.gameObject);
if(grabObjName != null || grabObjTag != null){
if(hit.collider.gameObject.name == grabObjName || hit.collider.gameObject.tag == grabObjTag){
grabbingObject = hit.collider.gameObject;
return true;
}
}else{
if(ignoreCollider){
return false;
}
if(ignorePlayer){
if(hit.collider.tag == "Player") return false;
}
if (ignoreMovable){
if(hit.collider.tag == "Movable" || hit.collider.tag == "Block") return false;
}
return true;
}
}
return false;
}
CheckCollidingObject() Raycasthit
RaycastHit hit = new RaycastHit();
Debug.DrawRay(trans.position, direction * rayLength, Color.green);
*RaycastHit is a structure used to get information back from a raycast. *Also it draws a green raycast line that is only visible in the Unity editor menu.
CheckCollidingObject() Physics.Raycast
if (Physics.Raycast(trans.position, direction, out hit, rayLength))
{
Debug.DrawRay(trans.position, direction * rayLength, Color.red);
collidingObject = hit.collider.gameObject;
Debug.Log(hit.collider.gameObject);
if(grabObjName != null || grabObjTag != null){
if(hit.collider.gameObject.name == grabObjName || hit.collider.gameObject.tag == grabObjTag){
grabbingObject = hit.collider.gameObject;
return true;
}
This block code is covered within the if statement to ensure that the raycast has collided with any objects with a collider.
If it collides with any objects, the Debug line color becomes red.
Then the collidingObject is set as an object of the RaycastHit.
If the parameter grabObjName or grabObjTag is not null and if the RaycastHit object's name is equal to the grabObjName or its tag is equal to the grabObjTag, the grabbingObject is set as an object of the RaycastHit.
CheckCollidingObject() Ignore variables
}else{
if(ignoreCollider){
return false;
}
if(ignorePlayer){
if(hit.collider.tag == "Player") return false;
}
if (ignoreMovable){
if(hit.collider.tag == "Movable" || hit.collider.tag == "Block") return false;
}
return true;
}
If the boolean ignoreCollider is set to true, the method returns false.
Other than that, if the boolean ignorePlayer is set to true and the tag of the RaycastHit object is "Player", the method also returns false.
Furthermore, the method returns false if ignoreMovable is true and the tag of it is "Movavle" or "Block".
CheckCollidingObject() End method
return false;
}
It returns false if the method hasn't abrupted yet because it needs to return a boolean value in the last.
Full Code Reference
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CollisionChecker : MonoBehaviour
{
public float rayLength = 1.25f;
public GameObject collidingObject;
public GameObject grabbingObject;
public bool ignoreCollider = false;
public bool ignorePlayer = false;
public bool ignoreMovable = false;
public bool CheckCollidingObject(Transform trans, Vector3 direction, string grabObjName = null, string grabObjTag = null){
RaycastHit hit = new RaycastHit();
Debug.DrawRay(trans.position, direction * rayLength, Color.green);
if (Physics.Raycast(trans.position, direction, out hit, rayLength))
{
Debug.DrawRay(trans.position, direction * rayLength, Color.red);
collidingObject = hit.collider.gameObject;
Debug.Log(hit.collider.gameObject);
if(grabObjName != null || grabObjTag != null){
if(hit.collider.gameObject.name == grabObjName || hit.collider.gameObject.tag == grabObjTag){
grabbingObject = hit.collider.gameObject;
return true;
}
}else{
if(ignoreCollider){
return false;
}
if(ignorePlayer){
if(hit.collider.tag == "Player") return false;
}
if (ignoreMovable){
if(hit.collider.tag == "Movable" || hit.collider.tag == "Block") return false;
}
return true;
}
}
return false;
}
}