Hi all,
I had the case that my gameObject was not in the “Editor Set Rotation”. As soon as I start the game the ship will point to a 180 degree angle (like if the front is pulled by gravitation but the center mass is fixed at set position).
ANDAs you can see the angle is fixed at those start rotation points and will slightly move with the euler quaternion command.
If you want to move the ship correctly via code I found this solution (maybe there is a simpler one):
you must add/deduct to the corresponding x,y,z values so match the desired angle at state. The problem is then, that it is frozen in this state. Any idea how to tell the program that the 90 degree is the normal state without letting it freeze? I also tried the below command in start parameter but it seems the original code from Ben pulls my ship downwards all the time.
private void ProcessRotations()
{
float pitch = transform.localPosition.y * positionPitchFactor;
transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
{
transform.eulerAngles = new Vector3(
transform.eulerAngles.x + 90,
transform.eulerAngles.y - 90,
transform.eulerAngles.z - 90);
}
}
![grafik|690x234](upload://7paPJPFMYEjDG7TCztgPJA60Ibj.jpeg)
Here is the full code
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
public class PlayerShipMovement : MonoBehaviour
{
[Tooltip(“In ms^-1”)][SerializeField] float speed = 4f;
[Tooltip(“In m”)] [SerializeField] float xRange = 2f;
[Tooltip(“In m)”)] [SerializeField] float yRange = 1f;
[SerializeField] float positionPitchFactor = -2F;
[SerializeField] float controlPitchFactor = -10F;
[SerializeField] float positionYawFactor = 2f;
[SerializeField] float positionRollfactor = 2f;
[SerializeField] float controlRollFactor = -10F;
float xThrow, yThrow;
float xOffset, yOffset;
float rawXPos, rawYPos;
float clampedXPos, clampedYPos;
float pitchDueToPosition;
float pitchDueToControlThrow;
float pitch;
float yaw;
float roll;
// Start is called before the first frame update
void Start()
{
{
transform.eulerAngles = new Vector3(
transform.eulerAngles.x+90,
transform.eulerAngles.y -90 ,
transform.eulerAngles.z -90);
}
}
// Update is called once per frame
void Update()
{
ProcessTranslation();
ProcessRotations();
}
private void ProcessRotations()
{
float pitch = transform.localPosition.y * positionPitchFactor;
transform.localRotation = Quaternion.Euler(pitch, yaw, roll);
{
transform.eulerAngles = new Vector3(
transform.eulerAngles.x + 90,
transform.eulerAngles.y - 90,
transform.eulerAngles.z - 90);
}
}
private void ProcessTranslation()
{
xThrow = CrossPlatformInputManager.GetAxis("Horizontal");
xOffset = xThrow * speed * Time.deltaTime;
rawXPos = transform.localPosition.x + xOffset;
clampedXPos = Mathf.Clamp(rawXPos, -xRange, +xRange);
yThrow = CrossPlatformInputManager.GetAxis("Vertical");
yOffset = yThrow * speed * Time.deltaTime;
rawYPos = transform.localPosition.y + yOffset;
clampedYPos = Mathf.Clamp(rawYPos, -yRange, +yRange); //design up speed
transform.localPosition = new Vector3(clampedXPos, clampedYPos, transform.localPosition.z);
}
}