- Home /
Help with raycasting code
This is my code for a raycast thats meant to display how far something is away from you when you click. but it doesnt change the "distance" vriable. can someone fix it for me? It also has another set of code but dont worry about it.
#pragma strict
var dmg : int = 50;
var distance : float = 1.0;
function update()
{
if (Input.GetButtonDown("Fire1"))
{
var hit : RaycastHit;
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit))
{
distance = hit.distance;
hit.transform.SendMessage("Applydmg", dmg, SendMessageOptions.DontRequireReceiver);
}
}
After pasting code, select the code and use the 101/010 button to format it (and the new lines will show up). I did it for you this time.
your RayCast is missing a fourth variable a float for the distance to send the ray.
ie:
if (Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), hit, 100.0))
@LouisWatson - a fourth parameter is not required. If not provided, it will default to $$anonymous$$athf.Infinity.
Answer by philwinkel · Sep 21, 2014 at 05:02 AM
Raycast wont hit anything that does not have a collider. Make sure your geometry have some kind of colliders on them.
So do i just have to turn $$anonymous$$esh collider on?
It may be what Lylek23 said above, you did not capitalize Update() so it won't be called. change update() to Update(). And of course, you do need a collider, or the raycast won't hit anything, like philwinkel said.
It was the capital :( almost 2 weeks of trying to figure it out and it was a capital u :(