- Home /
turn around 90 curve with forward/rotation
I'm trying to do some kind of tower defense game.
The map is created from blocks (prefabs) and then combined.
In each corner there's a waypoint-GameObject so the Enemy knows its path.
the movement is achieved by translation (speed * Time.deltaTime).
I'd like to achieve a smooth turn around the 90-corners while keeping the speed (as variable in the enemy-Object). what's the optimal way to achieve this? Do I need to use complicated trigonometry to calculate each movement segment around the curve (if yes, are there any examples, and isn't this too cpu-expensive if there are many objects turning at the same time)?
Any ideas?
Here's a little illustration how it's supposed to be: (movement along the black line, curve between the blue lines, waypoint in magenta. the red lines are on the texture (ignore them))
Answer by skovacs1 · Oct 12, 2010 at 05:26 PM
Here are some options for how to round corners:
- Include more waypoints and fake a curve. Simple, but awkward.
- Include a directions to raycast against for corner sections. Assumes you know the structure of your walls, that you have colliders and adds raycasts, making it simple, but costly.
- Set up a bzier curve or NURBS system for your waypoints. Less simple.
- You could use iTween which has all of the facilities needed for this. Simple (Look into PutOnPath, PointOnPath, MoveTo with movetopath).
Thank you, i'll look into these solutions (i love bezier curves but calculating them myself is a little tricky, unity should really have some sort of spline-tool), i will look into those and maybe use itween (i use it for other stuff already)
Btw: I think iTween is free. You can just download it (and its on google code so it must be open source...). But i actually donated a little bit, cause i like it a lot (i'm co$$anonymous$$g from flash (R.I.P.), it works much like the tweener library in ActionScript)
Sorry about the mistake on the itween stuff. I was looking at the examples on their site which are 2.00 USD apiece.
Answer by ckfinite · Oct 12, 2010 at 05:53 PM
You should not need to use trig, unless you calculate the texture's curves at runtime.
What you could do is determine the turn rate for the object. To do this, you will need to know the speed of the object and the radius of the turn. Calculate the circumference of the circle that the curve is part of, divide it by 4, and that is the distance the object will need to travel. Then, divide this number by the speed of the object, to get the amount of time the object will take to move through the curve. Then, divide the angle of the turn by this time, resulting in the amount of degrees per unit is required. Then, add this number (divided by deltaTime) to the correct rotation every tick.
Thanks for the tip, this seems to be the easiest solution (since the turns are always the same, i can pre-calculate most of it)