- Home /
Manual Collision Detection through triggers...
Here's my movement script, it moves in directions by 1 unit. I tried to implement bools that allow movement in the four directions, but I came across an issue... As of now, my character does collide with an object tagged as "wall" when facing it... That is because there is a trigger in front of the character that detects when a wall is directly forward as its OnTriggerEnter function is run. This works to an extent. If I were to use my method, I would need four different triggers for all sides of the character (a cube). They would ideally be placed as children to the player in the Hierarchy, and so I would need to know how I can access a child's component (the box collider, and the child being an empty preferably.)
Now, I can either carry out with my approach with some help from the community, or someone can suggest a better way if there is any. Both would be extremely appreciated.
Here is my movement script that has the manual collision detection:
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public bool CanMoveForward;
public bool CanMoveBackward;
public bool CanMoveLeft;
public bool CanMoveRight;
//Translation:
float movSpeed = 4.0f;
Vector3 pos;
Transform tr ;
bool moving = false;
//Rotation:
bool rotating = false;
public float rotSpeed = 360f;
float rotDegrees = 0f;
Quaternion rotToAngle ;
void Start () {
pos = transform.position;
tr = transform;
CanMoveForward = true;
CanMoveBackward = true;
CanMoveLeft = true;
CanMoveRight = true;
}
// Update is called once per frame
void Update () {
Debug.DrawRay(transform.position, transform.forward, Color.red);
//Input:
if (!moving && !rotating) {
if (Input.GetKey(KeyCode.D) && tr.position == pos && CanMoveRight) {
//pos += Vector3.right;
pos += transform.right;
moving=true;
// print ("MOVE LEFT");
} else if (Input.GetKey(KeyCode.A) && tr.position == pos && CanMoveLeft) {
pos += -transform.right;
moving=true;
// print ("MOVE RIGHT");
} else if (Input.GetKey(KeyCode.W) && tr.position == pos && CanMoveForward) {
pos += transform.forward;
moving=true;
// print ("MOVE FORWARD");
} else if (Input.GetKey(KeyCode.S) && tr.position == pos && CanMoveBackward) {
pos += -transform.forward;
moving=true;
// print ("MOVE BACK");
} else if (Input.GetKey(KeyCode.Q) && tr.position == pos) {
rotDegrees -= 90f;
//rotToAngle = Quaternion.Euler(0, rotDegrees, 0);
rotToAngle = Quaternion.Euler(0, rotDegrees, 0);
rotating = true;
// print ("TURN LEFT");
} else if (Input.GetKey(KeyCode.E) && tr.position == pos) {
rotDegrees += 90f;
//rotToAngle = Quaternion.Euler(0, rotDegrees, 0);
rotToAngle = Quaternion.Euler(0, rotDegrees, 0);
rotating = true;
// print ("TURN RIGHT");
}
}
//Translation:
if (moving) {
if (Vector3.Distance(transform.position,pos) <0.05f){
transform.position = pos;
moving=false;
// print ("FINISHED MOVE");
} else {
transform.position = Vector3.MoveTowards(transform.position, pos, Time.deltaTime * movSpeed);
}
}
//Rotation:
if (rotating) {
if (Quaternion.Angle(transform.rotation,rotToAngle) <10f) {
transform.rotation = rotToAngle;
rotating=false;
// print ("FINISHED TURN");
} else {
transform.rotation = Quaternion.RotateTowards(transform.rotation, rotToAngle, rotSpeed * Time.deltaTime);
}
}
}
void OnTriggerEnter(Collider other) {
if(other.CompareTag("wall")) {
Debug.Log("Collided with 'wall'");
CanMoveForward = false;
}
}
void OnTriggerExit(Collider other) {
CanMoveForward = true;
}
}
Thanks in advance! :D
Note: The only one working is the frontal collision as a GameObject can only have one collider/trigger that is accessed by a script independently. What I want to do is have children with four different colliders. :D
mm... I'd make one big trigger around the player and whenever it collides with something I'd take the relative position of the obstacle to see which way I can move
and what is the problem? make a child for every direction and attach trigger to each of them... to acces them use GetComponentsInChildren and check some publics to detech what direction this trigger for.
or make public fields for every direction and drag-drop children with triggers to them.
or you can do as @taxvi said :)