- Home /
raycast not detecting my mesh from blender
long story short, i imported a multipart mesh as a .blend file, i gave all its parts a rigidbody, mesh collider(convex), and a fixed joint to connect them all together. each individual peice is set to my "Interactive" layer.
then in my player script i do this:
#pragma strict
var ray: Ray;
var hit: RaycastHit;
var interactionLayer:LayerMask;
var cam:Camera;
var camTr: Transform;
function Start ()
{
cam = Camera.main;
camTr = cam.transform;
ray = Ray(camTr.position, camTr.forward);
}
function Update ()
{
if(Input.GetButtonDown("Kick"))
{
aim();
if(hit.collider != null)
{
Debug.Log("Kick Hit");
hit.collider.gameObject.SendMessage("Kicked");
}
}
}
function aim()
{
Debug.Log("RayShot");
Physics.Raycast(ray, hit, 2.5, interactionLayer);
Debug.DrawRay(camTr.position, camTr.forward*2.5, Color.white,2);
Debug.Log(hit.collider.name);
}
the "interactionLayer" layermask is set to the Interactive layer via the inspector. this code works perfect on any other object from unity, but when it comes to my custom mesh from blender the raycast just doesnt hit it.
the only debug.log i get is the "RayShot" and then an error because the hit.collider.name was out of bounds. (because of no hit)
can anyone tell me whats going wrong?
Answer by hardmode2236 · May 11, 2014 at 04:17 AM
ohh dip... nvm, im a dingus. i set the ray to the cams position at the start, so every time i aimed it was originating from where i spawned. i moved the ray = (cam position, cam fwd) to the top of the aim function and it works now.