- Home /
what code should i be using if im only wanting a function to happen if a raycast does not return a hit
Im Writting a Script that will generate a list of connect waypoints but the only thing is im stuck with the last peice of code...
this is my code
#pragma strict
@script ExecuteInEditMode()
/************************************************************************************************************/
// Simple Waypoint Script.
// Author: Harley Urquhart
// Version: 1
/************************************************************************************************************/
//Notes
//This Requires that at least one other waypoint is included as a link
//Behaviours can then be made
/************************************************************************************************************/
//Variables
var Links : Transform[];
/************************************************************************************************************/
function Awake(){
//test for errors
if (Links.Length <1){
//display an error and the name of the waypoint
Debug.LogError("Please assign at least one object to the array of waypoints in the editor for: " + gameObject.name);
}
if(gameObject.CompareTag("Untagged")){
// Set the Tag Immediately if the player forgot to tag the Waypoint Prefab
gameObject.tag = "Waypoint";
}
}
function Start () {
//make the object invisible
renderer.enabled = false;
//turn off collisions
collider.enabled = false;
}
function Update(){
// draw a ray to visualise the path between Two way points(minimum)
// create a forloop
//instead of manually adding linked waypoints
//find waypoins that are not obscured by any colliders
//then link them together by adding that waypoint to the links array
// Find all game objects with tag Waypoint
var Waypoints : GameObject[];//array of all tagged Gameobjects
Waypoints = GameObject.FindGameObjectsWithTag("Waypoint");
var position = transform.position; //
//find waypoints that are not blocked by a collider.
for (var waypoint : GameObject in Waypoints) {
//cast out a ray
var hit : RaycastHit;
if (Physics.Raycast (position, waypoint.transform.position, hit)) {
}
}
for (var x = 0; x < Links.Length;x++){
//iterate through all the coonected waypoints and cast rays out
Debug.DrawLine(transform.position, Links[x].position,Color.red);
}
}
function OnDrawGizmos () {
Gizmos.DrawIcon (transform.position, "Light Gizmo.tiff", true);
}
i just need help at line 53 to inset the correct code here
please note this is an addition to a script that will then be a part of a Waypoint Navigation system i will add to the asset store later on.
This Script will be part of a small package later on...
Answer by wilgieseler · Oct 17, 2013 at 05:34 AM
Seems like a simple if/else statement to me, at least in this particular script.
if (Physics.Raycast (position, waypoint.transform.position, hit)) {
// raycast did hit
}
else {
// raycast didn't hit
}
the only problem is no matter what i have checked it against so far the else wont return or i get both happening.. ill try to see if this works and ill be right back
From the scripting reference
Returns bool True when the ray intersects any collider, otherwise false.
Your answer
![](https://koobas.hobune.stream/wayback/20220613120100im_/https://answers.unity.com/themes/thub/images/avi.jpg)