- Home /
Raycasting isn't working anymore (C#)
Hello, a raycast in my game isn't working anymore, it was working lately before but I don't know what happened, and I didn't change anything in it.
void Update ()
{
RaycastHit RayHit;
Ray ShootRay = new Ray (transform.position, Vector3.forward);
if (Input.GetKey (Keycode.Y))
if (allweapons.weapon == true)
if (Physics.Raycast(ShootRay, out RayHit))
if (RayHit.collider.gameObject.name == OpponentPlayer.name)
{
Debug.Log ("Shooting" + OpponentPlayer.name);
}
Any help would be useful, thanks in advance
Can it be the layer settings ?
The code seams ok at first sight, i would only recomend to put the raycasthit and ray declaration inside the shooting if statement, note that you are creating a new one every update even when there's no need for it.
Also set out a debug.log on the ray object name and the oponente player name, see what's returning.
Answer by fbecker · Jul 04, 2016 at 04:47 PM
Try:
void Update (){
if (Input.GetKey (Keycode.Y))
if (allweapons.weapon == true){
RaycastHit RayHit = Physics.Raycast(transform.position, Vector3.forward,Mathf.Infinity);
if(RayHit)
if (RayHit.collider.gameObject.name == OpponentPlayer.name)
{
Debug.Log ("Shooting" + OpponentPlayer.name);
}
}
}
Your answer
Follow this Question
Related Questions
Finding a RaycastHit tag (Null Reference Exception) 2 Answers
raycastHit.point - How do I make it ignore colliders? 1 Answer
Raycast Not Drawing In Target Direction 0 Answers
How do I check for collisions when I move objects based on Time.deltaTime? 3 Answers
Problem with Raycast detecting a collider overtime. 2 Answers