- Home /
Crosshair possible with mobile flying game?
I've been looking around quite a bit, thinking and trying things and I want to have it to follow the yaw of the ship, or the direction it's facing but it seems impossible right now. I am using nGui, however if I have to use unity gui I suppose that is okay because it is something simple. I've tried attaching items as children to the ship but nothing seems to be working. I'm just not sure where to start, if you have any information thank you.
I have only a vague idea of your problem from your question. You need to add a lot more information...maybe an images as well. Is the plane first or third person? How do you want the crosshair to track the plane? Why did making the crosshair a child not work? Do you want to the crosshair in world space or some other space? What is the relationship between the crosshair and any projectiles fired. And so on.
Sorry I wasn't clear as I should have been. Basically what I have right now is a plane, or space ship that's fully moving and working with shooting missiles from the front of it. So if I am moving left the missiles will shoot out left. All I need is a crosshair that follows the direction of the ship, so if it is turning left a little, the cross hair will be moved left a little so it's more accurate if they were to shoot a missile. Does this help at all?
Answer by robertbu · Apr 18, 2013 at 04:13 AM
It is still fairly fuzzy, but I'm going to make some guesses. Let's assume you have a 3-person plane with a camera following the plane. Step one is figure out a hit distance in 3D space. This could be a a fixed distance of travel for the missiles or it could be a position of a target. Let assume the first. Construct a ray from the missile in the direction of travel. Then calculate the world position of the hit. Finally transform the world position into a position to display the crosshair. In the code below, I transform it into screen coordinates for use with GUI. I don't know NGUI.
var ray = new Ray(missile.transform.position, missile.transform.forward);
var world_pos = ray.GetPoint(hit_distance);
var screen_pos = Camera.main.WorldToScreenPoint(world_pos);
Frogot to mention that if you are using gravity, the missile will drop a bit between firing and when it reaches the 'hit_distance.' With a fixed distance, you can just subtract some from world_pos before making the final conversion to compensate.
Oh goodness that was more than I expected it to be, does this have a big hit on performance? I should note this would be for a mobile game as I said in the title, also everything is working in the game, I just wanted the crosshair to help someone while shooting.
The missile fires and does everything just fine, it's just difficult to aim. It's rather simple so I'm surprised about the ray casting etc, but you know more than I do, clearly.
This is not a raycast. It is just using a ray to fine a point in 3D space. You should not see a performance hit.
Your answer
Follow this Question
Related Questions
iOS and Android Publishing 2 Answers
Shadows problem with mobile. 1 Answer
Is this surface shader too heavy for mobile? 0 Answers