Home>

There are 3 classes, abstract Human and its derivatives Player and Enemy (Enemy has not even tried to do it yet). As a result, on the stage, my player crawls to the side, and does not react to clicks at all, although if you write the logic directly in the Player class, then everything works. Sorry if this looks too silly.

The Human class

using UnityEngine;
namespace Assets
{
    public abstract class Human: MonoBehaviour
    {
        protected readonly byte maxHealth= 100;
        protected string humanName;
        protected byte currentHealth;
        private IWalkToClick walkToClickBehaviour;
        //Behavior for the interface on click
        public void SetWalkToClickBehaviour (IWalkToClick behavior)= >
 walkToClickBehaviour= behavior;
        public void PerformClickTomove ()= >
 walkToClickBehaviour.MoveToClick ();
        public void PerformLocatePositionFromClick ()= >
 walkToClickBehaviour.LocatePosition ();
    }
}

Player Class Player

using UnityEngine;
namespace Assets
{
    public class Player: Human
    {
        public CharacterController Controller;
        private Vector3 StartPosition;
        [System.Serializable]
        public struct PlayerInfo
        {
            public Vector3 position;
            public PlayerInfo (Vector3 position)
            {
                this.position= position;
            }
        }
        [SerializeField]
        private Vector3 direction;
        private PlayerInfo playerInfo;
        private void Start ()
        {
            playerInfo= new PlayerInfo (transform.position);
            StartPosition= transform.position;
            SetWalkToClickBehaviour (new HumanWalkToClick (Controller, transform, .25f));
        }
        private void Update ()
        {
            if (Input.GetKey (KeyCode.Space))
            {
                LocatePosition ();
            }
            Movement ();
        }
        private void LocatePosition ()
        {
            PerformLocatePositionFromClick ();
        }
        private void Movement ()
        {
            PerformClickTomove ();
        }
    }
}

Interface for class implementation

using UnityEngine;
namespace Assets
{
    public interface IWalkToClick
    {
        void LocatePosition ();
        void MoveToClick ();
    }
}

Class with implementation of movement

using UnityEngine;
namespace Assets
{
    public class HumanWalkToClick: IWalkToClick
    {
        private Vector3 StartPosition;
        private Transform HumanTransform;
        private float Speed;
        private CharacterController Controller;
        public HumanWalkToClick (CharacterController controller, Transform humanTransform, float speed)
        {
            Controller= controller;
            HumanTransform= humanTransform;
            Speed ​​= speed;
        }
        public void LocatePosition ()
        {
            RaycastHit hit;
            Ray ray= Camera.main.ScreenPointToRay (Input.mousePosition);
            if (Physics.Raycast (ray, out hit, 1000))
            {
                //Find out where the user clicked
                StartPosition= new Vector3 (hit.point.x, hit.point.y, hit.point.z);
            }
        }
        public void MoveToClick ()
        {
            //If the distance between the object and the click position is greater than 1, then move to the point
            if (Vector3.Distance (HumanTransform.position, StartPosition) >
 one)
            {
                Quaternion newRotation= Quaternion.LookRotation (StartPosition -HumanTransform.position, Vector3.forward);
                newRotation.z= 0f;
                newRotation.x= 0f;HumanTransform.rotation= Quaternion.Slerp (HumanTransform.rotation, newRotation, Time.deltaTime * 10); Controller.SimpleMove (HumanTransform.forward * Speed);
            }
            else
            {
                //What if the distance between the object and the desired point is less than 1
            }
        }
    }
}

I can't immediately understand completely what's what, but there is a sixth sense of the fifth exact, that the problem is what you expect from value types, structures and vectors, the behavior is like reference. Understand where the copy of the structure is created when the argument is passed, and where is the reference to the object. Are you familiar with the ref keyword and what does it do? If yes, then you know what I mean.

aepot2021-12-23 09:43:49

It is probably better to start any manipulations with objects on the scene from FixedUpdate, and not from Update.

aepot2021-12-23 09:45:23