Forum rules - please read before posting.

Keep an NPC away from an object. Help.

edited April 2023 in Technical Q&A

Is there any resource similar to or equal to that component of the NPC '' Keep out of Player 's way.'' in AC?

I have an Npc that throws a bomb to attack the Player and I want other people or NPCS in the game to stay away from the bomb, fleeing at an adjustable distance.

Comments

  • edited April 2023

    Not built-in, but you can attach a custom script that runs equivalent code:

    using UnityEngine;
    using AC;
    
    public class NPCAvoidObject : MonoBehaviour
    {
    
        public NPC npc;
        public GameObject objectToAvoid;
        public float minDistance = 1f;
    
        void Update ()
        {
            if (npc.charState == CharState.Idle && Vector3.Distance (npc.Transform.position, objectToAvoid.transform.position) < minDistance)
            {
                Vector3 relativePosition = npc.Transform.position - objectToAvoid.transform.position;
                if (relativePosition == Vector3.zero) relativePosition = new Vector3 (0.01f, 0f, 0f);
                Vector3[] pointArray = TryNavPoint (relativePosition, relativePosition.magnitude);
                int i = 0;
    
                if (pointArray == null) // Right
                {
                    pointArray = TryNavPoint (Vector3.Cross (npc.Transform.up, relativePosition.normalized), relativePosition.magnitude);
                    i++;
                }
                if (pointArray == null) // Left
                {
                    pointArray = TryNavPoint (Vector3.Cross (-npc.Transform.up, relativePosition.normalized), relativePosition.magnitude);
                    i++;
                }
                if (pointArray == null) // Towards
                {
                    pointArray = TryNavPoint (-relativePosition, relativePosition.magnitude);
                    i++;
                }
                if (pointArray != null)
                {
                    if (i == 0)
                    {
                        npc.MoveAlongPoints (pointArray, false);
                    }
                    else
                    {
                        npc.MoveToPoint (pointArray[pointArray.Length - 1], false);
                    }
                }
            }
        }
    
        protected Vector3[] TryNavPoint (Vector3 _direction, float currentDistance)
        {
            Vector3 _targetPosition = npc.Transform.position + (minDistance - currentDistance) * 1.2f * _direction.normalized;
            if (SceneSettings.CameraPerspective == CameraPerspective.ThreeD)
            {
                _targetPosition.y = npc.Transform.position.y;
            }
            Vector3[] pointArray = KickStarter.navigationManager.navigationEngine.GetPointsArray (npc.Transform.position, _targetPosition, npc);
            if (pointArray.Length == 0 || Vector3.Distance (pointArray[pointArray.Length - 1], npc.Transform.position) < minDistance * 0.6f)
            {
                return null;
            }
            return pointArray;
        }
    
    }
    

    If you're looking to implement an action game with such mechanics, however, it sounds like your game would be better suited to using a different asset to AC - which is intended for traditional adventure games.

  • These errors appear.

    https://uploaddeimagens.com.br/imagens/UR6vCSs

    Assets\LeaoDevorador.2.5d\ScriptsPersonalizados\NPCAvoidObject.cs(56,94): error CS0120: An object reference is required for the non-static field, method, or property 'Transform.position'

  • https://uploaddeimagens.com.br/imagens/WZRUTpw

    I filled in the fields, I don't know if I did it correctly, I reduced the errors leaving 3 that I don't know what they are.

    https://uploaddeimagens.com.br/imagens/B5UBFvg

  • I've updated the code above - try it now.

  • It worked. Thanks.

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.