#enemy ai help
1 messages · Page 1 of 1 (latest)
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);
}
}```
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
so what can I change in the code
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™️
but then how would I get the enemy to move to the player
uncomment this line: //_agent.SetDestination(_target.position);
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
read the documentation pinned in #🤖┃ai-navigation to learn how to use unity's navmesh system
this worked perfectly, thank you so much, however, how can I add an animation to him?
tie navmesh agent velocity to Blend Tree float parameter
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.
just animate it with code?
should be pretty easy with like a Sine wave or something
i'll look into it. thank you so much
I know you can animate using animator but i think it overrides the transforms so that messes with it I think
yeah
Im not sure I do basic animations 😛