Rotating a 3D object with movement along a 2D background at an angle
Sorry if this is a silly question, I'm pretty new to unity but have been trying to figure this one out all day..
So I'm making a test scene for a game with orthographic, top down perspective. The background is a 2d painting and the character is a 3d model, similar to games like Bastion and Transistor.
Like so:
Problem is, the character needs to be at an angle to achieve the proper perspective, which throws it off the default axes that are aligned with the background.
So when I try to move it, it moves in and out of the background. I fixed this easily by parenting it to an Empty Game Object, which could then move it along the proper axes.
The real challenge has come with trying to rotate the model along with its movement, since the rotation still needs to be along this misaligned z-axis of the child model and not the parent. To achieve this, I wrote separate scripts for the empty Parent Object for the movement:
[SerializeField]
private float _speed = 5.0f;
public float horizontalInput;
public float verticalInput;
void Update()
{
Movement();
}
public void Movement()
{
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
transform.Translate(Vector3.right * _speed * horizontalInput * Time.deltaTime);
transform.Translate(Vector3.up * _speed * verticalInput * Time.deltaTime);
}
And then the closest I've gotten with the Child Object rotating correctly is :
public PlayerController parentScript;
void Update()
{
Vector3 movement = new Vector3(parentScript.horizontalInput, parentScript.verticalInput, 0);
if (movement != Vector3.zero)
{
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(movement.normalized, Vector3.up), 0.2f);
}
This is able to rotate the object, but it resets the axes to standard (therefore not maintaining the angle I need) and is rotating on the wrong axes. Changing the Vector3.up to either forward, right, or any of the other options seems to just rotate it weirdly along the x-axis, and I need it to rotate on the misaligned z.
I've considered changing the rotation in Blender before importing the model to the scene, but then I'm worried that A. the rotation still won't be at the right angle, and B. any future animations I'll do will be dramatically confused.
I hope this was articulated clearly enough... I think I'm very close but something is wrong with Quaternion.LookRotation... is there a better method I could use?
You may also try looking at using transform.localrotation ins$$anonymous$$d of transform.rotation. Did you ever find a solution to your problem?
Answer by DarkestAngel · Jan 23, 2020 at 06:38 AM
Have you tried rotating your scene and camera instead of the character? Then you can rotate your character normally?