Launching projectile towards crosshair.

I’m having hard time finding out how I would launch rigidbody (a grenade) towards crosshair, that is at center of the screen. This would not be problem if I would launch rigidbody from camera, but I need it to shoot from player, that is slightly to the left of screen. Therefore I cant use script from fps tutorials, that use the angle of camera.

For example, this shoots grenade straight forward from player. How would I change the forward to be crosshairs position?

var grenadePrefab: Rigidbody;
var throwForce: float;

function Update () {

//GREANDES

if(Weapons.weaponSlot == 2) {

if(Input.GetButtonDown(“Fire1”)) {

var grenadeInstance : Rigidbody = Instantiate(grenadePrefab, transform.position, transform.rotation);

grenadeInstance.rigidbody.AddForce(transform.forward * 2000);

Physics.IgnoreCollision(grenadeInstance.collider, collider);

}
}
}

I’m doing something simliar.

1st, a script on a sphere (no collision, no rendering) that has a follow mouse script. So the sphere is always at the mouse pointer. (has depth adjust)

2nd, a gameObject (gun) that is in front of camera. It has a ‘LookAt’ script with the target set the the sphere. May be a hack but very simple scripts, the gun always shoots at the sphere which is always at the cursor.

(of course this assumes your character is always looking at mouse pointer) - my character always moves foward, so cursor wanders around screen. If yours follows the mouse direction then your cursor is probably locked mid screen anyway.

(don’t have access to scripts atm)

Alternately just attach a child sphere (or cube) to the player at a distance and it’ll always be in front of player).

I did something similar to fire a missile using raycasting (with a lot of help from this forum):

var targetpoint : Vector3;
var targetRange: int;
function Update()
{
var ray : Ray = camera.ViewportPointToRay (Vector3(0.5,0.5,0));
var rayHit : RaycastHit;
targetpoint = ray.GetPoint(targetRange);

Debug.DrawRay(ray.origin, ray.direction * targetRange, Color.yellow);
}

This casts a ray right down the center of the field of view. You might launch the grenade toward the targetpoint using a LookAt script attached to the grenade. Set the targetpoint well down field. Or, you could get the rayHit.point of the ray and aim toward that. Then, if there’s something or someone in the line of sight, the grenade will aim for it.In my case, I used a combination. If there’s nothing in the way of the raycast, the missile fires toward the targetpoint, else it fires toward the rayHit.point if there’s an object in the crosshairs…

Your not the first one to ask this.

Play it with gizmo’s turned on.

433054–15045–$mouseAimEx.zip (43.8 KB)

To keep it simple, you can also just add a slight force in the X direction, which is actually more realistic. Instead of rigidbody.AddForce(transform.forward*2000), you could use:

var correction: float;

rigidbody.AddForce (correction, 0, 2000);

A small correction force will push the grenade to the right (or left if you make it negative) and toward the target of the crosshairs.