I’m trying to write an if statement, whereas if a Ray hits nothing, like if I’m looking into the sky, I want it to change a variable value.
Here’s what I’ve been messing with
var hit : RaycastHit;
if (hit == null)
{
sensitivityX = 3.0;
sensitivityY = 3.0;
}
as per the reference, the actuall raycast function will return true if something is hit.
function Update () {
var fwd = transform.TransformDirection (Vector3.forward);
if (Physics.Raycast (transform.position, fwd, 10)) {
print ("There is something in front of the object!");
}
}
Thanks for the reply, and yeah hitting an object is not my problem. I want to change the value of a variable if I Don’t hit anything.
//pseudo code
If ray hits nothing
then variable value = x
I think it’s prolly as simple as stating something = null, but I haven’t figured out how to word it right yet, and I’ve been over the scripting reference carefully already
Perhaps setting the variable to the value you want it to be when nothing is hit, and then change it when something is hit.
//pseudo code
foo = 10; // this is the value you want for no hit
if(raycast hits something){
foo = 20; // this is the value for the hit
}
if (!Physics.Raycast(...)) {
// set value
}
–Eric
1 Like
Well actually I have the variable changing when the ray hits an object tagged “Player”, the ray is cast from the crosshair, and it changes back to default just fine when the crosshair passes over an object without the “Player” tag.
But if I aim at an object with the Player tag, and then aim into the sky, the variable does not change back to default as desired.
So in order to remedy this I need to write an Else If that will change the variables value back to default when the ray collides with nothing.
Here’s my code;
public var aimFarthestPoint : int = 100; //The farthest distance the aim-assist will function
public var aimNearestPoint : int = 15;
function Update()
{
//If Ray collides with object tagged Player within 15 and 100 units from this object
//then lower aim sensitivity, else, don't
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(this.transform.position, fwd, hit, aimFarthestPoint))
{
if ((hit.collider.gameObject.tag =="Player") (hit.distance >= aimNearestPoint))
{
sensitivityX = 0.3;
sensitivityY = 0.3;
}
else if (hit == false) <<<====== Not working
{
sensitivityX = 3.0;
sensitivityY = 3.0;
}
else
{
sensitivityX = 3.0;
sensitivityY = 3.0;
}
}
}
Your problem is that if your Raycast doesn’t hit anything you have no else statement. You actually put the part you need inside of the if(raycast) statement. Since it only makes it inside of that if statement when there is a hit, the variable hit will never be null. Add that other stuff to an else statement instead.
public var aimFarthestPoint : int = 100; //The farthest distance the aim-assist will function
public var aimNearestPoint : int = 15;
function Update()
{
//If Ray collides with object tagged Player within 15 and 100 units from this object
//then lower aim sensitivity, else, don't
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
if (Physics.Raycast(this.transform.position, fwd, hit, aimFarthestPoint))
{
if ((hit.collider.gameObject.tag =="Player") (hit.distance >= aimNearestPoint))
{
sensitivityX = 0.3;
sensitivityY = 0.3;
}
else
{
sensitivityX = 3.0;
sensitivityY = 3.0;
}
}
else
{
sensitivityX = 3.0;
sensitivityY = 3.0;
}
}
1 Like
Thanks, I figured that out last night, and it worked a charm :]
Here’s a link to the full aim assist code if you’re interested
http://forum.unity3d.com/viewtopic.php?t=53032