- Home /
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..
Honestly, using a capsule just sounds weird. Why not use a sphere, or some other custom geometry and get rid of the unnecessary rotation?
If you're going to use the capsule though, all you have to do is change the direction that you're adding to it from forward to up/down.
Also, check it:
Ray ray = camera.ScreenPointToRay(Input.mousePosition);
GameObject bullet = GameObject.Instantiate(bulletPrefab, spawnPoint.position, Quaternion.LookRotation(ray.direction)) as GameObject;
bullet.rigidbody.AddForce(bullet.transform.forward * bulletSpeed, Force$$anonymous$$ode.VelocityChange);
Either raycast and use GetComponent to do your damage, or don't Raycast. You don't need the hit point, because you're firing a bullet through space that is going to hit whatever it hits, and when it does (theoretically) do damage. :)
Answer by Moor · Nov 23, 2013 at 09:27 AM
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
Answer by highpockets · Nov 23, 2013 at 07:26 AM
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.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
I can't shoot up or down. HELP! 1 Answer
Shoot Bullet At Touch Position : 2D 1 Answer
RayCast Problem : 2d 1 Answer
Rotation Problem 1 Answer