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.
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, 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.
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.