Fire Bullet To Mouse Position Problem

Hello everyone,

I am firing bullet to mouse position. And my bullet is translating towards mouse position correctly.

Code i have written is here :

#pragma strict
var bullet : Transform; // the bullet prefab
private var spawnPt : GameObject; // holds the spawn point object

function Start()
{

}

function Update()
{
	 if(Input.GetMouseButtonDown(0))
	 { 
	 	
	 	// only do anything when the button is pressed:
     	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
     	var hit : RaycastHit;
     	if (Physics.Raycast (ray, hit, 100))
     	{
        	if (!spawnPt) spawnPt = GameObject.Find("RightGunTip");
        	var projectile = Instantiate(bullet, spawnPt.transform.position, spawnPt.transform.rotation); 
        	// turn the projectile to hit.point
        	projectile.transform.LookAt(hit.point); 
        	Debug.Log("rotation angel :" + projectile.transform.rotation);
        	// accelerate it
        	projectile.rigidbody.AddForce(projectile.transform.forward * 1000);
     	}
     }
}

Now problem is that my bullet object is Capsule game object. And i have given rotation -90 on z axis.

But when i am shooting the bullet , at that time because of the projectile.transform.LookAt(hit.point); , bullet gets rotation on y axis which i don’t want. so what should i write for perfect rotation of bullet?

Thanks in advance for your support and help…

2 Answers

2

like the image, make a Empty game object and child the capsule bullet,
then add a sphere collider and rigidbody to that empty game object remove those from capsule, finally add this script to that empty game object,

#pragma strict
var bullet : Transform; // the bullet prefab
private var spawnPt : GameObject; // holds the spawn point object
 
function Start()
{
 
}
 
function Update()
{
     if(Input.GetMouseButtonDown(0))
     { 
 
       // only do anything when the button is pressed:
        var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        var hit : RaycastHit;
        if (Physics.Raycast (ray, hit, 100))
        {
            if (!spawnPt) spawnPt = GameObject.Find("RightGunTip");
            var projectile = Instantiate(bullet, transform.position,   Quaternion.identity);         

			projectile.transform.rotation = Quaternion.LookRotation(ray.direction);
            projectile.rigidbody.AddForce(ray.direction * 1000);
        }
     }
}

or other simple way is make a bullet from maya like software, and use it with that script

Thanks sir. Its working now..

The forward vector of your object is going to lookat the location of the vector or transform that you pass the function (make sure that you know what side the forward vector of your object is on ). It also rotates the Transform’s up vector to the world up location, which is y up, top of your screen by default unless you change it. Example: lookat( lookAtMe, thisIsMyUpDirection ). Vector3.up is default for the up vector, which is the second and optional parameter, but you can make it Vector3.down y axis (left,right x axis,forward,back z axis).

Hopefully that helps. Let me know if you still have troubles

sir , what r u trying to say ? Sorry could not understand.