Homing missile just rolls toward the player/not floating
I'm using this script for a homing missile that I got off of a tutorial. The homing part is fine but when the player is on the ground, the missile object is on the ground and looks like it's rolling toward the player. Is there a way to get the missile to float until it is a certain distance from the player? The ground is a flat plane with a few walls here and there that destroy the rocket upon collision.
public class Track_Player : MonoBehaviour { public Rigidbody rb; public Transform player; public float turn; public float speed;
// Update is called once per frame
void Update()
{
rb.velocity = transform.forward * speed;
var missileRotation = Quaternion.LookRotation(player.position - transform.position);
rb.MoveRotation(Quaternion.RotateTowards(transform.rotation, missileRotation, turn));
}
}
Answer by judahcline · Sep 10, 2020 at 01:34 PM
your rigidbody might still have gravity on it so change that number to 0. but if it already at zero you can add a raycast to check if it is to close to the ground
Answer by kmay11005 · Sep 10, 2020 at 03:50 PM
What do you mean change the gravity number to 0? Do you mean turn gravity off or is there a function/option that I don't know of that strengthens gravity?
What they mean is: Look at the RigidBody of the missile object, it should have a property called: "Gravity Scale". If it's set to 0, it won't be affected by gravity. You can change this at run time.
Can you specify a bit more what it is that you're trying to do? Do you want the missile to try to avoid hitting the ground? Do you want it to explode instead of rolling when it does hit the ground? Both?
@$$anonymous$$azer83 I looked through the rigid body and did some research online, but unity 3d doesn't have a gravity scale setting, instead, it has a use gravity settings. Switching to disable gravity does make the missile hover over the ground a little though.
Eventually, I want the missile to do both. When I ran the game, unless the player object was jumping, the missile was on the ground, and it looked like it was rolling toward the player. So I've been trying to get the missile object to track the player whilst hovering over the ground at a certain height until it's a certain distance from the player, which at that point, I wanted it to descend/dive toward the player. But now that I actually got the missile to float, the descend/ dive part seems like a bad idea. I might still want the missile to hover a little bit higher than it is now, but not really sure how to do that so any help is appreciated.
I'm planning to add the explosion upon collision, and avoiding hitting the ground stuff once I figure out the ho$$anonymous$$g parts out by using raycasts, capsule checks, and Addforce.