2D Sprite moving on Y-Axis because of his X-Axis rotation

Hi ! French here, I’m totally new to Unity (And coding by the way) but I think what I want to do is simple. My world is 3D, my sprite 2D but it needs to be rotated on X-Axis. I think my code is good for classic movement but because of the rotation, the sprite is shrinkig (cause of the Y-Axis movement). Is what I want possible ? The sprite only move on X and Z axis even with a rotation ? Thanks in advance !
(CharacterController is refering the Unity built-in one.)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class déplacementPlayer : MonoBehaviour
{
    public CharacterController controller;
    public float speed = 0.5f;

    void Update()
    {
        float horizontal = Input.GetAxisRaw("Horizontal");
        float vertical = Input.GetAxisRaw("Vertical");
        Vector3 direction = transform.forward * vertical + transform.right * horizontal;
        controller.Move(speed * direction * Time.deltaTime);
    }
}

One of the easiest ways to solve most rotation problems like this is to put more GameObjects into the hierarchy.

For instance, instead of just your sprite, you might put:

PivotAroundYAxis (empty)
PivotAroundXAxis (empty)
TheActualSpriteHere (with graphics)```

That way you just rotate another transform via its ```.localRotation``` component.
1 Like

Alright thanks so much for the quick reply. The problem is that I don’t really understand your last sentence. what do you mean by “you just rotate another transform via its .localRotation component.” ?
I don’t understand what (and mostly how/why) I must rotate (The sprite ? One of the empty GameObject ? The rotated one ?)
I’m really sorry i’m such a newbie and I don’t to want be harrassing or something like that but I have some interrogations.
I’ve done what you said in the hierarchy. Can you please be more precise ? Thanks a lot for time and implication :slight_smile:

Studying your code some more, I think your problem is this:

Your base controller will rotate for you (automatically when you call .Move()) but you do NOT want your sprite to rotate because then it will get smaller and face away from you.

So you need to have an extra Gameobject where the CharacterController is, but put the sprite on a child GameObject.

Try this approach for your GameObject structure:

MyPlayerSpriteGraphics```

When you use the above structure, add this script to the ```MyPlayerSpriteGraphics``` GameObject, which will make it always face the camera:

using UnityEngine;

public class RotateIdentityLateUpdate : MonoBehaviour
{
void LateUpdate()
{
transform.rotation = Quaternion.identity;
}
}

1 Like

Ok first of all, really thanks for all. Did exactly what you said but the problem’s still here. I think i’ts my fault and maybe I given a bad explanation of my situation.
My project is kind of 3D environements and 2D sprites (= rotated 50° on X-Axis) with a “higher(upper?)-wiew” (sorry don’t know how to traduce it) camera (e.g ~like in Pokemon HGSS or B/W).
I’m not a mastermind but I understood that movement is governed by rotation, because when the sprite is not rotated, i’m rid of the problem : It only move on 1 axis at the time.
Without any aggressivness saying that, you said “the sprite will get smaller and get away from you” but it’s only partially true : the main problem is the sprite moving on 2 axis when I want to move Up/Down, not just move away from camera (not at all the same problem and will be a mess when I will implement environements).
Maybe it’s clearer ? Maybe it’s what your understood earlier ? I don’t know if I’m missing something evident but now I can’t be more precise haha.
Thanks you so much for time and implication ! :slight_smile:

Is this the motion you’re looking for? See enclosed package and try out the UnityExampleCharMoverSprite scene.

6958298–819314–UnityExampleCharMover.unitypackage (286 KB)

1 Like

Dude, I think you’re my saivor ! The video was exactly that, just the sprite wasn’t inclined on X-Axis, but when modifying some parameters I finally arrived to something very close (quite perfect for sure) I had in mind. Thank you so much for all your job with me ! Take care and really, one last time : Thanks !!

1 Like