- Home /
How to get moving player to tilt when turning
Hello,
I am pretty new to Unity, and am working on a simple, 2D Top-Down flying game. When my spaceship thing is turning, I would like it to be able to tilt side to side, to make things a little more interesting. The input is simple left/right, and the ship will be constantly moving. I have a movement script that works:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShipMovement : MonoBehaviour
{
public float Speed;
public float Acceleration;
Rigidbody2D rb;
public float RotationControl;
float MovY, MovX = 1;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
MovY = SimpleInput.GetAxis("Horizontal");
}
private void FixedUpdate()
{
Vector2 Vel = transform.up * (MovX * Acceleration);
rb.AddForce(Vel);
float Dir = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up));
if (Acceleration > 0)
{
if (Dir > 0)
{
rb.rotation += MovY * RotationControl * (rb.velocity.magnitude / Speed);
}
else
{
rb.rotation -= MovY * RotationControl * (rb.velocity.magnitude / Speed);
}
}
float thrustforce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.right)) * 2.0f;
Vector2 relForce = Vector2.left * thrustforce;
rb.AddForce(rb.GetRelativeVector(relForce));
if (rb.velocity.magnitude > Speed)
{
rb.velocity = rb.velocity.normalized * Speed;
}
}
}
And I have thrown together a visual script for the Y-axis tilt:
(I could probably convert that to C# pretty easily if that helps)
Anyway, both of those scripts work fine, separately, but when I try to use them together, or use the movement script on a Parent object and the tilt visual script on a child object, neither will move. I've been working on this problem for a while, and it seems like I've tried everything, including rotation and tilt on the same object (same result), and a bunch of other things (localRotation, snapping an object back and forth underneath the spaceship, then having it 'look' toward that object, etc.) Nothing has worked so far. It seems that movement/rotation and tilt are mutually exclusive, and any answers I have found elsewhere don't seem to work.
I'd really appreciate any ideas on how to get a constantly moving object to rotate/turn, and tilt at the same time!
Your answer
Follow this Question
Related Questions
My vehicle wont perform both the Tilt and Turn 0 Answers
How do I make a 2D object face the direction it is moving in top down space shooter 1 Answer
add force at position with respect to rotation 1 Answer
Tilt an object when rotating and set it back to normal afterwards 0 Answers
Tilt with rotation. 0 Answers