- Home /
Question by
OctoSloths · Nov 04, 2014 at 11:29 PM ·
javascriptshootaim
Spawn point aim at mouse to shoot
Hi I have a script to shoot projectiles:
var Bullet : Rigidbody;
var SpawnPoint : Transform;
function Fire () {
// Create a new bullet pointing in the same direction as the gun
var Shoot = Instantiate(Bullet,SpawnPoint.position,SpawnPoint.rotation);
Shoot.AddForce(SpawnPoint.forward*5000);
}
function Update ()
{
if (Input.GetMouseButtonDown(0)){
var hit: RaycastHit;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Fire();
if (Physics.Raycast(ray, hit)){
if (hit.collider.CompareTag("beep")){
hit.collider.gameObject.SendMessage("Damage");
}
}
}
}
Is it possible to make the spawn point rotate so that I can aim using the mouse? What I want is to have the bullets be aimed at my mouse so I can use it to aim. Thanks.
Comment
Use the ray you create in line 13 to assign the rotation of your spawn point (or the projectile itself).
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Quaternion rotation = Quaternion.LookRotation(ray.direction);
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
I'm having problems with the FPS Tutorial MachineGun script 2 Answers
Bullet shots direction player is facing, cant shoot in diagnal or up 2 Answers
how do I shoot where my cross hair points? 0 Answers
Aim and than shoot 2 Answers