Here’s something that will calculate where two lines will intersect. If they don’t intersect then the two resulting vectors will mark the points where both lines are closest together.
/* setup vectors */
Vector3 l1begin = playerCurrentPosition;
Vector3 l1end = playerTargetPosition - l1begin;
Vector3 l2begin = enemyCurrentPosition;
Vector3 l2end = enemyTargetPosition - l2begin;
/* calculate vectors */
Vector3 n = Vector3.Cross(l1end, l2end);
Vector3 u = Vector3.Cross(n, l1begin - l2begin) / Vector3.Dot(n, n);
Vector3 intersection1 = l1begin - l1end * Vector3.Dot(l2end, u);
Vector3 intersection2 = l2begin - l2end * Vector3.Dot(l1end, u);
/* draw vectors */
Debug.DrawRay(l1begin, l1end, Color.cyan);
Debug.DrawRay(l2begin, l2end, Color.magenta);
Debug.DrawLine(intersection1, intersection2, Color.white);
If you want to have an object turn automatically towards a target then you have to find out in which direction to turn. The usual angle function just returns the smallest angle between the current heading and the heading towards the target but not which way around.
Vector3 lTarget = transform.InverseTransformPoint(target.position);
targetDistance = lTarget.magnitude;
targetAngle = Mathf.Atan2(lTarget.x, lTarget.z) * Mathf.Rad2Deg;
- This calculates the position in relation to the current objects local coordinate system.
- It then calculates the distance (because you’ll need it anyway).
- Finally it calculates the angle in radians and then converts it to degrees, just for good measure.
September 13, 2011 – 10:39
To get the vector from a quaternion rotation you simply multiply the quaternion by the forward vector.
Debug.DrawRay(transform.position, transform.rotation * Vector3.forward, Color.red);
This also effectively rotates the forward-vector with the rotation of the transform, positioning it relative to the rotation of the actual object.
September 13, 2011 – 10:38
I’m getting a bit fed up having to look up everything with google when programming. Therefore I’ll add everything I need and find here (even if it isn’t all that extrodinary).
Added a whole new track with better curves and everything.
I tried to build the track from separate segments which I wanted to combine in Unity but it was quite tricky building all parts in Lightwave and then getting them to fit in Unity. Therefore I prebuilt the track and imported it as one model in Unity. This should also be better for lightmapping, surfacing, etc. but I’m still quite far away from that.
I also added lap-tracking and repositioning of the player (if you get stuck). The turbo will also not be unlimited anymore, which is now indicated by a couple of dials on the right side of the display.
SCR003
Controls are as follows:
Cursor keys: Steering, throttle and brakes
Ctrl.: Turbo
Space: Reposition vehicle
You’ll need the turbo to get over the last jump and it will also help greatly managing the first climb.
Did a quick 3d off-road car in Lightwave for this project and integrated it into the scene.
Also added a boost via the fire button (ctrl.) and a camera control script. It also should be much more difficult to tip the car over, but the car will not slide yet.
SCR002
Had a scan through another bunch of old Amiga games and stumbled across Geoff Crammonds excellent Stunt Car Racer. Knowing that Unity had some sort of vehicle physics (albeit not very good ones) I wanted to have a go at this myself.
Here’s what I have come up until now.
SCR v01
Added shields and the energy pads, that will power the shields. At the moment the shields just protect the catapult and will vanish when hit. In the next version the energy pads will replenish the shields as long as they are not blocked by the projectiles that are being dropped.
http://game.lychesis.net/CP003.html
Fixed a couple of things and added a new type of enemy. Still no way to die, but I now have a good idea of where I am going with this.
http://game.lychesis.net/CP002.html
We’ve been developing in Unity again at work and this got me started at several small games at home (just for practise and maybe a bit more).
Catapult is a variation of the old PC game called Paratrooper.
You control a turret and have to defend yourself from aircraft dropping bombs onto you all the time. It’s not very advanced as of yet but that’s mostly because I am trying and testing different techiques at the moment.