- Home /
Plane increase speed
OK, I have a plane script that works just fine. I need it to slowly increase speed just like a real plane. The way it is written just shoots it off.
var moveSpeed= 60.0;
var turnSpeed = 40.0;
var Forward : KeyCode;
var Backward : KeyCode;
var Left : KeyCode;
var Right : KeyCode;
function Update () {
if(Input.GetKey(Forward))
{
transform.eulerAngles.z += - turnSpeed * Time.deltaTime;
}
if(Input.GetKey(Backward))
{
transform.eulerAngles.z += turnSpeed * Time.deltaTime;
}
if(Input.GetKey(Left))
{
transform.eulerAngles.x += turnSpeed * Time.deltaTime;
}
if(Input.GetKey(Right))
{
transform.eulerAngles.x += - turnSpeed * Time.deltaTime;
}
if(Input.GetButton("Jump")){
transform.position += transform.right * - moveSpeed * Time.deltaTime;
}
}
Any help would be appreciated.
Thanks, Tim
are you using a character controller, a kinematic rigidbody or a rigidbody.
it APPEARS based on the inclusion of Jump code (for a plane :P) your using the included charactercontroller.
Would you like an arcade style aircraft or a realistic physics simulation of the plane?
realistic physics requires a rigidbody aircraft.
A charactercontroller is fine for an arcade style flight simulation.
I think I am using a rigidbody. I would like a simulation style though.
well your not using a rigidbody with that code. :P
rigidbodies apply forces to move objects they dont move them directly
what your doing is teleporting the object around basically.
Ridigbodies use the nvidia physix engine to calculate movement.
for a rigidbody you would add the rigidbody component to the plane and use
plane.rigidbody.addforce(vector3 force);
it will add force in the direction vector3
if you would like it to add forward momentum use
plane.rigidbody.addforce(plane.transform.forward * speed)
it will build on itself so if you add 10 meters per second it will grow by 10 meters per second per second until you stop adding force.
as such it will be
1st second 10m per s 2nd second 20m per s 3rd second 30m per s
except physics calculations will kick in and based on the drag coefficient you set in the rigidbody options the object will have a negative force applying on in the opposite direction trying to stop it as well as gravity pushing it down.
I added a picture just saying. And saying plane.transform would do an error right.
So, this would be the new code?
var moveSpeed= 60.0; var turnSpeed = 40.0; var Forward : $$anonymous$$eyCode; var Backward : $$anonymous$$eyCode; var Left : $$anonymous$$eyCode; var Right : $$anonymous$$eyCode; var plane :GameObject; function Update () { if(Input.Get$$anonymous$$ey(Forward)) { transform.eulerAngles.z += - turnSpeed Time.deltaTime; } if(Input.Get$$anonymous$$ey(Backward)) { transform.eulerAngles.z += turnSpeed Time.deltaTime; }
if(Input.Get$$anonymous$$ey(Left))
{
transform.eulerAngles.x += turnSpeed * Time.deltaTime;
}
if(Input.Get$$anonymous$$ey(Right))
{
transform.eulerAngles.x += - turnSpeed * Time.deltaTime;
}
if(Input.GetButton("Jump")){
plane.rigidbody.addforce(plane.transform.forward * moveSpeed);
}
}
Answer by MountDoomTeam · Nov 27, 2012 at 02:54 PM
Hello, did you see there is a code formatting button at the top of the post page
if ( Input.GetKey("mouse 1") )
{constantForce.relativeForce = 15000 * Vector3.forward
;}
this simulates thrusters on a plane by adding a constant force forwards
Here is the complete code for my plane:
var speed = 15.0;
var rotspd = 10.0 ;
var bullitPrefab : Transform;
function Update ()
{
if ( Input.GetKey("mouse 1") )
{constantForce.relativeForce = 15000 * Vector3.forward
;}
else
{constantForce.relativeForce = Vector3.zero;}
if(Input.GetButton("forwards"))
{
transform.eulerAngles.x += rotspd * Time.deltaTime * 7;
}
if(Input.GetButton("backwards"))
{
transform.eulerAngles.x += -rotspd * Time.deltaTime * 7;
}
if(Input.GetButton("left"))
{
transform.eulerAngles.y += -rotspd * Time.deltaTime * 7;
}
if(Input.GetButton("right"))
{
transform.eulerAngles.y += rotspd * Time.deltaTime * 7;
}
if(Input.GetButton("Fire1"))
{
var bullit = Instantiate(bullitPrefab, transform.Find("spawnPoint").transform.position, Quaternion.identity);
bullit.rigidbody.AddForce(transform.forward *8000);
}
}
/*var speed = 3.0;
var rotateSpeed = 32.0;
var bullitPrefab : Transform;
function Update ()
{
var controller : CharacterController=GetComponent(CharacterController);
//Rot
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
//fwd bck
var forward = transform.TransformDirection(Vector3(0,0,1));
var curSpeed = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
if(Input.GetButtonDown("Fire1"))
{
var bullit = Instantiate(bullitPrefab, transform.Find("spawnPoint").transform.position, Quaternion.identity);
bullit.rigidbody.AddForce(transform.forward *2000);
}
}
*/