- Home /
Doing a Crosshair for a Space Fight Simulator
Yes i know there are plenty of questions answered for this kind of problem, but most of em do not seem to answer my question for i am still trying to get it running.
I am doing a space flight simulator where players connect and fight against each other, quite simple, i got nearly everything up and running except the crosshair and the velocity/bullet speed indicator to attack my target.
The crosshair should be shown always in front of my Ship for my weapons are pointing straight forward the camera should point to where my bullets are going. The velocity indicator should be shown at the position of the targets velocity Vector3 + the time my bullets need to be translated about the distance by speed.
I will not paste my code for it is totally not working and would be only the last try of many.
I tried DrawTexture, i tried a Label with the texture as value, i tried a GUITexture, i tried to get it running with camera.WorldToScreenPoint(Target.rigidbody.velocity bulletSpeed) -> GUIUtility.ScreenToGUIPoint, with camera.WorldToViewPortPoint by calculating a relative position of Target.position - transform.position, i used transform.TransformDirection(Vector3.forward distance) aso.
The last try was to addept a Script aldonaletto posted 2011 and its near the goal i am trying to reach but when i turn my ship to the left or the right the Crosshair moves ~100px to the left or right and returns to the correct position after stopping the movement, i do not get it. The following is the adepted code from aldonaletto.
// targ is the target transform, vel is its velocity vector,
// and speed is the projectile speed. Returns the time needed
// to reach the target, or -1 if it's not possible
float HitTime(Transform targ, Vector3 vel, float speed) {
Vector3 dist = targ.position - transform.position;
float a = Vector3.Dot(vel, vel) - speed * speed;
float b = 2 * Vector3.Dot(vel, dist);
float c = Vector3.Dot(dist, dist);
float det = b * b - 4 * a * c;
if (det >= 0)
return 2 * c / (Mathf.Sqrt(det) - b);
else
return -1;
}
// that's an example code to test this function:
Vector3 targetVel;
Vector3 lastPos;
void DoHitMarker () {
if (Target != null){ // if some target locked...
// if you don't know the target velocity, calculate it
Vector3 vel = Target.rigidbody.velocity;
// Vector3 vel = (Target.position - lastPos)/Time.deltaTime;
// lastPos = Target.position;
// filter the velocity to avoid a shaking crosshair
targetVel = Vector3.Lerp(targetVel, vel, Time.deltaTime);
float _bulletSpeed = RapidGunAmmoPrefab.GetComponent<Bullet>().Speed;
// calculate the hit position:
float t = HitTime(Target, targetVel, _bulletSpeed);
if (t < 0){
// target moving away too fast to be reached
GUIHit.enabled = false;
}
else {
// target may be hit:
GUIHit.enabled = true;
// calculate the position where the target will be hit
Vector3 hitPos = Target.position + t * targetVel;
// calculate the crosshair position
Vector3 crossPos = _cam.WorldToViewportPoint(hitPos);
GUIHit.transform.position = crossPos;
}
}
}
void DoCrossHair () {
_relativCrossHairPos = _cam.WorldToViewportPoint(transform.TransformDirection(Vector3.forward * 20));
GUICrossHair.transform.position = _relativCrossHairPos;
}
If anyone got an idea what i am doing wrong or a complete other suggestion please enlighten my mind for i am trying to get this running for at least 2 month now while everything else is running properly. I am using a rigidbody for my ship and AddRelativeForce(Vector3.back boost) to move my ship and simulate a bit of real physics, the bullets are moved by transform.Translate(Vector3.forward bulletSpeed) so the speed is calculatable.
Greetz Mahtrok
Ok forget about it, i found my mistake by childing the GUITexture to my Ships transform. Now everything works fine.
Your answer
Follow this Question
Related Questions
Crosshair Y-axis inverted 1 Answer
Question about a space shooter crosshair targeting script 1 Answer
Spaceship crosshair 1 Answer
Worldtoscreenpoint..... 2 Answers
screen coord 1 Answer