#help with enemy ai code?
1 messages · Page 1 of 1 (latest)
was following this enemy ai video (https://www.youtube.com/watch?v=xppompv1DBg&list=PLPV2KyIb3jR4KLGCCAciWQ5qHudKtYeP7&index=11) and ran into a problem, my enemy walks through walls and does not move if an animation is set. can someone help?
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);
}
}```
Let’s make some enemies!
❤️ Donate: https://www.paypal.com/donate/?hosted_button_id=VCMM2PLRRX8GU
●The Playlist: http://bit.ly/2xiecdD
● Sebastian's Channel: https://www.youtube.com/user/Cercopithecan
● Download the assets: http://bit.ly/2u4rcEX
● Download the source code: http://bit.ly/2uecCew
● Singleton Patterns: http://wiki.unity3d.com/i...
!code
📃 Large Code Blocks
Use links to services like:
https://gdl.space/, https://paste.ofcode.org/, https://hatebin.com/, https://paste.myst.rs/, https://hastebin.com/
📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.