Forum rules - please read before posting.

Unwanted player character transform on turn

Heyo!

My 3D game uses Direct movement "Relative to Camera" and is seen more or less horizontally. I have constrained the player to only be able to move left and right, which works fine along the X axis of my scene.

However, this only works fine if I have "Turn instantly under player control?" activated. If I deactivate it, the player character will translate slightly on Z every time she turns from left to right or vice versa. And this is an additive problem so every time she turns she goes a little deeper into the scene. She always makes a left-hand turn for some reason. I do not understand what is causing this offset...

Perhaps if it were left-right-left-right this would compensate somehow, and she would remain roughly in the same Z all the time.

My player character does not move with rigidbody and is controlled using a character controller.

Unity 2020.3.16f1 AC 1.74.2

Thoughts?

Thank You!

t

Comments

  • edited November 2021

    A direct-controlled Player will move forward in the direction they face, so some movement along the Z-axis would be expected if turning is not instant.

    If you wish to lock their position along the Z-axis, you should be able to with a simple script (ForceZPos.cs) attached to the Player's root:

    using UnityEngine;
    
    public class ForceZPos : MonoBehaviour
    {
    
        public float forcedZPosition;
    
        private void LateUpdate ()
        {
            transform.position = new Vector3 (transform.position.x, transform.position.y, forcedZPosition);
        }
    
    }
    
  • You're right, the z movement makes sense the way you explain it.

    Your solution solves the core problem during gameplay, however I need to be able to walk the character to a new Z position from actionlists. For an instance if the player chooses to interact with an elevator, an actionlist runs where the character will walk into the elevator, thus moving in Z.

    Ideally there'd be some magic bool somewhere that checks whether the player is moving the character around, and if so, locks Z, and if not, doesn't.

    Any such luck? And if so what would said script look like?

    Thanks again and thanks for the rapid response!

    t

  • You can wrap the re-positioning code around a check for whether or not you're currently in gameplay, so that it has no effect during cutscenes:

    using UnityEngine;
    
    public class ForceZPos : MonoBehaviour
    {
    
        public float forcedZPosition;
    
        private void LateUpdate ()
        {
            if (AC.KickStarter.stateHandler.IsInGameplay ())
            {
                transform.position = new Vector3 (transform.position.x, transform.position.y, forcedZPosition);
            }
        }
    
    }
    
  • Thanks, Chris! Can't wait to try this out. :smile:

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.