#enemy ai help

1 messages · Page 1 of 1 (latest)

fleet oriole
#

I was following a tutorial and ran into a problem, my enemy walked through walls and couldn't use an animation, so I switched to rigidbody movement instead of transform movement but it started teleporting straight to the player.

#

here is the code I was using

#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class eye_behavior : MonoBehaviour

{
    public float LookRadius = 10f;

    public bool stopright;


    Transform _target;
    NavMeshAgent _agent;
    // Start is called before the first frame update
    void Start()
    {
        _target = PlayerManager.instance.Player.transform;
        _agent = GetComponent<NavMeshAgent>();
    }

    // Update is called once per frame
    void Update()
    {
        float distance = Vector3.Distance(_target.position, transform.position);

        if(distance <= LookRadius)
        {
            //_agent.SetDestination(_target.position);
            GetComponent<Rigidbody>().MovePosition(_target.position);

            if (distance > 0f)
            {
                FaceTarget();
            }
        }
        stopright = Physics.Raycast(transform.position, Vector3.forward, .67f);
        if(stopright)
        {

        }
    }

    void FaceTarget ()
    {
        Vector3 direction = (_target.position - transform.position).normalized;
        Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, direction.y, direction.z));
        //transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
        GetComponent<Rigidbody>().MoveRotation(Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f));
    }
    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, LookRadius);
    }
}```
frank monolith
#

https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html

Moves the kinematic Rigidbody towards position.

Rigidbody.MovePosition moves a Rigidbody and complies with the interpolation settings. When Rigidbody interpolation is enabled, Rigidbody.MovePosition creates a smooth transition between frames. Unity moves a Rigidbody in each FixedUpdate call
you're just telling it to move to the player position immediately

fleet oriole
#

so what can I change in the code

frank monolith
#

you could instead let the NavMeshAgent move the object and if you bake your navmeshsurface so that walls are not included in the walkable areas then it will JustWork™️

fleet oriole
#

but then how would I get the enemy to move to the player

frank monolith
#

uncomment this line: //_agent.SetDestination(_target.position);

fleet oriole
#

k I'll try ig

#

sorry, I took a bit, but how can I bake my navmeshsurface so that walls are not included?

#

fyi Im using unity 3.7.0

#

or unity 2020.3.27f1

frank monolith
fleet oriole
#

this worked perfectly, thank you so much, however, how can I add an animation to him?

toxic panther
fleet oriole
#

Im sorry, I don't know what this means

#

Im referring to Tree Float parameter

#

I only need to use one animation, see, my enemy is a floating eyeball, I want to add an animation that moves it up slowly and back down slowly over the span of 2 seconds, this will be "turned on" no matter what. but when I have it on it locks the x and z coordinates and doesn't allow my enemy to move towards my player, while I only want it to animate the y coordinate.

toxic panther
#

should be pretty easy with like a Sine wave or something

fleet oriole
#

i'll look into it. thank you so much

toxic panther
#

I know you can animate using animator but i think it overrides the transforms so that messes with it I think

fleet oriole
#

yeah

toxic panther
#

Im not sure I do basic animations 😛