Hey guys!
I have a game in which tanks can shoot bullets at other tanks. The levels will include ramps for these tanks to drive on to platforms and what not.
I have been trying to create a script, that would enable the bullets to follow the ground, so that you would be able to shoot your opponent even if he is on higher grounds, but only if you shoot up a ramp.
My code at the moment looks something like this. It checks for collision with ground and objects below and tries to alter the velocity:
if(Physics.Raycast(transform.position, Vector3.down, out hit, raycastDistance, layer)){
if(hit.distance < altitudeToMaintain){
rigidbody.AddForce(Vector3.up * (altitudeToMaintain-hit.distance), ForceMode.VelocityChange);
}else{
rigidbody.AddForce(Vector3.down * (hit.distance-altitudeToMaintain), ForceMode.VelocityChange);
}
}
I think I need to consider the velocity as well, as it spins out of control when the velocity.y != 0.
Is there anyone out there, who could help me with calculating the right forces to add?