Okay so I’m trying to make a 3rd person sort of Arena game, and finally got the camera and character to move right, but now I’m having problem shooting a fireball from my character to the crosshair. I need it to be a projectile and not pure raycast, but I’m out of ideas on how to go about it.
The way I’m doing it now is to have a raycast from the camera straight forward, and then instantiate the fireball at a transform position in from of the character. The thing is the cross hair won’t align up with the fireball no matter what I do. The crosshair is just a simple sprite on a canvas in anchored to the center of the screen and placed in the center of the screen.
What happens is I fire the fireball like so:
As you can see in the editor its about to hit the right position on the ray but in game mode it looks like this:
This is the code attached to the player:
Ray fireRay = new Ray(Camera.main.transform.position,Camera.main.transform.forward);
Debug.DrawRay (fireRay.origin, fireRay.direction * shootDistance, Color.green);
if (Physics.Raycast(fireRay, out fireHit, shootDistance, fireMask))
GameObject go = (GameObject)Instantiate(bullet,castPoint.position,Quaternion.identity);
I’ve tried moving the crosshair around but it’s never accurate all of the time, I’ve also tried changing the way I do the raycast to:
Ray fireRay = Camera.main.ViewportPointToRay (0.5f, 0.5f, 0);
but that just seems to do the same. I found this forum post that seems to describe my problem, but offers no solution:
If anybody could point me in the right direction I would be extremely grateful, I’ve been googling this for days now.
The raycast is great it goes from the main camera directly to the center of the screen, if i made a line render from the camera to where im point it would be accurate, likewise if I spawned the fireball in the same position as the camera it would go where I’m aiming, the problem comes in when I try to spawn the fireball in front of the player.
The crosshair is directly in the middle of the camera, so I don’t understand why it doesn’t match in game.