If you’re reading this, there probably aren’t very many posts yet. But don’t worry, you can be the first! Either create a new post or just reply to this one to say ‘hi’.
I have an odd issue for this lesson as well. When I start my game, my enemies will sprint the length of the map to my player. So they aren’t just waiting for me to get inside a radius to attack my player.
It’s not a bug, it’s a feature!
If you dragged the Player into the Target spot manually to test it, make sure you removed it so it’s only being set via code!
Do I have to do something special to the navmesh to make it recognise obstacles? I have got the enemies charging my player when I go too close, and giving up when I run far enough away, but if there is a block in the way they just stand there running at it. I assume the point of the navmesh is that it pathfinds the way around such things?
We’ll make some improvements to the navmesh soon, but meanwhile…
- Select a mesh sub-object in the Hierarchy
- Select the Navigation window
- Set the Navigation Area there…
I hope this helps
Thank you, that’s so cool! I even got my slopey, steppy ramp working by setting it to jump.
got a weird bug
private void Start()
{
thirdPersonCharacter = GetComponent<ThirdPersonCharacter>();
player = GameObject.FindGameObjectWithTag("Player");
}
private void Update()
{
float distanceToPlayer = Vector3.Distance(player.transform.position, transform.position);
if(distanceToPlayer <= attackRadius)
{
characterControler.SetTarget(player.transform);
}else
{
characterControler.SetTarget(transform);
}
}
same as we all got. Find Refrence in Start but if im in Attack Range i got this Error NullReferenceException: Object reference not set to an instance of an object Enemy.Update () (at Assets/Scripts/Enemy.cs:45)
its all setup correct.But i cant assign i target in runtime…
Combined both Scripts and now it works.
Hello everyone.
I have a strange bug regarding the enemy animation.
Code wise everything runs OK, the enemy is detecting me when I enter the attackRadius variable, and he is running towards me, but the enemy is rotating in the Y axis like crazy, is funny, but the run animation is not displaying correctly.
I’ve re done the segment several times to be sure I followed all the steps, and I can’t see anything code wise that goes wrong.
In the next segment, player also moves with nav mesh and the same problem is happening to my player run animation.
Plz help.
Thank you.
I ran into a problem where my enemies would be stuck in a jumping animation when dropped into a scene. The problem turned out to be the ground check distance in the 3rd person character controller component. I made it a bit bigger.
Thanks, that’s been a major source of frustration that I’ve had with the player character as well as the npcs. My player just broke on me without me having a clue what I’d changed to cause it . Just started from scratch with the player and inexplicably, it didn’t have an issue the second time, but then I got to the enemies and here I was again with the same issue.
Having another problem though that doesn’t make any sense to me. The enemy will run into me and chase for a little, but ultimately, the target will change to itself, and it will never update back to making the player the target, no matter where the player is. Anybody have an answer to that?
I get nullreference exception as soon as I launch game and it won’t let me unpause it’s talking about the line in the else statement. Here is the code
using System.Collections;
using UnityStandardAssets.Characters.ThirdPerson;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour {
[SerializeField] float maxHealthPoints = 100f;
float currentHealthPoints = 100f;
AICharacterControl aiCharacterControl = null;
GameObject player = null;
[SerializeField] float attackRadius = 4f;
public float healthAsPercentage
{
get
{
return currentHealthPoints / maxHealthPoints;
}
}
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag("Player");
aiCharacterControl = GetComponent<AICharacterControl> ();
}
// Update is called once per frame
void Update () {
float distancetoPlayer = Vector3.Distance (player.transform.position, transform.position);
if (distancetoPlayer <= attackRadius) {
aiCharacterControl.SetTarget (player.transform);
} else {
aiCharacterControl.SetTarget (this.transform);
}
}
}
Hello guys,
This is my first post here and after following this course I wanted to make the NPC AI to return back to their origin once I leave their area of attack instead of staying in one place… I managed to alter original code with what i want but my problem is that on Start() the aiCharacterOrigin = aiCharacterControl.transform; is changing inside the start() on every step that the enemy takes.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
public class Enemy : MonoBehaviour {
[SerializeField] float maxHealthPoints = 100f;
[SerializeField] float attackRadius = 4f;
float currentHealthPoints = 100f;
Transform aiCharacterOrigin;
AICharacterControl aiCharacterControl = null;
GameObject player = null;
public float healthAsPercentage
{
get {
return currentHealthPoints / maxHealthPoints;
}
}
void Start()
{
player = GameObject.FindGameObjectWithTag ("Player");
aiCharacterControl = GetComponent<AICharacterControl> ();
aiCharacterOrigin = aiCharacterControl.transform;
}
void Update()
{
float distanceToPlayer = Vector3.Distance (player.transform.position, transform.position);
if (distanceToPlayer <= attackRadius) {
aiCharacterControl.SetTarget (player.transform);
} else {
aiCharacterControl.SetTarget (aiCharacterOrigin);
}
}
void OnDrawGizmos()
{
//Draw movement gizmos
Gizmos.color = Color.black;
print ("From: [" + transform.position + "] To: [" + aiCharacterOrigin.transform.position + "]");
//trying to draw a gizmo line from curent enemy place to the origin of the enemy
Gizmos.DrawLine (transform.position, aiCharacterOrigin.transform.position);
//attack sphere
Gizmos.color = new Color(255f, 0f, 0, .5f);
Gizmos.DrawWireSphere (transform.position, attackRadius);
}
}
Strange bug for me, I can get the enemy to follow fine, but everytime it hits a collider that isn’t the terrain, it’s stuck in the falling animation. I’ve tried scouring the Third person character script for reference to what it considers to be “ground” but can’t find any layer reference to set to walkable. Is it hidden somewhere else deeper in unity scripts? Is anyone else having this issue?
Hi Ismael, I know this has been a long time since posting but maybe you remember if this got solved or not? I don’t get the behaviour on the player, but when I get near an enemy whilst on a terrain (this works fine on a ground plane/cube) , the enemy starts zig-zagging and wobbling towards me.
Anyone who would get here (like me earlier) and having the “wobbly character issue” , Section 49 “A message from the future” gives a fix for this … hint , it has to do with the adjustable parameters on the Thirdperson character script.