Action_Grab - robblofield/TomeboundDocs GitHub Wiki
This script handles the character's action to grab nearby objects, move along with them, and release the object. It forms part of the "Action".
- GridBasedMovement1.cs
- CollisionChecker.cs
- Player.cs
- Action_Abilities.cs
- GameObject with either of tags, "Book", "Movable", or "Block"
- Use case
- Breakdown of code
- Using Statements
- Variables
- GrabObject()
- ReleaseObject()
- Future Expansion
- Full Code Reference
The script is currently used when the player character tries to grab the Tome Object or when the Wizard's Drones wants to grab a Movable Object. The script is not a standalone and the function is run from scripts including Player.cs and Action_Abilities.cs.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public bool isGrabbingObject = false;
private bool canGrabObject = false;
public GridBasedMovement1 movement;
public CollisionChecker collision;
bool isGrabbingObject: A boolean is set to true when the character is grabbing the object and is set to false when releasing the object.
bool canGrabObject: Determines whether the character is capable of grabbing an object. It is not referenced in the current build.
GridBasedMovement1 movement: A data which stores the GridBasedMovement1.cs component of the character.
CollisionChecker collision: A data which stores the CollisionChecker.cs component of the character.
public void GrabObject(string objectName = null, Vector3 direction =
default(Vector3), string objectTag = null, Transform customTransform = null)
{
if(customTransform == null){
customTransform = transform;
}
if(!movement.isMoving){
//Grabbing Object
if(!isGrabbingObject){
//Look for one specific direction
if(direction != default(Vector3)){
if(collision.CheckCollidingObject(customTransform, direction, objectName, objectTag)){
Debug.Log(collision.collidingObject.tag);
movement.allTransforms.Add(collision.collidingObject.transform);
}
}//Look for NESW directions
else if(collision.CheckCollidingObject(customTransform, Vector3.forward, objectName) || collision.CheckCollidingObject(customTransform, Vector3.back, objectName) || collision.CheckCollidingObject(customTransform, Vector3.left, objectName) || collision.CheckCollidingObject(customTransform, Vector3.right, objectName))
{
StartCoroutine(movement.TurnObject(collision.collidingObject.transform.position));
movement.allTransforms.Add(collision.collidingObject.transform);
Debug.Log(collision.collidingObject.tag);
//If the Grabbing Object was the book
if(collision.grabbingObject.tag == "Book"){
if (movement.xLocked && !movement.yLocked)
{
collision.grabbingObject.GetComponent<Tome>().SetDistanceBetweenWizY(gameObject.transform);
}
else if (movement.yLocked && !movement.xLocked)
{
collision.grabbingObject.GetComponent<Tome>().SetDistanceBetweenWizX(gameObject.transform);
}
}
}
if(collision.grabbingObject != null){
isGrabbingObject = true;
}
}
}
}
The function handles everything about the character grabbing the specific object.
public void GrabObject(string objectName = null, Vector3 direction = default(Vector3), string objectTag = null, Transform customTransform = null)
{
if(customTransform == null){
customTransform = transform;
}
It has 4 parameters to set in order to run the function.
String objectName is used to find GameObject with the same name. The default value is null.
Vector3 direction is set when the single RayCast in a specific direction is only required for the object to grab. If the direction remains unchanged from the default value, It casts rays in 4 directions, forward, left, right, and backward.
String objectTag is used to find GameObjects with the same tag. The default value is null.
Transform customTransform is used to customize the origin of RayCast. if it remains to be the default value of null, it takes data of the character's transform.
if(!movement.isMoving){
//Grabbing Object
if(!isGrabbingObject){
//Look for one specific direction
if(direction != default(Vector3)){
if(collision.CheckCollidingObject(customTransform, direction, objectName, objectTag)){
Debug.Log(collision.collidingObject.tag);
movement.allTransforms.Add(collision.collidingObject.transform);
}
}//Look for NESW directions
else if(collision.CheckCollidingObject(customTransform, Vector3.forward, objectName) || collision.CheckCollidingObject(customTransform, Vector3.back, objectName) || collision.CheckCollidingObject(customTransform, Vector3.left, objectName) || collision.CheckCollidingObject(customTransform, Vector3.right, objectName))
{
StartCoroutine(movement.TurnObject(collision.collidingObject.transform.position));
movement.allTransforms.Add(collision.collidingObject.transform);
Debug.Log(collision.collidingObject.tag);
This section of the function won't run unless the character is not moving. It requests its CollisionChecker.cs component to RayCast in specific directions to look for the object to grab. The value of the parameter of the function CheckCollidingObject is inherited by the value of this function's parameter.
If a ray has collided with the object to grab, it adds transform data of the colliding object into the array of transforms to sync the movement with the character in GridBasedMovement1.cs.
If the direction parameter is set to a custom value, it casts a single ray in the direction.
If the parameter is not set, it casts a ray in every 4 directions. If a ray detects the object it starts the subroutine TurnObject to horizontally rotate the character towards the object.
//If the Grabbing Object was the book
if(collision.grabbingObject.tag == "Book"){
if (movement.xLocked && !movement.yLocked)
{
collision.grabbingObject.GetComponent<Tome>().SetDistanceBetweenWizY(gameObject.transform);
}
else if (movement.yLocked && !movement.xLocked)
{
collision.grabbingObject.GetComponent<Tome>().SetDistanceBetweenWizX(gameObject.transform);
}
}
This segment of the function runs when the tag of grabbingObject is "Book", which means the character is carrying the Tome.
If the character is WizardY or WizardX, it gets the component Tome.cs of the Tome and sets the distance between the character and the Tome.
if(collision.grabbingObject != null){
isGrabbingObject = true;
}
It's quite self-explanatory, isn't it?
public void ReleaseObject(){
if(!movement.isMoving){
if(isGrabbingObject){
//Releasing Object
isGrabbingObject = false;
movement.movementLocked = true;
movement.allTransforms = new List<Transform> {transform};
Debug.Log("Released");
//If the Grabbing Object was the book
if(collision.grabbingObject.tag == "Book"){
collision.grabbingObject.GetComponent<Tome>().Released(transform);
}
}
}
}
This function is called when the character wants to release an object which it is grabbing.
The character cannot release an object while moving.
A boolean isGrabbingObject is set to false.
The array allTransforms in GridBasedMovement1.cs is cleared except for the transform data of itself.
If the grabbing object was the Tome, It requests the Tome to run the special function Released with the parameter value of its transform value.
This version of Action_Grab.cs contains all the functions required to work properly on any characters Regardless of the player interactions. No future expansion has been planned so far.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Action_Grab : MonoBehaviour
{
//This script handles basic grab action ONLY. No other actions go here.
public bool isGrabbingObject = false;
private bool canGrabObject = false;
public GridBasedMovement1 movement;
public CollisionChecker collision;
public void GrabObject(string objectName = null, Vector3 direction = default(Vector3), string objectTag = null, Transform customTransform = null)
{
if(customTransform == null){
customTransform = transform;
}
if(!movement.isMoving){
//Grabbing Object
if(!isGrabbingObject){
//Look for one specific direction
if(direction != default(Vector3)){
if(collision.CheckCollidingObject(customTransform, direction, objectName, objectTag)){
Debug.Log(collision.collidingObject.tag);
movement.allTransforms.Add(collision.collidingObject.transform);
}
}//Look for NESW directions
else if(collision.CheckCollidingObject(customTransform, Vector3.forward, objectName) || collision.CheckCollidingObject(customTransform, Vector3.back, objectName) || collision.CheckCollidingObject(customTransform, Vector3.left, objectName) || collision.CheckCollidingObject(customTransform, Vector3.right, objectName))
{
StartCoroutine(movement.TurnObject(collision.collidingObject.transform.position));
movement.allTransforms.Add(collision.collidingObject.transform);
Debug.Log(collision.collidingObject.tag);
//If the Grabbing Object was the book
if(collision.grabbingObject.tag == "Book"){
if (movement.xLocked && !movement.yLocked)
{
collision.grabbingObject.GetComponent<Tome>().SetDistanceBetweenWizY(gameObject.transform);
}
else if (movement.yLocked && !movement.xLocked)
{
collision.grabbingObject.GetComponent<Tome>().SetDistanceBetweenWizX(gameObject.transform);
}
}
}
if(collision.grabbingObject != null){
isGrabbingObject = true;
}
}
}
}
public void ReleaseObject(){
if(!movement.isMoving){
if(isGrabbingObject){
//Releasing Object
isGrabbingObject = false;
movement.movementLocked = true;
movement.allTransforms = new List<Transform> {transform};
Debug.Log("Released");
//If the Grabbing Object was the book
if(collision.grabbingObject.tag == "Book"){
collision.grabbingObject.GetComponent<Tome>().Released(transform);
}
}
}
}
}