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.