Need solution for rotation with player input of X, Y, Z
Hello,
I'm kind of stuck in my current project. So to give a proper explanation of the situation, I need to rotate a spaceship, in wich the player sits in, but here comes the hard part (at least for me), because this is going to be a vr game(GearVR), but will need a good amount of player input whitout any peripherals. I created a keypad, wich is able to take one, two, or three values, positive and negative numbers too. After inputting the required amount of values for the given function to work, it calls the function and that works with the input values from the keypad, in this case X, Y ,Z for rotation. So what i want to achieve is after the input, the player ship should smoothly rotate to face the given coordinates. This is my 3rd or 4th attempt at making it work, but I still keep getting really strange rotations. The problem is probably with my understanding of how Quaternions should work, I would like to get some help from anybody who can wrap his head around Quaternions and Angles and all that... Ideas of other solutions, even dumping the keypad mechanic are welcome also.
Sorry for the wall of text, and thanks for taking the time to read it ;)
This is the code i managed to get to work at least with false rotations. The relevant functions are the faceGivenCoordinates and rotateShip functions.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GyroStab : MainFrameApplication {
public float rotationSpeed;
Quaternion targetRotation;
//Vector3 currentRotationAngle;
Vector3 targetRotationAngle;
public void stabilize()
{
mainFrameScreen.GetComponent<Text>().text = "GyroStab initiated...stabilizing ship.";
}
public void initialiseRotationalControls()
{
mainFrameScreen.GetComponent<Text>().text = "Initialising rotational controls,\nplease input the following parameters\nin this exact order:\nRotation X, Rotation Y, Rotation Z";
keypad.GetComponent<KeyPadScript>().activatingFunction = KeyPadScript.function.GyroStab;
keypad.GetComponent<KeyPadScript>().activatingButtonGameObject = this.gameObject;
//keypad "felnyit�sa" haszn�lathoz
}
public void faceGivenCoordinates(int rotationX, int rotationY, int rotationZ)
{
mainFrameScreen.GetComponent<Text>().text += "Starting rotation towards given coordinate: [ " + rotationX + ", " + rotationY + ", " + rotationZ + " ]";
//cockpit �s ship model aktu�lis forgat�sa
//currentRotationAngle = spaceCraft.transform.eulerAngles;
targetRotationAngle = new Vector3(rotationX, rotationY, rotationZ);
targetRotation = Quaternion.LookRotation(targetRotationAngle);
StartCoroutine(rotateShip());
}
IEnumerator rotateShip()
{
rotationSpeed = 0.25f;
Quaternion startingRotation = spaceCraft.transform.rotation;
Debug.Log(startingRotation);
Debug.Log(targetRotation);
for (float i = 0f; i < 1f; i += rotationSpeed * Time.deltaTime)
{
spaceCraft.transform.rotation = Quaternion.Lerp(startingRotation, targetRotation, i);
yield return null;
Debug.Log("tick");
}
spaceCraft.transform.rotation = targetRotation;
}
}
Answer by X10KND · Nov 21, 2016 at 04:07 PM
Ah I got lost in the script (viewing this in mobile)
Try this (sorry if it is hard to read cuz I'm typing from a mobile device)
This script will be attached to the spaceship
Vector3 target;
void Update () {
target = new Vector3 (put x value here, put y value here, put z value here);
transform.LookAt(target);
}
If it is not smooth, notify me.
Thanks for the answer! This was a posted a long time ago, I've already solved the problem by reworking the way rotations will be done, but thank you for you :)