- Home /
RaycastHit always returns 0
I'm trying to move a blood prefab (a decal) to an enemy's feet when they die. The old script I had (commented out in the Start() function) simply drops the y coordinate to 0. However, I want to use raycasting to drop the prefab to the nearest collider, within 2 units. Otherwise, the prefab should be destroyed.
using UnityEngine;
using System.Collections;
public class PuddleDown : MonoBehaviour
{
// Use this for initialization
void Start()
{
//transform.position = new Vector3(transform.position.x, 0, transform.position.z);
}
void Update()
{
RaycastHit floor;
Vector3 down = transform.TransformDirection(Vector3.down);
if (Physics.Raycast(transform.position, down, 2))
{
transform.position = new Vector3(floor.point.x, floor.point.y, floor.point.z);
}
else
{
Destroy(gameObject);
}
}
}
The problem is that floor.point is always 0, no matter where the AI is. I don't use raycasting a lot and I have no idea why this is happening. Can someone help me?>
Use Debug.DrawRay() to see the ray and check what's going on
Answer by $$anonymous$$ · May 16, 2015 at 06:18 PM
It is because you have declared RaycastHit floor variable but you are not assigning value to it from the Raycast itself. This might help:
if (Physics.Raycast(transform.position, down, out floor, 2))
Your answer
Follow this Question
Related Questions
Boxcast vs Raycast Oddities 1 Answer
Raycast not hitting the collider properly it always has a weird offset 0 Answers
Problem With Raycasthit Angle 0 Answers
Physics.Raycast doesn't hit anything even though Debug.DrawRay works 1 Answer
Quarternion.LookRotation is only rotating my game object on one axis 1 Answer