Camera relative player movement?

Hello, Ive been struggling for some time now… Im confined to using an older version of Unity (5.6) which is incompatible with many standard assets, so Ive had to do a lot of scripting myself… that being said, Ive been struggling to get my player movement relative to my camera axes (for a 3d platformer, so rotating the camera and pressing forward or left will rotate and move the character in the respective direction)… Unfortunately I also cant use a characterController either because I need to use my own physics (and every resource ive found involves characterControllers, which has left me a little confused). Here is my code so far. At the moment my player will rotate forward in relation to the camera (and will rotate left and right relative to itself) but wont move left or right… and similarly, I have another script where Im able to move left and right but not rotate. Any help is appreciated, thanks!

void FixedUpdate()
    {
        // Get the horizontal and vertical axis.
        // By default they are mapped to the arrow keys.
        // The value is in the range -1 to 1
        float translation = v * speed;
        float rotation = h * rotationSpeed;

        // Make it move 10 meters per second instead of 10 meters per frame...
        translation *= Time.deltaTime;
        rotation *= Time.deltaTime;

        if (v != 0)
        {
            transform.rotation = Quaternion.Euler(0f, pivot.rotation.eulerAngles.y, 0f);
        }

        // Move translation along the object's z-axis
        transform.Translate(0, 0, translation);
        // Rotate around our y-axis
        transform.Rotate(0, rotation, 0);
    }

Hey, I’m still using Unity5.6.6 on an old laptop. Welcome.

Coincidentally, I might have exactly what you’re looking for, in a Unity5.6.6 project repository no less.

Check out the scene called DemoCameraRotatedControls

This is the actual code driving your player and camera:

https://github.com/kurtdekker/proximity_buttons/blob/master/proximity_buttons/Assets/DemoCameraRotatedControls/RotatedControlsPlayerController.cs

The full repository can be downloaded here:

proximity_buttons is presently hosted at these locations:

https://bitbucket.org/kurtdekker/proximity_buttons

Hmm… I tried the demo scene and it’s close to what I need, but I need my character to rotate on its axis then move, whereas yours seems to rotate and move at the same time (which makes the character move in a sort of a arc, something I came up against in my script also when I tried to translate by my horizontal axis as well as vertical and rotating)

That’s a trivial fix to the script linked above: just zero out the motion each frame when you’re not facing close enough to the direction you want.

Honestly just reading through it Im not so sure Id even know where to start setting up the script myself either for what I need, sorry! I appreciate it though.

But I did find a simple script to do what I was trying to do movement-wise here… still need to figure out how to properly implement player rotation before the movement but its definitely a big step closer, between the unity transform.rotation documentation and maybe trying what you mentioned above (zeroing out the motion each frame when youre not facing close enough to the direction i want) Im sure i can figure it out from here if i mess with it a bit. Thanks!

1 Like