Investigating line of sight - Problem solved by just using brainpower

in #unity8 years ago (edited)

So I solved it! I actually ended up doing it without the DrawSolidArc debugging code though that was useful to learn about so if I'm experimenting with other stuff I may try it again. It turns out that there was nothing necessarily wrong with the code provided by the developer in his tutorial, but it turns out it was out of date, it came to me when he was talking about the raycast projecting from the enemy's feet. This simply doesn't happen in the later versions of Unity, this is a bit complicated to explain for absolute newbies, but what happens is when you use something like Physics.Raycast to project a ray it put out an invisible line to detect the enemy, now what happens in the latest version of Unity the starting point of the ray is what is called the centre pivot. The centre pivot is a term used for those red, green and yellow blue arrows you use to manipulate objects within the 3D space, the ray comes from the absolute centre or if it is targeting a gameobject projects to the absolute centre. I remember this from when I was learning about building placement because I rant into the exact same problem.

So the TLDR of all of this is that the code given to me by this tutorial was that it was moving the line of sight cone way above my enemy capsule which is why I could end up triggering it by jumping on it's head but it did absolutely nothing when I was directly in front of it.

https://gfycat.com/CavernousFineBrahmancow

The code is now working almost perfectly, however I have a new problem as ever with programming, it's a bit difficult to replicate reliably but I'm pretty sure that what is happening after perusing posts on the subject is that my line of sight code is not updating every frame. What this means is that there are times when the code works brilliantly like in the gif I provided but then even after I leave the line of sight the enemy stays red no matter what. So in order to solve this I will look into how to get my line of sight code into the update function so the game engine is constantly checking for the player rather than getting confused and stalling itself if the player doesn't go in and out of the collider after awhile.

After I solve this problem I guess the next thing to do is get a timer set up for the player detection so I have a chance to run away and get the enemy sight being blocked by various bits of cover as it searches for me.

Note: Hope the gif looks a lot better quality for you all, finally found a decent host for them so that means I'll be able to do more little demonsration clips for you, also getting a lot better at recording.

Sort:  

Isn't coding fun? Local space center, or an objects root is what your talking about. And can be changed with just about any 3d software. For your last issue.....
Are you updating on Update, or FixedUpdate?

Neither, I'm using the OnTriggerStay function so I need to find out how to put it into the Void Update so it will constantly check for the player every frame. Embarassingly, I admit I haven't had to do this before I don't think, unless I used FixedUpdate in a tutorial and I completely forgot about it.

As expected when I wrap any sort of other void function around my OnTriggerStay the engine complains a lot.

I think I found the main answer here.

http://answers.unity3d.com/questions/887630/ontriggerstay-not-updating-every-frame.html

Oh and they have all sorts of names for it, which is what initially threw me off when I first learned about it when experimenting with building placement, once I found out more I got all sorts of tutorials on the subject and coding got a bit easier in regards to raycasts.

Yes, that is it. Do that, however; just remember FixedUpdate is not every frame, so frame dependent should be Update. Should not make a difference being a psychics raycast. Imho should not exactly be frame dependent for a simple line of sight either....so FixedUpdate should do fine. Sorry if it seems confusing.

No problem, I get it, I get that it 'shouldn't' be as you say but from the way I've seen it working it clearly is not calculating every frame, so it's a good idea just to put it in to be safe and prevent any bugs.

Coin Marketplace

STEEM 0.18
TRX 0.13
JST 0.029
BTC 57912.75
ETH 3070.69
USDT 1.00
SBD 2.33