Linecast error

HI everyone, i am attempting to create a simple platform as a learning experience.
Currently working on the jumping and detecting if the player is on the floor after jumping.

After reading documentation i am tryin to use physics.linecast but running into the following error whenever jumping.

NullReferenceException: Object reference not set to an instance of an object
Player.FixedUpdate () (at Assets/Scripts/Player.cs:51)

here is my code:

 Transform target;
    if (jumping == true) {
       if (Physics.Linecast (transform.position, target.position )) {
         Debug.Log ("Woop");
       }
     }

the script is attached to the player which i believe will just cover the transform.position.

The player has a rigidbody2d and box collider2d attached to it. He is standing on a floor which has the same attached to it.

Anyone have any ideas what i am missing, im sure its something simple but just cant see it.

Thanks.

Have you set the variable target to something? If you haven’t, then that is the cause of the error, since you need to supply both a starting point and an end point.

No i havent, i assumed that target.transform would be when it hits another collider?

If i have a level with several objects i want to act as teh floor, how would i go about setting them as the target?

AM i right in thinking also that linecast projects a line down?

No, that is not how it works. Linecast, casts a line between two points and you must supply a start and an end point. The only thing returned is a bool, indicating if something was hit or not. If you want more information about what was hit, you need to supply more parameters to the method. Have a look at the docs; http://docs.unity3d.com/ScriptReference/Physics.Linecast.html, if you scroll down a bit you’ll see that there is another, longer way to call the method.

I’m not sure I would use Linecast for your purposes. Raycast is more appropriate and simpler to use in your scenario.

That is great, thank you.
i changed teh code to this:

      Vector3 dwn = transform.TransformDirection (Vector3.down);
       Physics.Raycast  (transform.position, dwn,0.1f );
         Debug.DrawRay(transform.position,dwn) ;
         Debug.Log ("Woop");
       jumping = false;

Which is working fine, however the line is ray line is too long. I tried shortening it to 0.1 but the debug line appears to be the same length.

Because you haven’t changed the length of the debug line. This is actually an important distinction between how the Physics.Raycast and Debug.DrawRay functions work. In the Physics.Raycast, the “dwn” here is purely a point in space to determine direction from the origin point, with length defined separately, but in the Debug.DrawRay it’s a point AND a length, because the ray isn’t really a ray, it’s a line segment with two terminating points. If you want to draw the length of the ray accurately, then you need to give the Debug.DrawRay the “final point”, not just a direction (multiply the direction by the length).

In this case, you can subtract the origin point from the destination point, multiply the result by the adjustment to the length that you want (.1f I guess), then add that back to the origin point value to get a “final adjusted point”. You could also just use transform.TransformPoint(Vector3.direction * length adjustment) to do the same thing.

In other words:

float distanceMultiplier = .1f;

Vector3 dwn = transform.TransformDirection (Vector3.down);
Vector3 finalPoint = transform.TransformPoint(Vector3.down * distanceMultiplier);

Physics.Raycast (transform.position, dwn, distanceMultiplier);
Debug.DrawRay(transform.position, finalPoint);

Debug.Log ("Woop");
jumping = false;

… or something. Or you could do it the way that doesn’t use TransformPoint (it’s a lot slower than TransformDirection).

float distanceMultiplier = .1f;

Vector3 dwn = transform.TransformDirection (Vector3.down);
Vector3 finalPoint = (dwn - transform.position) * distanceMultiplier + transform.position;

Physics.Raycast (transform.position, dwn, distanceMultiplier);
Debug.DrawRay(transform.position, finalPoint);

Debug.Log ("Woop");
jumping = false;

I’m honestly not sure if there’s any difference though.