- Home /
Fire projectie to crosshair in center of screen
I am making a FPS. I have a rocket launcher firing a projectile. It currently fires from the muzzle in the direction the launcher is pointing to. This makes it hard to aim at anything. I would like for the projectile to go to the crosshair in the middle of the screen instead. I think i could use raycast but im not sure how to make that work with what i have currently. Is there an easy and quick solution without having to change to much? I am using C# but i think i could work with java-script.
public GameObject launchPosition;
public Rigidbody projectilePrefab;
float projectileSpeed= 30.0f;
Rigidbody instantiatedProjectile = Instantiate (projectilePrefab, launchPosition.transform.position, launchPosition.transform.rotation)as Rigidbody;
fireLauncherAnim.animation.Play("firefromhip");
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0.0f,0.0f, projectileSpeed));
Physics.IgnoreCollision(instantiatedProjectile.collider, GameObject.FindWithTag("Player").transform.root.collider);
I would like to make an off-topic suggestion which may or may not be of interest to you. I see that you are using Instantiate
to create projectiles which can lead to performance problems. You should consider pooling projectiles for improved performance.
There are several good items from the asset store for this. Pool$$anonymous$$anager by Path-o-logical Games is a fantastic solution which is surprisingly easy to setup!
Answer by robertbu · Feb 17, 2013 at 05:32 AM
I'm assuming that you have a character controller or some other sort of rotation script on the camera. And there is a crosshair in the middle that you want to use for targeting. If this is the setup, you can just create an empty game object just in front of the camera and attach the script above to that game object and you will shooting projectiles from the center of the screen. Depending on your game, there is a potential problem. If you projectiles are under the influence of gravity the projectiles will fall some distance between the time they are created and the time they impact, so it will typically impact below your target.
One solution is to turn off gravity for your projectile. There are other more complicated solutions.
I have a character controller where all the weapons are attached to. The cross hair is attached to the camera.. What do you mean by this? "you can just create an empty game object just in front of the camera and attach the script above to that game object? You mean ins$$anonymous$$d of the spawn point / weapon script attached to the weapon attach both to the camera ins$$anonymous$$d? Doesn't seem likely to move the weapon class.
I'm confused. You have a character controller that can point weapons in various directions, but you want the bullet to go where the camera is looking no matter where the weapon is pointed? You said this is a FPS game, so how is the camera and the weapon controller attached? I have a few suggestions, but I need to understand what you are asking first.
You are correct on everything you said. . I have a RPG with arms attached to a controller. I can point in any direction i want. I have a main camera parented to the player(renamed controller). I think this is normal. I also have another camera called weapons camera parented to the player. The RPG is parented to that. I cannot move things around at this point. i have a crosshair script attached to the main camera. I just want the rpg to shoot from the gun to the cross hair. It shoots directly straight from the gun. Which would be normal in real life. i am open to making a new cross hair that points where the RPG is and not center screened.
After you've Instantiated your instantiatedProjectile, I'd do something like (untested):
var v3LookPoint : Vector3;
var ray : Ray = camera.main.ViewportPointToRay(Vector3(0.5, 0.5, 0));
var hit : RaycastHit;
if (Physics.Raycast(ray, hit)) {
v3LookPoint = hit.point;
}
else
v3LookPoint = ray.GetPoint(distance);
instantiatedProjectile.gameObject.transform.LookAt(v3LookPoint);
instantiatedProjectile.AddRelativeForce(transform.forward * 2000);
The 'distance' variable is an average distance to targets. It just assures there is a reasonable look point if the raycast does not hit anything.
Answer by RyanZimmerman87 · Feb 17, 2013 at 11:58 PM
I am still not quite sure what your issue is, you should be able to just automatically instantiate your rockets to shoot from your character as long as the gameObject your rocket script is attached to can in fact rotate?
It does not look like you are using Quarternion.identity in your instantiate? Try something like this might help:
instantiateBullet = (Rigidbody)Instantiate(bullet, transform.position, Quaternion.identity);
I also use something like this for the projectile because it allows physics to work:
instantiateBullet.AddForce(transform.forward *bulletVelocity);
If you have both those set up it should work automatically as long as the gameObject it is attached to can in fact rotate. Quaternion.Identity will ensure that the projectile will come from the position you originally set it to, in relation to your new rotation.