I am reading the book learning C# by developing games with unity. In the book you make a hovercraft that is suppose to hover above the terrain. it says to check is triggered, on the player object. When i do that the object falls below the terrain. I believe its suppose to.
The book tells me to write a code that will keep it floating above the terrain by using built in method OnTriggerStay. but it still just falls through the terrain. this method is in my playercontrol script attached to the player object.
First of all, the code you have posted won’t compile. GetComponent needs either a type parameter:
player.GetComponent(typeof(Rigidbody))
a string specifying the type:
player.GetComponent(typeof("Rigidbody"))
or a generic type parameter:
player.GetComponent<Rigidbody>()
The last one is preferable, as you don’t have to do any troublesome casts if you use it. Assuming your code is compiling, there’s two possible mistakes I can come up with off the top of my head:
The player variable isn’t pointing at the hovercraft, so you’re adding force to the wrong rigidbody
The “hovorPower” (should be “hoverPower”) value is too low, meaning that you’re adding less upward force to the hovercraft than the gravity.
For the second option; if the variable is a public variable, you have to set it’s value in the editor - a common beginners mistake is to set it in the script like this:
public float hovorPower = 5f;
but then have it set to 0 in the inspector, meaning that zero power is being added to the hovercraft.