# Flying script

**URL:** https://discussions.unity.com/t/flying-script/373253
**Category:** Scripting
**Tags:** Scripting
**Created:** [August 12, 2005, 4:39pm UTC](https://discussions.unity.com/t/flying-script/373253 "2005-08-12T16:39:04Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![anon\_14684896](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@anon\_14684896](https://discussions.unity.com/u/anon_14684896)
#### Post date: [August 12, 2005, 4:39pm UTC](https://discussions.unity.com/t/flying-script/373253/1 "2005-08-12T16:39:04Z")

</div>

We’re playing around with an idea for flying an object through 3d space. We have a basic translate script running fine but it’s locked to a plane (x,y, or z). Anyone have any ideas on how we could define the translate function to “forward thrust” the object in the direction it’s looking (using a MouseLook script)?

Kind of thinking along the lines of a plane or space ship flying through 3d space.

- Erik

---

<div class="post-metadata">

### Author: ![Joachim\_Ante\_1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/joachim_ante_1/32/44155_2.png) [@Joachim\_Ante\_1](https://discussions.unity.com/u/Joachim_Ante_1)
#### Post date: [August 12, 2005, 9:27pm UTC](https://discussions.unity.com/t/flying-script/373253/2 "2005-08-12T21:27:26Z")

</div>

Use the Translate function for this.

```auto
forward = Vector3.fwd;
forward *= Time.deltaTime * Input.GetAxis ("Vertical");
transform.Translate (forward);

```

---

<div class="post-metadata">

### Author: ![anon\_14684896](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@anon\_14684896](https://discussions.unity.com/u/anon_14684896)
#### Post date: [August 14, 2005, 3:45am UTC](https://discussions.unity.com/t/flying-script/373253/3 "2005-08-14T03:45:51Z")

</div>

I found a nice solution. Create a game object. Attach a mouse look script then add this “Thrust” script (which is derived from the fps script):

```auto
using UnityEngine;
using System.Collections;

public class Thruster : MonoBehaviour {
	// declare the variables
	public float Speed = 7;
	public float Drag = 20;
	public float DragNoMovement = 50;
	const float airDrag = 0F;

	void FixedUpdate () {
		// get the inputs
		float horizontal = Input.GetAxis ("Horizontal");
		float vertical = Input.GetAxis ("Vertical");

		// check to see if the user is moving
		bool userMoved = Mathf.Abs (horizontal) > 0.1F || Mathf.Abs (vertical) > 0.1F; 

		// determine the force vector
        		float x = horizontal * Speed;			
		float z = vertical * Speed;
		rigidbody.AddRelativeForce (new Vector3 (x, 0, z), ForceMode.VelocityChange);
		
		// apply the appropriate drag when moving
		if (userMoved)
			rigidbody.drag = Drag;
		else
			rigidbody.drag = DragNoMovement;
	}
	
	
	void Start () {
		if (rigidbody==null)
			gameObject.AddComponent ("Rigidbody");

		// don't let the physics engine rotate the character
		rigidbody.freezeRotation = true;
	}
}

```

Basically, I’ve snapped the mouse look camera to the game object so when the camera looks up, the game object looks up. Then add the force in the “looking” direction.

- e

---

<div class="post-metadata">

### Author: ![nm8shun](https://avatars.discourse-cdn.com/v4/letter/n/df788c/32.png) [@nm8shun](https://discussions.unity.com/u/nm8shun)
#### Post date: [July 14, 2007, 10:44pm UTC](https://discussions.unity.com/t/flying-script/373253/4 "2007-07-14T22:44:46Z")

</div>

Cool idea. Does this still work with the latest versions of Unity? I’ve tried to create a Camera, add a MouseLook script and then created a new C Sharp script with your script above pasted into it (and then applied also to the camera). But it seems to break. Am I incorporating this incorrectly?

---

<div class="post-metadata">

### Author: ![AaronC](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/aaronc/32/293104_2.png) [@AaronC](https://discussions.unity.com/u/AaronC)
#### Post date: [July 14, 2007, 11:41pm UTC](https://discussions.unity.com/t/flying-script/373253/5 "2007-07-14T23:41:45Z")

</div>

wadamw\>  
You have to name C# scripts definitively:ie

```auto
...using System.Collections; 

public class Thruster : MonoBehaviour { 
   // declare the variables 
   public float Speed = 7; 
   public float Drag = 20; 
...

```

This one NEEDS to be named “Thruster” with no quote maks…  
did that work?

Welcome to the Unity forums  
AC

---

<div class="post-metadata">

### Author: ![nm8shun](https://avatars.discourse-cdn.com/v4/letter/n/df788c/32.png) [@nm8shun](https://discussions.unity.com/u/nm8shun)
#### Post date: [July 14, 2007, 11:56pm UTC](https://discussions.unity.com/t/flying-script/373253/6 "2007-07-14T23:56:45Z")

</div>

Sho 'nuf. I coulda sworn I tried that…but it worked just like you suggested. Thanks much!

---

<div class="post-metadata">

### Author: ![nm8shun](https://avatars.discourse-cdn.com/v4/letter/n/df788c/32.png) [@nm8shun](https://discussions.unity.com/u/nm8shun)
#### Post date: [July 15, 2007, 12:08am UTC](https://discussions.unity.com/t/flying-script/373253/7 "2007-07-15T00:08:13Z")

</div>

One other question on this thread. Is there a way to add say, Shift and Cntrl to allow for movement straight up and straight down?

---

<div class="post-metadata">

### Author: ![AaronC](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/aaronc/32/293104_2.png) [@AaronC](https://discussions.unity.com/u/AaronC)
#### Post date: [July 15, 2007, 2:38am UTC](https://discussions.unity.com/t/flying-script/373253/8 "2007-07-15T02:38:15Z")

</div>

If you check that other thread theres a script where if you change the (“a”) and (“d”) parts for (“ctrl”) and (“shift”) it should work…  
AC
