Kinematic rotation/movement problem

Hi, i’m new at scripting so hopefully i describe my problem correctly. :slight_smile:

I’m creating a side-scrolling shooter-type game (2.5d). I made the player’s craft as a kinematic rigidbody, since it seems that the physics engine would complicate matters unnecessarily for the purposes of this game. The player only controls up and down, and the ship moves of it’s own accord horizontally unless a booster is applied.

So to begin with the rotation of the craft was static, with it facing forwards the whole time. However then I decided I want the craft to rotate slightly when it moves up or when it moves down, to look more realistic. The problem I have is that the rotation means that the axis of the ship changes, so that it is no longer vertical relative to the world, and when the player moves the ship ‘up’, it actually moves up and to the left (‘up’ according the the local axis of the gameobject).

My question is how I do one of the following:

  • rotate the craft without changing its axis?
    OR
  • move the craft vertically according the world axis rather than the crafts local axis?

Or any other comments which shed light on the problem. Like I said I’m new at this, so maybe I have approached it in completely the wrong way? I’m working in c#, script below:

void Update () {
		
		float h = thrust * Time.deltaTime;
		float v = Input.GetAxis("Vertical") * Time.deltaTime * moveSpeed;
		debugDisplay = h;
	
		
		//ROTATION 
		//rotation based on vertical motion
		if (v > 0.1){
			Debug.Log("rotate up");
			Quaternion target = Quaternion.Euler(0,0,45);
			transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * rotationSmooth);
		}
		if (v < 0.1  v > -0.1){
			Debug.Log("rotate to centre");
			Quaternion target = Quaternion.Euler(0,0,0);
			transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * rotationSmooth);
		}
		if (v < -0.1){
			Debug.Log("rotate down");
			Quaternion target = Quaternion.Euler(0,0,-45);
			transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * rotationSmooth);
		}
		
		// performs the translate (now vertical motion only)

		transform.Translate(h,v,0);
		
		
		// constrains vertical movement
		if (transform.position.y > maxTop){
			transform.position = new Vector3(transform.position.x, maxTop, transform.position.z);
		}
		if (transform.position.y < maxBottom){
			transform.position = new Vector3(transform.position.x, maxBottom, transform.position.z);
		}
		
		// BOOSTER MECHANICS
		// when fire is pressed the boost gets used
		if (Input.GetKey ("space")  (boostLevel > 0f)){
			// code which increases the speed while the key is pressed, until it is unpressed here.
			thrust += 0.25f;
			boostLevel -= Time.deltaTime * boostRateOfDecay;
			Debug.Log("boost was pressed");
			gameObject.SendMessage("MultiplierChange");
		}
		// when fire is not pressed, thrust goes down.
		else if (thrust > normalThrust){
			thrust -= postBoostSlowDown * Time.deltaTime;
			gameObject.SendMessage("MultiplierChange");
		}
		
		// fixeds negative float figures on boostLevel
		if (boostLevel < 0f){
			boostLevel = 0f;
		}
		// fixes thrust less than normal speed
		if (thrust < normalThrust){
			thrust = normalThrust;
		}
	}

Assuming Y is still vertical and Z is now forward you’d make the gameObject tilt slightly on the Z Axis when the button is pressed(I believe) by adding a slight rotation. Hope that helps point you in the right direction…

In this case the ship is moving horizontally from left-to-right along the x axis, with y as vertical. so it rotates in the x axis fine, but then ‘up’ is no longer vertical relative to the world, because the object’s axis has rotated. so unless i’m misunderstanding you, tilting the z axis won’t help?

maybe a picture makes it clearer what i mean

try rotating using the Space.Self

transform.Rotate(0,1,0 , Space.Self);

got what i wanted by using a separate parent object, however i will also try this out, thanks.