Be the first to post for 'Player Pathfinding To Enemies'!

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’.

Hi @ben. From about 13:30 onwards to the end of the video in Udemy, the video cuts out and a static sound appears. Potential bug?!?

For me, it is around 13:30.
Static picture an noise.

13:30 for me as well

Decided to get the latest source code from git.
Using Unity 5.5.1f1

In Combat Sandbox scene payer is moving OK, but in Ricks Village player and enemies are moving chaotically.
It is like they can not find a path, turning in different directions a lot, one step and again turning

Might have to wait for @ben @Rick_Davidson or @Lucy_Becker

Hi gang, thanks for reporting the issue and sorry for the ****** experience.

It looks like Udemy got hungry in the latter stages of the uploaded and wigged out on its processing. The original video file looks sharp so I’m adding it again. Its currently processing (which can take minutes or hours, we don’t really get an progress bar on that). So, give it a little bit and have a look again and hopefully it should be all fixed.

@softak Thanks for that feedback. I’ll investigate.

Thanks.
I’ve tried different computer and it works now.
Looks like the scene is to complex and needs more powerful computer.
Did not work properly on a laptop without dedicated video card, works fine on a desktop with dedicated video card.

1 Like

I have an odd issue with mine for some reason. When I click anywhere in my game, my player takes like three steps and stops. if I hold the mouse button down he runs to where I am clicking but a single click he just runs in the direction he should then stops running.

Most likely you need to check “Stopping distance” in Nav Mesh Agent.
Make it smaller, like .5

I’ve made a complete mess of my project. I had the weird movement issues so I tried using the player assets from the project git and now I am getting two errors and can’t figure out why they are popping up. When I open up the error, it launches the cs files as it should but it’s code that should be fine. This is the first part I’ve become frustrated with as I can’t figure it out.

edit Ok, going to leave this here in case someone else ever runs into this issue. I figured it was due to having the class in another location and I initially checked the player folder only and it wasn’t there. However, I was manually moving files into those folders and I dropped it into a scenes folder by accident. So if you’re silly like me and drop the class elsewhere by accident, this is where this error comes from lol. Oops!

I’ve just doubled checked this video and confirm it plays OK now.

Hi, really enjoying the course so far, thanks! Not sure if it’s covered later but I’m finding an issue when using the pathfinding to enemies setup whereby I have a hill at the start which my camera is clipping through. This means whenever I click to move it’s using a hit point on the hill rather than the floor so I can’t move away from it. I know I could change the design so there’s no hill, or move the camera but I like the dynamic of the starting position with the hills etc.

Is there a way to address this?

Cheers
Pete

Hi Pete, are you able to carry-on with the bug as we’ll be revisiting pathfinding soon?

Hi Ben, sure I can keep working through, it’s not stopping progress just a frustratation at not being able to do what I wanted, good to know it will be revisited. :+1:t3:

I got that error once. I guess I moved some files while editing it, and Visual Studio reloaded the file or something… anyway, yeah, I had two copies of the script: one in my script folder (as intended) and one in my root asset folder.

As far as I know, the code in your .cs file should be fine. The problem is that somewhere, in another script maybe, there’s already a Class with that same name.

If I’m right, you can solve the problem by simply renaming the duplicate class (and .cs file), but it would probably be better to find the duplicate file, if you can.

I think the best way to address this is to prevent the camera from hitting the hill in the first place. A camera controller can be made that will fly it around or above the hill instead of flying through it. A common camera trick is to move the camera closer to the player if a raycast detects an intervening terrain obstacle.

The only other solution I can think of is to use RayCastAll to get an array of different hits, and them use an IF condition to somehow filter through the hits. It will take some trial and error to get a filter you are happy with. (Last terrain obstacle hit? Elevation within a certain range?) It doesn’t seem like a good solution though, because if the player clicks on a hill, how will you know if he’s actually targeting the hill or a position behind the hill? The programming aspect of it can be done, but you have UI decisions to consider so that you know the program is always doing what the player wants.

Personally, I prefer the solution of dynamically making the camera avoid obstacles. It takes longer to code, but I think it’s just better in the long run. Also, I personally don’t like it when the camera clips like that.

edit: I suppose what you could do is raycast upward, and if you hit terrain then you know you’re in a hill. Once you know you’re in a hill, you can either move the camera or send out a RayCastAll (instead of RayCast). This way you won’t get any undesired behavior when you’re camera isn’t inside a hill.

1 Like

I’m getting error for enemy/walkable layernumber not existing in the current context in playermovement. I think it looks fine here:
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.AI;
using UnityStandardAssets.Characters.ThirdPerson;

[RequireComponent(typeof(NavMeshAgent))]
[RequireComponent(typeof(AICharacterControl))]
[RequireComponent(typeof (ThirdPersonCharacter))]
public class PlayerMovement : MonoBehaviour
{

//[SerializeField] float walkMoveStopRadius = 0.5f;
//[SerializeField] float attackMoveStopRadius = 5f;

GameObject walkTarget = null;
AICharacterControl aiCharacterControl = null;

ThirdPersonCharacter Character = null;   // A reference to the ThirdPersonCharacter on the object
CameraRaycaster cameraRaycaster = null;
Vector3 currentDestination, clickPoint;

bool isInDirectMode = false; 
    
private void Start()
{
    cameraRaycaster = Camera.main.GetComponent<CameraRaycaster>();
    Character = GetComponent<ThirdPersonCharacter>();
    currentDestination = transform.position;
	aiCharacterControl = GetComponent<AICharacterControl> ();
	walkTarget = new GameObject ("walkTarget");

	cameraRaycaster.notifyMouseClickObservers += ProcessMouseClick;
}

void ProcessMouseClick(RaycastHit raycastHit, int layerHit) {
	switch (layerHit)
	{
		case enemyLayerNumber:
			GameObject enemy = raycastHit.collider.gameObject;
			aiCharacterControl.SetTarget(enemy.transform);
			break;
		case walkableLayerNumber:
			walkTarget.transform.position = raycastHit.point;
			aiCharacterControl.SetTarget(walkTarget.transform);
			break;
		default:
			Debug.LogWarning("Don't know how to handle mouse click or movement");
			return;
	}
		
}

// Fixed update is called in sync with physics

// private void FixedUpdate()
//{

//	if (Input.GetKeyDown(KeyCode.G)) { 
//		isInDirectMode = !isInDirectMode; 
//		currentDestination = transform.position; //clears target
//	}
//
//	if (isInDirectMode) {
//		ProcessDirectMovement ();
//	} else {
//		ProcessMouseMovement ();
//		WalkToDestination ();
//	}
//}

//private void WalkToDestination() {
//	var playerToClickPoint = currentDestination - transform.position;
//	if (playerToClickPoint.magnitude >= 0) {
//		Character.Move (currentDestination - transform.position, false, false);
//	} else {
//		Character.Move (Vector3.zero, false, false);
//	}
//}

//private void ProcessMouseMovement() {
//	if (Input.GetMouseButton (0)) {
//		clickPoint = cameraRaycaster.hit.point;
//		switch (cameraRaycaster.currentlayerHit) {
//		case Layer.Walkable:
//			currentDestination = ShortDestination (clickPoint, walkMoveStopRadius);
//			break;
//		case Layer.Enemy:
//			currentDestination = ShortDestination (clickPoint, attackMoveStopRadius);
//			break;
//		default:
//			print ("SHOULDN'T BE HERE");
//			return;
//		}
//	}
//}

//TODO make it get called again
private void ProcessDirectMovement() {

	print ("Direct Movement"); 

	float h = Input.GetAxis("Horizontal");
	float v = Input.GetAxis("Vertical");

	Vector3 CamForward = Vector3.Scale (Camera.main.transform.forward, new Vector3 (1, 0, 1)).normalized;
	Vector3 Move = v * CamForward + h * Camera.main.transform.right;

	Character.Move (Move, false, false);
}

Vector3 ShortDestination(Vector3 destination, float shortening) {
	Vector3 reductionVector = (destination - transform.position).normalized * shortening;
	return destination - reductionVector;
}

void OnDrawGizmos() {
	//Movement Gizmos
	Gizmos.DrawLine (transform.position, currentDestination);
	Gizmos.color = Color.black;
	Gizmos.DrawSphere (currentDestination, 0.1f);
	Gizmos.DrawSphere (clickPoint, 0.15f);

	//Draw Attack Sphere
	Gizmos.color = new Color(255f,0f, 0, .5f);
	Gizmos.DrawWireSphere (transform.position, attackMoveStopRadius);
}

//TODO Fix Nullreference exception reference not set to instance of object
// line 45 playermovement.fixedupdate.

}

2:37 just became my Facebook post for today.

Privacy & Terms