0

Im making the basic foundations of an RPG game. Its point and click, using a navmesh. I have set up the player to move to an enemy and attack when in range. The problem i am having is that when the player reaches the enemy (or the chest/interactable) it rotates on the x axis. Im assuming its something to do with the lookAt function using the pivot point of the target object, but i cannot find a solution. Any help directing me to a solution would be amazing. I have scoured sites, forums and the Unity API for a couple of days now. Im sure its something simple, i just cant seem to see it.

Much love

public class clickToMove : MonoBehaviour
{
    // Attack variables
    [Header("Attack")]
    public float attackDistance;
    public float attackRate;
    private float nextAttack;

    //Navigation variables
    private NavMeshAgent navMeshAgent;
    //private bool walking;

    //Animation variables
    private Animator anim;

    //Enemy variables
    private Transform targetedEnemy;
    private bool enemyClicked;

    //Interactable variables
    private Transform targetedObject;
    private bool objectClicked;

    void Awake()
    {
        anim = GetComponent<Animator>();
        navMeshAgent = GetComponent<NavMeshAgent>();
    }

    void Update()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;

        if (Input.GetButton("Fire1"))
        {
            navMeshAgent.ResetPath();
            if(Physics.Raycast(ray, out hit, 1000))
            {
                if(hit.collider.tag == "Enemy")
                {
                    enemyClicked = true;
                    targetedEnemy = hit.transform;
                }
                else if (hit.collider.tag == "Chest")
                {
                    objectClicked = true;
                    targetedObject = hit.transform;
                }
                else if(hit.collider.tag == "Player")
                {
                    //walking = false;
                    navMeshAgent.isStopped = true;
                }
                else
                {
                    //walking = true;
                    enemyClicked = false;
                    navMeshAgent.isStopped = false;
                    navMeshAgent.destination = hit.point;
                }
            }
        }
        if (enemyClicked)
        {
            MoveAndAttack();
        }
        else if(objectClicked && targetedObject.gameObject.tag == "Chest")
        {
            Interaction(targetedObject);
        }
        else
        {
            if (!navMeshAgent.pathPending && navMeshAgent.remainingDistance <= navMeshAgent.stoppingDistance)
            {
                //walking = false;
            }
            else if (!navMeshAgent.pathPending && navMeshAgent.remainingDistance >= navMeshAgent.stoppingDistance)
            {
                //walking = true;
            }
        }
        //anim.SetBool("isWalking", walking);

        //TODO: needs finishing. Still need to lock it to the y axis to stop its rotation being funny.
        if (Input.GetKey(KeyCode.LeftShift))
        {
            //walking = false;
            navMeshAgent.isStopped = true;
            transform.LookAt(ray.origin + ray.direction * ((transform.position - Camera.main.transform.position).magnitude * 0.5f));
        }
    }

    // TODO: still a bug where the player rotates 15 deg on x axis when it reaches target. Has something to do with the Lookat function.
    void MoveAndAttack()
    {
        if (targetedEnemy == null)
        {
            return;
        }
        navMeshAgent.destination = targetedEnemy.position;

        if (!navMeshAgent.pathPending && navMeshAgent.remainingDistance >= attackDistance)
        {
            navMeshAgent.isStopped = false;
            //walking = true;
        }
        else if (!navMeshAgent.pathPending && navMeshAgent.remainingDistance <= attackDistance)
        {
            //anim.SetBool("isAttacking", false);
            transform.LookAt(targetedEnemy);
            Vector3 dirToAttack = targetedEnemy.transform.position - transform.position;

            if(Time.time > nextAttack)
            {
                nextAttack = Time.time + attackRate;
                //anim.SetBool("isAttacking", true);
            }
            navMeshAgent.isStopped = true;
            //walking = false;
        }
    }

    void Interaction(Transform target)
    {
        // set target
        navMeshAgent.destination = target.position;
        //go close to the target
        if(!navMeshAgent.pathPending && navMeshAgent.remainingDistance > attackDistance)
        {
            navMeshAgent.isStopped = false;
            //walking = true;
        }
        //read the info
        else if (!navMeshAgent.pathPending && navMeshAgent.remainingDistance <= attackDistance)
        {
            navMeshAgent.isStopped = true;
            transform.LookAt(targetedObject);
            //walking = false;

            //play animation
            //target.gameObject.getComponentInChildren<Animator>().SetTrigger("Open");

            objectClicked = false;
            navMeshAgent.ResetPath();
        }
    }
}
Neil
  • 1
  • 2
  • Have you tried to add the second parameter of LookAt, to give an additional Up vector? Such as: `transform.LookAt(targetedObject, Vector3.left);` (or Vector3.forward, Vector3.right, etc.) – Kokosbrood Apr 07 '20 at 12:11
  • Hi @Kokosbrood. Yeah i sure have. It still does it. When i left click on the enemy, or the interactable, once the player object has reached its attackDistance, it rotates in the x axis by about 22 degrees. Is there a part of lookAt function that makes the object look at its targets pivot point? Both the enemy and the player are the same size object, each attached to an empty game object. I have tried moving the empty game object down, and the enemy up to effect the pivot, but no luck. – Neil Apr 07 '20 at 12:21

0 Answers0