|
Hello Unity Fans, I've been using a simple AI waypoint system for sometime, and it has only just occurred to me that it causes my enemies to follow the same height as my waypoints too. In other words, if I position my waypoints 50 on the Y axis, then the enemies will be walking 50 above the floor. What would I do to my script to ignore the Y axis of the waypoints? Also, if you would be kind enough to make my enemies turn 'smoother' from one waypoint to another? Right now, the enemy walk to one waypoint, and then 'snap' to the other (Linier) - any way of making this smooth?
Thanks folks!
(comments are locked)
|
Since your solution overrides the previous velocity of the rigid body, you might want to keep another variable updating the rate of fall, for gravity. The method I showed won't look pretty, but it won't slow down near the checkpoints due to the moveDirection is wrong. I hate to hand it out to you, but I think you have a problem with your solution all together. Why did you use rigid bodies? They are hard to get right and is probably not what you need for most problems anyhow. Why don't you at least add forces instead of overwriting the velocity, so gravity and other forces are accounted for? Why not use the character controllers that are out there? Anyhow, try the change with the last line of code to see if you get your desired effect. I tried this and it works, BUT, if my terrain goes negative (walking down a hill), then it doesn't work because its following the 0. I've tried making it a negative number, such as -20, but then the enemy walks slower due to the 'pulling mass'.
Dec 15 '10 at 04:37 PM
oliver-jones
It sounds like your character just have to apply gravity?
Dec 15 '10 at 04:47 PM
Statement ♦♦
I have gravity applied - what do you suggest for a new approach then? All my enemy do is to follow one waypoint after another in sequence.
Dec 15 '10 at 08:02 PM
oliver-jones
How about you give proper pathfinding a shot? http://www.arongranberg.com/unity/a-pathfinding/
Dec 16 '10 at 03:08 PM
Statement ♦♦
I know I'm far to late, but I felt like I just had to say this: Use
Apr 01 '11 at 12:57 PM
e.bonneville
(comments are locked)
|
|
For smooth rotation without physics, you can use this:
The function should be called in Update. It returns The magic number of 1000f you see there is one that works for me, but I don't really understand what it does :( The docs on Vector3.RotateTowards is not very clear. The following is a JavaScript version (I am not a JavaScript native speaker, if anyone sees anything amiss, please let me know). It is not static, so you have to make put it in the same file as the thing you wish to rotate. Looks good - I'm trying to convert it into JavaScript, but I keep getting errors. Could you give me a hand? Cheers :)
Dec 16 '10 at 02:48 PM
oliver-jones
@oliver-jones I updated my answer.
Dec 16 '10 at 03:15 PM
Herman Tulleken
(comments are locked)
|
|
I post another answer because this isn't related to my previous one. How about you give proper pathfinding a shot? Then I think you have to worry less about all the different special cases such as slopes and colliders. I had a look at this - but its not really for my game - mines more of a tower defence type - so the enemy follow a specific path.
Dec 16 '10 at 09:38 PM
oliver-jones
Then simply do this. Ignore the rigidbody all together. Use the velocity to find the next position. (nextPosition = position + velocity * Time.deltaTime). Then sample the Y coordinate of your terrain and replace nextPosition.Y with that sample. Finally position your character at nextPosition. Done.
Dec 16 '10 at 11:40 PM
Statement ♦♦
I like the idea of this - Could you please collaborate some more? Maybe give an example of code in your post? Thanks
Dec 17 '10 at 02:56 AM
oliver-jones
(comments are locked)
|
|
To make turning smoother, try Here is some example code for looking at and then moving towards a waypoint called
The way this works is that the gameObject this script is attached to looks at the gameObject I keep getting errors like: 'position is not a member of Unity transform[]'
Dec 16 '10 at 02:53 PM
oliver-jones
(comments are locked)
|
|
You might also want to try using a dynamically-created Empty GameObject, instanced in the AI object's Awake(). Set the x&z position of this to where you want to go (say the waypoint's x&z), then you can set y to whatever you like (terrain height, etc). You can then move the AI Target to wherever you want when you want the target to change, without needing to touch its seeking code once you're happy with it. Also, using a Transform means you can treat your AI target like one with regard to handy functions like Translate(). It is however less optimised and streamlined than simply storing a target x and z number. For smooth turning, AddRelativeTorque() does indeed work well, although Mathf.MoveTowardsAngle() and Mathf.SmoothDampAngle() can be useful too for this purpose, particularly if you aren't using rigidbodies.
(comments are locked)
|
