How to investigate an object in front of the player
I am trying to use transform.forward
to detect the front object but there is a problem. Since transform.forward
can only check the GameObject’s position on the Z axis (blue axis) of the transform, the object cannot be detected when it is higher or lower than the blue arrow. So, is there any ways to detect the front side of the player exactly? I hope you can help me, thanks!
public void DetectThings() {
RaycastHit hit;
if (Physics.Raycast (playerTrans.position, playerTrans.forward, out hit, InvestigateDistance)) {
if (hit.collider.tag == "Portable") {
Transform PICKUpTrans = playerTrans.gameObject.GetComponent<PlayerController> ().PickUpPos;
GameObject GOTit = Instantiate (hit.transform.gameObject, PICKUpTrans.position, PICKUpTrans.rotation);
GOTit.transform.parent = playerTrans;
Destroy (hit.transform.gameObject);
}
}
}
Your answer
Follow this Question
Related Questions
I can not modify localscale while I'm playing because of the Animator. 1 Answer
Problem. Pick up and Grab object script, except all objects in scene are picked up instead of one. 0 Answers
Camera isn't move position? Why my camera isn't change position? 0 Answers
GameObject not looking at me.. 1 Answer
IndexOutOfRangeException error when getting transform of array object 0 Answers