This post has been wikified, any user with enough reputation can edit it.
Question by
825orion · Jul 28, 2016 at 07:41 PM ·
physicsflight sim
Rotate Slightly With Torque
Hello,
I am working on a pretty basic flight simulator, using a helicopter model taken from the asset store. I wrote a basic movement script for it. It will move the helicopter using AddForce along all axis, but remaining in the same direction. My question is how to use torque to rotate the helicopter slightly, to make it look more realistic. My current code is as follows:
using UnityEngine;
using System.Collections;
public class HeloFlight : MonoBehaviour {
private Rigidbody rigidbody;
// Use this for initialization
void Start () {
rigidbody = GetComponent<Rigidbody> ();
}
public int vertspeed = 0;
public int horspeed = 0;
public int lrspeed = 0;
public int torque;
public bool lightstate = false;
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.Q))
vertspeed++;
Debug.Log("Increasing Vertical Speed");
//Enact Movement
if (Input.GetKey(KeyCode.E))
vertspeed--;
Debug.Log("Decreasing Vertical Speed");
//Enact Movement
if (Input.GetKey (KeyCode.W))
horspeed++;
Debug.Log ("Increasing Horizontal Speed");
//Enact Movement
if (Input.GetKey (KeyCode.S))
horspeed--;
Debug.Log ("Increasing Horizontal Speed");
//Enact Movement
if (Input.GetKey (KeyCode.A))
lrspeed--;
Debug.Log ("Increasing Right Speed");
//Enact Movement
if (Input.GetKey (KeyCode.D))
lrspeed++;
Debug.Log ("Increasing Left Speed");
//Enact Movement
if (Input.GetKey (KeyCode.X))
rigidbody.useGravity = true;
if (Input.GetKey (KeyCode.Y))
rigidbody.useGravity = false;
if (!Input.GetKey (KeyCode.D) && !Input.GetKey (KeyCode.A)) {lrspeed = 0;}
if (!Input.GetKey (KeyCode.Q) && !Input.GetKey (KeyCode.E)) {vertspeed = 0;}
if (!Input.GetKey (KeyCode.W) && !Input.GetKey (KeyCode.S)) {horspeed = 0;}
rigidbody.AddForce (lrspeed * Vector3.right);
rigidbody.AddTorque (torque * Vector3.right);
rigidbody.AddTorque (torque * Vector3.left);
rigidbody.AddForce (horspeed * Vector3.forward);
rigidbody.AddTorque (torque * Vector3.forward);
rigidbody.AddTorque (torque * Vector3.back);
rigidbody.AddForce (vertspeed * Vector3.up);
rigidbody.AddTorque (torque * Vector3.up);
rigidbody.AddTorque (torque * Vector3.down);
}
}
As you can see I have rigidbody.AddTorque
but it does absolutely nothing. Ideas? Thanks in advance.
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612085708im_/https://answers.unity.com/themes/thub/images/avi.jpg)