- Home /
How to make a 3d model plane fly?
Im new to unity3d and have been trying to make a 3d model of a fighter jet fly but dont know where to start. I've looked at other questions and tried using scripts but no script I have tried does anything so I can guess im doing something wrong so could someone please tell me how to get a script to work properly and then make a plane fly.
Much appreciated
Answer by rabbitfang · Nov 27, 2011 at 06:01 PM
Don't try to mimic physics by changing the transform's position. It can look unrealistic.
You are going to want to attach a Rigidbody component to the plane object. This will apply physics simulations to the plane. You want to create a script that applies a force to lift the plane, and a force to turn the plane.
http://unity3d.com/support/documentation/ScriptReference/Rigidbody.html
Answer by ptdnet · Nov 27, 2011 at 05:29 PM
You want to change the object's transform variable:
myPlane.transform.position = new Vector3(newXPosition, newYPosition, newZPosition);
Simulating physics by changing the transform is not a very good idea. There would be a lot of calculations involved that would slow down the game as opposed to using the Physics engine.
@ptdnet Once you attach the 3D rigidbody, what code in the script would make it move forward? I'm trying to make an airplane. I grouped all the objects into an empty object, and rotated it in the air so it looks like it's flying forward. I use this script, but my player doesn't move with it and can somehow go through objects.
using UnityEngine;
using System.Collections;
public class $$anonymous$$oveForward : $$anonymous$$onoBehaviour {
public float speed = 0.5f;
public Vector3 direction = Vector3.right;
public void Update()
{
transform.Translate (direction * speed * Time.deltaTime);
}
}
To make my plane go forward, the directions it moves is around X 6 and Z 1. Help please!
Answer by N1warhead · Apr 14, 2016 at 11:04 AM
What I would do, and this is just me.
I would apply a Rigidbody to my object. Then the technical parts (nothing fancy) but it should get you on the right track. KEEP IN MIND THIS ISN'T TESTED!!!!! (I just made this from the top of my head really quick for you).
HOWEVER, IT SHOULD IN THEORY WORK.
All this will do is, let you fly a plane from run way, reach a certain speed and start to lift. And very very basic rotations to let you rotate.
It's in NO WAY PERFECT. But should at least get you in the right direction to learning how to do this.
If this at least gets you going in the right direction please mark this as correct so others will know. Thanks buddy.
Any issues, please feel free to respond and I'll try my best to help fix them. (Not going to design the entire thing for you) - just helping you in the right basics of directions at least. //Keep in mind, this is very basic barebones to help get you going. I'll include basic rotation as well (NOT PERFECT).
//C# SCRIPT
public float maxSpeed; // Our maximum speed allowed.
public float decAltSpeed; //Decrease Altitude.
public float incAltSpeed; // Increase Altitude.
public float curSpeed; // Current Speed;
public float applySpeed; // How much force to apply (Forward).
public float airBrakeForce; // Breaks.
private Rigidbody myRigid; // Our Rigidbody.
void Start(){
myRigid = GetComponent<Rigidbody>(); // Gets the players Rigidbody.
}
void FixedUpdate(){
curSpeed = myRigid.velocity.z;
if(curSpeed >= maxSpeed){
//If our speed maxes out to our maxSpeed we restrict our plane from going any faster.
applySpeed = curSpeed;
}
if(Input.GetKey(KeyCode.W)){
//Apply Accelleration
myRigid.AddRelativeForce(Vector3.forward * applySpeed);
if(curSpeed >=incAltSpeed){
myRigid.AddRelativeForce(Vector3.up * AnyNumberYouWant);
}
}
if(Input.GetKey(KeyCode.S)){
//Apply BREAKS
myRigid.AddRelativeForce(Vector3.back * airBrakeForce);
if(curSpeed <= decAltSpeed){
myRigid.AddRelativeForce(Vector3.down * AnyNumberYouWant);
}
}
}
void Update(){
//NOT REALISTIC ROTATIONS
//You may have to move the '-' sign to the other one (- means opposite) E.G. - Negative so I THINK -Vector.up is left. Could be wrong though.
if(Input.GetKey(KeyCode.A)){
transform.rotate(-Vector3.up * 15 * Time.deltaTime);
}
if(Input.GetKey(KeyCode.D)){
transform.rotate(Vector3.up * 15 * Time.deltaTime);
}
}
}
Your answer
Follow this Question
Related Questions
the flying script 1 Answer
Accelerometer Plane Controls help 0 Answers
Optimizing and generating terrain on mobile 1 Answer
how to make a plane model fly 1 Answer
Transform.position with Box Collider? 4 Answers