When raycasting, script doesn't detect anything coming into ray yet I did everything right!
Hi!
So I've been making this script that will be used to launch missiles, and I'm wondering why when I fire the missile (NowShoot) the debug.log always says "Hit terrain" even when there is a cube clearly blocking the ray! It seems that it isn't detecting anything going along the ray. If you can't find anything wrong with my script, can you at least show some script you know works so I can modify mine? Thanks!
Here's a picture of the Debug when ray goes thru terrain:
View post on imgur.com
Now one where there is a box collider in it's way but still debug.log says "Hit terrain" :(
View post on imgur.com
using UnityEngine;
using UnityEngine.Networking;
//playerweapon.cs hosts info regarding weapons
public class Shoot : NetworkBehaviour
{
[SerializeField]
private Transform LaunchPoint;
[SerializeField]
private PlayerWeapon missile;
[SerializeField]
private LayerMask mask;
public void NowShoot()
{
//shoot the missile here;
StartShoot();
Debug.Log("NowShoot successful!");
}
void Start()
{
if (LaunchPoint == null)
{
Debug.LogError("PlayerShoot: LaunchPoint error!");
this.enabled = false;
}
Debug.Log("Shoot.cs Started Succesfully!");
}
void StartShoot()
{
RaycastHit _hit;
if (Physics.Raycast(LaunchPoint.transform.position, LaunchPoint.transform.forward, out _hit, missile.range, mask))
{
ShowRay();
Debug.Log("Hit this:" + _hit.collider.name);
//We hit something! :O
}
else
{
Debug.Log("If statement didn't complete! :( Check if all the stuff inside If() is correct");
}
}
void ShowRay()
{
//This is to show Raycast in scene editor. This does NOT affect any in-game graphics!
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 10 * 50, Color.blue);
//Debug.Log("Ray shown! (in theory)");
}
void Update()
{
// need the ShowRay() to keep updating else it won't show in the scene view :(
ShowRay();
}
}
look at the mask its possible your layermask is masking the collider you are blocking with , otherwise i don't see anything wrong with your script , also are you sure you calling now shoot ?
Answer by b1gry4n · Oct 21, 2016 at 05:38 AM
If you did everything right, there wouldnt be any problems! ( i know this question is old, OH WELL ANSWERING ANYWAYS)
The ray you are drawing with debug does not look like the same ray that is being cast in your missle shot.
Debug.DrawRay(transform.position, transform.TransformDirection(Vector3.forward) * 10 * 50, Color.blue);
if (Physics.Raycast(LaunchPoint.transform.position, LaunchPoint.transform.forward, out _hit, missile.range, mask))
try drawing the debug ray from the launchpoint and see if its actually in the spot you thought it was. Store the value of your launchpoint ray and update it when necessary.
Vector3 rayStart;
Vector3 rayDir;
if (Physics.Raycast(LaunchPoint.transform.position, LaunchPoint.transform.forward, out _hit, missile.range, mask)){
rayStart = LaunchPoint.transform.position;
rayDir = LaunchPoint.transform.forward * missile.range;
}
void ShowRay()
{
Debug.DrawRay(rayStart , rayDir , Color.blue);
}
Your answer
Follow this Question
Related Questions
How to make an object move to the direction in which the user faces using vr gaze interaction? 1 Answer
Raycast doesn't interact with Tilemap Collider when in a 3D scene. 0 Answers
Raycast hit or miss in the same situation 1 Answer
I want ray cast in 4 directions, instead i am getting this. 1 Answer
C# Door Script Problem 0 Answers