If your object start state is not in the correct angle

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).

AND

As 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);  
}

}

Hi JohnnyDerpo,

Did you want to share your solution or do you need help with something?

Hey there Nina,

Sorry for not being clear: I would need help on this. I can move that as much as I want the sensitivity on the rotation (pitch) is crazy. Without using the code above my ship is not starting from the preactivation position. With that code it push the ship to the right position but it doesnt allow any rotation. The only way I found was moving the control pitch slider to a crazy value of 500 to get it to the right pos and angle at start. However, the sensitivity of the pitch rotations is crazy and he does several loopings on the way up from Y -1 to Y +1.

Hope it is a bit understandable

Best

J

Just to ensure we are talking about the same: The ship is a child of the Main Camera. And the Main Camera is moved by either the Circuit or Timeline. The ship is not moved by the Circuit or Timeline. The ship is moved by your code. Is this correct?

If so, I’m afraid I do not understand your problem. Or rather: Does your ship, when the game is not started, have got a rotation of (0, 0, 0)? If not, is there a reason why the rotation must be (-180.6, 180, 180)?


See also:

This topic was automatically closed after 22 hours. New replies are no longer allowed.

Privacy & Terms