travel in GameObject's Y-Axis

I was wondering if any one had any idea of how to get physics to apply to a gameObject’s axis rather than the World. For instance, I have a C# script that when the ‘space’ is pressed the gameObject [helicopter] travels along the Y-axis as in <rBody.AddRelativeForce(0, thrust, 0, ForceMode.Acceleration);>. However, if the helicopter is upside-down, it still travels upwards instead of towards the ground. What I am looking for is that the gameObject always accelerates in the direction of the y-axis for itself…not worldSpace. I thank you for your responses. Here is a sample of the Coding:

using UnityEngine;
using System.Collections;
public class flightControls : MonoBehaviour {
//variables
public GameObject pitch;
public GameObject roll;
public GameObject yaw;
public Vector3 curPos;
public Vector3 curRot;
public Rigidbody rBody;
public float rotSpd = 50.0f;
public float thrust = 1.0f;
public bool isGrounded;
public Collision col;
// Use this for initialization
void Start () {
isGrounded = true;
}

// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.Space)) {
rBody.AddRelativeForce(0, transform.up * thrust, 0, ForceMode.Acceleration);
rBody.useGravity = false;
Debug.Log(“Space held”);
//---------------------------------------------------------
if (Input.GetKey (KeyCode.A)) {
yaw.transform.Rotate(0,-rotSpd*Time.deltaTime,0);
Debug.Log(“Space held & A pressed”);
}
//---------------------------------------------------------
if (Input.GetKey (KeyCode.D)) {
yaw.transform.Rotate(0,0,rotSpd*Time.deltaTime);
Debug.Log(“Space held & D pressed”);
}
//-----------------------------------------------------------
if (Input.GetKey (KeyCode.W)) {
pitch.transform.Rotate(rotSpd*Time.deltaTime,0,0);
rBody.AddForce(transform.forward * thrust,0,0, ForceMode.Acceleration;
Debug.Log(“Space held & W pressed”);
}
//-----------------------------------------------------------
if (Input.GetKey (KeyCode.S)) {
pitch.transform.Rotate(-rotSpd*Time.deltaTime,0,0);
Debug.Log(“Space held & S pressed”);
}
} else {
rBody.useGravity = true;
}
if (col.collider.tag == “ground”) {
isGrounded = true;
} else {
isGrounded = false;
}
if (isGrounded == false) {
if (Input.GetKey (KeyCode.A)) {
yaw.transform.Rotate (0, -rotSpd * Time.deltaTime, 0);
Debug.Log (“Space held & A pressed”);
}
//---------------------------------------------------------
if (Input.GetKey (KeyCode.D)) {
yaw.transform.Rotate (0, 0, rotSpd * Time.deltaTime);
Debug.Log (“Space held & D pressed”);
}
//-----------------------------------------------------------
if (Input.GetKey (KeyCode.W)) {
pitch.transform.Rotate (rotSpd * Time.deltaTime, 0, 0);
rBody.AddForce (thrust, 0, 0, ForceMode.Impulse);
Debug.Log (“Space held & W pressed”);
}
//-----------------------------------------------------------
if (Input.GetKey (KeyCode.S)) {
pitch.transform.Rotate (-rotSpd * Time.deltaTime, 0, 0);
Debug.Log (“Space held & S pressed”);
}
}
}
}

Please note that I am trying to avoid using any coding that accesses “vertical” or “horizontal” calls as I am looking to gain more precision in the controls.

lets start with http://forum.unity3d.com/threads/using-code-tags-properly.143875/

not too sure what you mean by “precision” in this context, but hopefully you’re aware of GetAxisRaw which returns the input value without smoothing.

Yes…I am aware of GetAxisRaw. I am trying to create a helicopter flight control script akin to BF4 controls. Not easily achieved when physics/collisions/momentum are involved.

in regards to ‘Percision’…I am stating that when using Euler, there is 12 permutations available that all result in gimble lock. I am trying to avoid using ‘Horizontal/Vertical’ calls because i am getting wierd results when i do. not the real-to-life action that i am seeking. I am currently working on a work around.

But thank you for the suggestion.

the main problem is that the chopper always lifts in the y direction of the world space. so even when the chopper object y direction is facing the world z direction…accelerating still moves the object in the world space y direction. so the chopper raises into the sky even if it is side ways.

You can use transform.Up/transform.Right/transform.Forward to get directions relative to the object.

thank you for your response…what i understand from watching many videos is that realistic helicopter controls are the hardest to program specifically due to all of the calculations involved with rotation and thrust. this is why i’m trying to find a work around. I have tweeked the code a bit and got promising results; but they are still far from accurate.

when i tried your suggestion, the object still rose in world space and not relative to its y-axis. but thank you for the suggestion.

updated code (Please note that the codes are not complete and are in test format):

FlightControl C# Script

using UnityEngine;
using System.Collections;
public class flightControls : MonoBehaviour {
//variables
public Vector3 curPos;
public Vector3 pitch;
public Vector3 yaw;
public Vector3 roll;
public Rigidbody rBody;
public float rotSpd = 5f;
public float thrust = 65f;
public bool isGrounded;
public Collision col;
// Use this for initialization
void Awake () {
isGrounded = true;
rBody.drag = 0;
}
void Update(){
curPos = curPos;
Debug.Log (curPos);
if (col.collider.tag == “ground”) {
isGrounded = true;
} else {
isGrounded = false;
}
}
// Update is called once per frame
void FixedUpdate () {
if (Input.GetKey (KeyCode.Space)) {
rBody.useGravity = false;
rBody.drag = 2;
rBody.AddForce(0, thrust, 0, ForceMode.Impulse);
Debug.Log(“Space held”);
//---------------------------------------------------------
} else {
rBody.useGravity = true;
rBody.drag = 0;
}
if (isGrounded == false) {
//
}
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360.0f)
angle += 360.0f;
if (angle > 360.0f)
angle -= 360.0f;
return Mathf.Clamp(angle, min, max);
}
}

thirdVCam C# Script

using UnityEngine;
using System.Collections;
[AddComponentMenu(“Camera-Control/Mouse Look”)]
public class thirdVCam : MonoBehaviour
{
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;

public GameObject target;
public Rigidbody rBody;
public Vector3 curPos;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
private float rotationX = 0F;
private float rotationY = 0F;
public float thrust;
private Quaternion originalRotation;
public bool isGrounded;
public Collision col;
public void Awake()
{
if (GetComponent())
GetComponent().freezeRotation = false;
originalRotation = transform.localRotation;
isGrounded = true;
thrust = rotationX;
}
public void Update ()
{
if (col.collider.tag == “ground”) {
isGrounded = true;
} else {
isGrounded = false;
}
Debug.Log(“Next”);
}
public void FixedUpdate()
{
Quaternion xQuaternion;
Quaternion yQuaternion;
if (axes == RotationAxes.MouseXAndY) {
rotationX += Input.GetAxis (“Mouse X”) * sensitivityX;
rotationY += Input.GetAxis (“Mouse Y”) * sensitivityY;

rotationX = ClampAngle (rotationX, minimumX, maximumX);
rotationY = ClampAngle (rotationY, minimumY, maximumY);

xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);

transform.localRotation = originalRotation * xQuaternion * yQuaternion;
} else if (axes == RotationAxes.MouseX) {
rotationX += Input.GetAxis (“Mouse X”) * sensitivityX;
rotationX = ClampAngle (rotationX, minimumX, maximumX);

xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
transform.localRotation = originalRotation * xQuaternion;
} else {
rotationY += Input.GetAxis (“Mouse Y”) * sensitivityY;
rotationY = ClampAngle (rotationY, minimumY, maximumY);

yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
transform.localRotation = originalRotation * yQuaternion;
}

if (rotationX <= maximumX) {
curPos.y += curPos.y + rotationX * Time.deltaTime;
}
if (rotationY <= maximumY) {
curPos.x += curPos.x + rotationY * Time.deltaTime;
if(rotationX <= -0.1 && isGrounded == false)
{
thrust = rotationX/20*Time.deltaTime;
rBody.AddForce(0,0,thrust,ForceMode.VelocityChange);
Debug.Log(thrust);
}
if(rotationX >= 0.1 && isGrounded == false)
{
rBody.AddForce(0,0,thrust,ForceMode.Acceleration);
}
}
}
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360.0f)
angle += 360.0f;
if (angle > 360.0f)
angle -= 360.0f;
return Mathf.Clamp(angle, min, max);
}
}