Snapping Problem

Hey guys.
I am currently trying to build some basic spaceshipmovement (and its progressing very well) but now I have encountered a problem. When the spaceship tilts up more and more it reaches a point when it just magically snaps to 90/270 degrees on the x-axis (at 87 and 273 degrees it snaps) and gets stuck there. I have allread read something about this and that this is a unity-problem but this looping like turn is pretty important for moving in 3D space (in this case really “space” because its a spaceship :smile:) so limiting the rotation to 87 or 273 degrees is no option(I would not like to :(). Do you have a clue how to get this right?

Thanks, Mike :slight_smile:

First, I’m pretty confident that it’s not “a Unity problem”. Based on your explanation, it sound like gimbal lock. You’ll probably need to post the code you’re using for rotation before you’ll get any useful help.

Jeff

Uhm now it also startet jumping around when it tilts over -10 or 350 on the x-axis… here is my script (some chunks of it);

For tilting. This makes the rotation turn up and down(and jump around)

public void Turnup()
	{
		desiredxrot = xangle-turnstep;
	}

                xangle = transform.rotation.eulerAngles.x;
		xangle = Mathf.Lerp (xangle,desiredxrot, Time.deltaTime*turnsmooth);
		Vector3 turneuler = transform.rotation.eulerAngles;
		turneuler.x = xangle;
		transform.rotation = Quaternion.Euler (turneuler);

When i reach the degrees i talked about way up (87 and 273 degrees) it snaps…

That can’t be live code, right? Most of it appears to not be within any method…

He clearly said

Either way, we need to see more code to really help.

Yeah, I guess I missed that… Though (as you implied) out-of-context chunks aren’t really overly helpful.

Okay I will paste my complete code here then if it helps;)

by the way “Shipremote” is just another script to controll this script because I want multiplayer to work later on. :wink:

using UnityEngine;
using System.Collections;

public class Shipcontrol : MonoBehaviour 
{
	public int speed = 0;
	int maxspeed = 50;
	int maxbspeed = -10;
	int acceleration = 2;
	float zrot = 90f;


	public float turnsmooth = 1.4f;
	
	public float smoothspeed = 1.0f;

	private float xangle;
	private float yangle;
	private float desiredxrot;
	private float desiredyrot;

	public float show;
	public float turnstep;


	void Update () 
	{
		show = desiredxrot;
		//access freeflightactive component of shipremote
		GameObject MainCam = GameObject.Find ("Main Camera");
		Shipremote remotescript = MainCam.GetComponent<Shipremote>();
		bool freeflightactive = remotescript.freeflightactive;

		float xrotation =transform.rotation.eulerAngles.x;

		if(!freeflightactive)
		{
			//rotate towars plane
			float angle = 0.0f;

			angle = transform.rotation.eulerAngles.z;
			angle = Mathf.Lerp(angle,zrot,Time.deltaTime*smoothspeed);
			Vector3 euler = transform.rotation.eulerAngles;
			euler.z = angle;
			transform.rotation = Quaternion.Euler (euler);
		}

		transform.Translate(0, 0, speed * Time.deltaTime); //moving forward placeholder



		xangle = transform.rotation.eulerAngles.x;
		xangle = Mathf.Lerp (xangle,desiredxrot, Time.deltaTime*turnsmooth);
		Vector3 turneuler = transform.rotation.eulerAngles;
		turneuler.x = xangle;
		transform.rotation = Quaternion.Euler (turneuler);

	}


	void OnGUI()
	{
		GUI.Label (new Rect (0,150,100,100),"Speed: "+speed);
	}

	public void IncreaseThrottle()
	{
		if(speed<maxspeed)
			speed = speed + acceleration;
		if(speed>maxspeed)
			speed = maxspeed;
	}
	public void DecreaseThrottle()
	{
		if(speed>maxbspeed)
			speed = speed - acceleration;
		if(speed<maxbspeed)
			speed = maxbspeed;
	}

	public void Turnup()
	{
		desiredxrot = xangle-turnstep;
	}
	public void Turndown()
	{
		desiredxrot = xangle+turnstep;
	}
	public void Turnleft(){}
	public void Turnright(){}
}

Here is also one picture of the ship, its transform and the desiredxrotation variable and how they act:

Picture one is just normal. “Show” is just for me to indicate the desiredxrot. I have touched nothing now.
Picture two shows what happens if I hit the up or down button. It just jumps to a high value but it shouldnt. This makes the rotation bumb around at 360°x.
Picture 3 is what happens if I get to 270 and the ship gets stuck there. As you can see it does not move to its desired rotation either…

I found the error but I dont know how to solve it. The problem is the line

xangle = Mathf.Lerp (xangle,desiredxrot, Time.deltaTime*turnsmooth);

Even if xangle=0 the Lerped xangle is around 0.1. Problem #1. #2 is that if I now push a button for turnig up or down the xangle value jumps to 3.01 or -3.01. The snaping at 90 and 270 degrees z is propably be the same problem because i I have done it with Mathf.Lerp too. Anyone knows how to fix this?

I would read the docs on ‘Lerp’ again… that lines looks completely wrong. What exactly are you expecting it to do?

I don’t know where this line is wrong even if I look at the documentary.
I want this line to Lerp the xangle (x rotation) from the actual x angle to the desired rotation I assign in the Up Down function. So I want to turn/rotate the ship smoothly with accelerating and descceleration when it reaches it’s desired rotation. That’s the best way I found but if there is a better method please tell me.