- Home /
Have to press Fire1 button twice to run if statement?
I'm trying to make something happen when my raycast hits a specific object at close range, the problem I'm having is that when I do, I have to press the "fire1" button twice before anything happens. The script is attached to the built in first person controller asset.
var hit3 : RaycastHit;
var Distance3 :float;
function Update () {
Distance3 = hit3.distance;
if (Input.GetButtonUp("Fire1") && Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit3) && hit3.collider.name == "BED>ELEC" && Distance3 < 3)
{
Debug.Log("SUCESS");
}
}
I'm very new to scripting and Unity in general, so I might be missing something obvious. Any help is greatly appreciated! :)
You have your Distance3 set/check as part of the if(Input....) bit which is probably null until the first click (which sets hit3 to something)
I'm not sure I understand. If Distance3 is part of the if(input...) it doesn't have to be within the { } tags? Because as I see it now Distance3 is set outside of the if(input...)?
Edit: Never$$anonymous$$d, I think I understand now, thanks!
Answer by getyour411 · Apr 25, 2014 at 02:46 AM
You are setting Distance3 to hit3.distance, but hit3.distance is not computed until the firstRaycast
Yarp. So Distance3 is always equal to the distance calculated during the previous frame.
Ins$$anonymous$$d of assigning it to a local variable just use hit3.distance
Your answer
Follow this Question
Related Questions
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Gun Script Help 2 Answers
Trouble with raycasting 0 Answers
How to fix a small raycast issue? 2 Answers