- Home /
Trouble applying rotation locally instead of globally
Hi all, could use a fresh set of eyes as I have been struggling with this script.
I am trying to have a ship control like an aircraft, where when it is rolled 90 degrees and the stick is pulled back the ship's nose will follow the horizon parallel instead of perpindicular towards the zenith. However, if I pull the stick back while in a roll my nose will go towards the sky not pull my jet into a turn.
I believe my problem in the script is the world rotation values are being changed instead of local but I cannot figure out where.
Thanks for any input you have.
using UnityEngine;
using System.Collections;
public class PlayerScript : MonoBehaviour {
private Rigidbody m_Rigidbody;
private Vector3 euler;
public float speed = 5.0f;
private float trueSpeed = 0.0f;
public float turnSpeed = 5.0f;
public float strafeSpeed = 5.0f;
public Vector3 strafe;
private float roll;
private float pitch;
private float yaw;
public float sensitivity = 10.0f;
public float yawSmooth = 0.5f;
public float rollSmooth = 0.75f;
public float pitchSmooth = 0.55f;
public GameObject bullet1;
public Transform bulletSpawn;
void Start (){
m_Rigidbody = GetComponent<Rigidbody>();
euler = transform.localEulerAngles;
roll = Input.GetAxis("Roll");
pitch = Input.GetAxis("Pitch");
yaw = Input.GetAxis("Yaw");
}
// Update is called once per frame
void Update () {
strafe = new Vector3(Input.GetAxis("Horizontal")*strafeSpeed*Time.deltaTime,
Input.GetAxis("Vertical")*strafeSpeed*Time.deltaTime,
0);
var power = Input.GetAxis ("Power");
//Truespeed Controls
if(trueSpeed < 10 && trueSpeed > -3){
trueSpeed += power;
}
if(trueSpeed > 10){
trueSpeed = 9.99f;
}
if(trueSpeed < -3){
trueSpeed = -2.99f;
}
if(Input.GetKey ("c")){
trueSpeed = 0f;
}
if(Input.GetKey ("f")){
m_Rigidbody.freezeRotation = true;
m_Rigidbody.freezeRotation = false;
}
roll = Input.GetAxis("Roll");
pitch = Input.GetAxis("Pitch");
yaw = Input.GetAxis("Yaw");
if(Input.GetMouseButtonDown(0)){
Debug.Log("Boom");
var temp = Instantiate(bullet1,
bulletSpawn.transform.position,
bulletSpawn.transform.rotation);
}
}
void FixedUpdate(){
//apply ship forces
GetComponent<Rigidbody>().AddRelativeForce(0.0f ,0.0f, trueSpeed*speed*Time.deltaTime);
//////////////////////////////////////////////////////////////////////////////////////////
// Yaw over time smoothed
euler.y += yaw * turnSpeed * Time.deltaTime / yawSmooth;
// Roll over time smoothed
euler.z += roll * turnSpeed *Time.deltaTime / rollSmooth;
// Pitch over time smoothed
euler.x += pitch * turnSpeed * Time.deltaTime / pitchSmooth;
// Apply rotation and apply some smoothing
Quaternion rot = Quaternion.Euler(euler);
//transform.rotation = Quaternion.Lerp (transform.rotation, rot, sensitivity);
transform.localRotation = Quaternion.Lerp (transform.localRotation, rot, sensitivity);
//////////////////////////////////////////////////////////////////////////////////////////
//apply strafing
GetComponent<Rigidbody>().AddRelativeForce(strafe);
}
}
Answer by FirePlantGames · May 25, 2015 at 11:56 PM
transform.localRotation?
Look near the bottom of my script, this is the function I am calling to rotate. Hence my question as to why its rotating in the world system.
Try transform.Rotate ins$$anonymous$$d of using your own function, I find that using Quaternion.Euler's can be a little buggy.
Thanks for the response. How can i rotate locally if i am using transform.rotate? $$anonymous$$y controls won't allow me to use world rotation without serious work on modifying outputs based on ship position.$$anonymous$$ath i don't want to have to do unless needed. Sorry for any typos i am typing this on my phone.
Transform.Rotate(0, 0, sensitivity * Time.deltaTime);
Or whatever axis you need, also sorry for any typos, I am on my phone also :)
SIR!
While reading your comment at work I seriously doubted it would work, however in my busy state of $$anonymous$$d that my job puts me in I missed the $$anonymous$$ute detail of the capital T in Transform!!
Just got home changed my code to do input based on this code.....
private Transform tform;
tform = GetComponent<Transform>();
if(pitch != 0){
tform.Rotate(pitch * pitchSmooth * Time.deltaTime,0,0);
}
Thank you very much I knew it was something simple! Worked like a charm.
Your answer
![](https://koobas.hobune.stream/wayback/20220613200255im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
ConfigurableJoint - angular positions (rotation) problem 1 Answer
Set rotation values equal to 0 5 Answers
EulerAngles conversion Quaternion problem 2 Answers
object placement on sphere, rotation deform 1 Answer
Mouse Orbit on a Different XY Plane? 2 Answers