hi!I need my missile flying a certain height and can detection ground
how should i do?
2 Answers
2Hi what you could do is add constant force component to the object and set your values for y and z
to detect the distance from the ground and to make it foolow the object here is the script
Add rigidbody and constant force componenet to the object and attach this script to it
note
the forward of the misslile should be correct ,to know e\what i am talking about go here read the comments 4th one in particular
public var maxheight:float = 400;
public var Target:Transform;
public var range:float;
private var _Missile:Transform;
private var distancetoground:float;
private var collided:boolean = false;
function Awake(){
_Missile = transform;//Cache it for better performance
rigidbody.useGravity = false;
}
function Update(){
_Missile.LookAt(Target);
var hit:RaycastHit;
if(Physics.Raycast(_Missile.position,-Vector3.up,hit,range)){
if(hit.collider){
distancetoground = hit.distance;//to check distance from
//ground use full for realistic simulation i.e missiles after
//following a target for a certain time and when it comes
//closer to ground its enemrgy cannot overcome the gravity and crashes to the ground
// create a code to make this happen after few second (use +=Time.deltaTime) if you want to know how just ask
if(hit.distance >= maxheight){
constantForce.force.y = 0;
}
if(distancetoground <=5 && distancetoground >=0){
rigidbody.useGravity = true;
constantForce.force.y -= 0.5 * Time.deltaTime;//havent tried it yet
}
}
}
}
function OnCollisionEnter(coll:Collision){
if(coll.collider){
collided = true;
constantForce.force = Vector3.zero;//dont add force
//Explosion and destroy
}
}
i need to know my missile get rigidbody with force and still flying height maybe 200m to the target ,i want my missile count A terrain and can detect distance when missile touch mountain,it can change direction.And detect distance maybe 50m .thanks everyone tell me problem and solve my problem
You should start by trying Trying to accomplish this, and not just go on a website and let other people spoon feed you the answers
– Benproductions1Please provide some more info about this couldn't get you properly .......Are you trying to launch a missile to a certain height or trying to heading the missile towards the ground while it's at certain height????
– Maulik2208You need to provide a lot more information before any can help you. How is it being moved? Translation? Rigidbody with force? What do you mean by certain height. Is this the top of the arc of a projectile, or the maximum height of a guided missile? What do you mean by ground? Do you have a plane as a ground? A terrain? Do other object count? There are lots of ways to do things in Unity and to get a reasonable answer, you really need to explain your problem.
– robertbuConverted :)
– Maulik2208are you trying to calculate the distance between the missile and the ground?
– schetty