Forum rules - please read before posting.

How to make NPC walk backwards?

I'm trying to move NPC character few steps backward while still facing player. Is it possible in AC?
SetMoveDirectionAsBackward doesn't seem to work or I'm doing something wrong because my NPC is always turning to face direction towards he moves.

Comments

  • What's the context? Is this for a 2D/3D game, and is it only during a specific moment?

    In 2D, you can use the Character: Change rendering Action to temporarily force the character to face a given direction.

    In script, SetMoveDirectionAsBackward would need to be called each frame, but you may have luck just setting their turnSpeed to zero.

  • edited January 28

    It's a 3D game, it's only during specific moment.
    When I set turnSpeed to zero, character moves forward but never reaches destination, because destination is in the back.
    When I call SetMoveDirectionAsBackward every frame - character speed float in animator goes beyond zero but he still turn toward direction and move like normal.
    Here's the code:

        public Char Char;
        Vector3 point;
        public float distance;       
        private void Update()
        {
            ///Char.turnSpeed = 0;
            Char.SetMoveDirectionAsBackward();
        }
        void OnTriggerEnter(Collider other)
        {
                Vector3 backwards = -transform.forward;
                point = transform.position + (backwards * distance);
        }
        private void OnTriggerExit(Collider other)
        {
            StartCoroutine(Wait());
        }
    
        private void OnTriggerStay(Collider other)
        {
    
    
            Char.MoveToPoint(point, false,false);
    
        }
    
        IEnumerator Wait()
        {
            yield return new WaitForSecondsRealtime(5);
    
            Char.Halt(true);
    
        }
    
  • edited January 30

    This does it:

    public Char character;
    public Marker moveMarker;
    public Marker faceMarker;
    public bool doMove;
    
    void Update ()
    {
        if (!doMove) return;
        if (Vector3.Distance (moveMarker.transform.position, character.transform.position) < 0.01f)
        {
            character.charState = CharState.Idle;
            doMove = false;
        }
        else
        {
            character.charState = CharState.Move;
            character.SetMoveDirection (moveMarker.transform.position - character.transform.position);
            character.SetLookDirection (faceMarker.transform.position - character.transform.position, true);
        }
    }
    
  • Thank you, I had to modify it slightly for my needs but this approach works fine. Although speed parameter in animator is still >0 but it's easy to make some workaround.

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.