- Home /
Setting bullet instansiate direction? help?
Hello, I'm making a medieval first person shooter, similar to the usual FPS but a magic wand instead for a gun, fireballs instead of bullets.
The fireballs are not as fast as bullets, so creating a prefab that travels with a medium speed and makes damage on collision is the best idea I can think of.
The problem is, i can't make the fireballs come of the wand to go to the center of the screen where the GUI for the cross that shows where the fireball will hit(i forget its name). This is the script, I attached the script to an invisible cube, so the fireballs get created at the top of the want and go forward, but the problem is they go to a different direction everytime
var fireBall : Rigidbody;
function Update () {
if (Input.GetButtonDown("Fire1"))
{
Instantiate(fireBall, transform.position, transform.rotation);
fireBall.AddForce(Vector3.forward * 10);
}
}
![alt text][1]
PLEASE HELP ME FIND A CORRECT WAY TO DO IT
Thank you [1]: /storage/temp/13333-screenshot_6.jpg
Answer by robertbu · Jul 20, 2013 at 09:38 PM
To make this work, you have to specify a distance from the camera. It can either be a set distance, or you can use Raycast() to determine the distance. In addition, you need to do the AddForce() to the object created, not to the prefab. Here is a bit of untested code:
#pragma strict
var fireBall : GameObject;
var distance : float;
function Update () {
if (Input.GetButtonDown("Fire1"))
{
var hit : RaycastHit;
var point : Vector3;
if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, hit)) {
point = hit.point;
}
else {
point = Camera.main.transform.position + Camera.main.transform.forward * distance;
}
var go = Instantiate(fireBall, transform.position, transform.rotation) as GameObject;
go.transform.LookAt(point);
go.rigidbody.AddForce(go.transform.forward * 1000.0);
}
}
'distance' is the default distance in front of the camera to use as a point if the Raycast() fails.
returns a NullReferenceExeption , still trying to figure out the solution :(
I made a couple of changes. Note you will need to reinitialize (drag and drop again) the fireBall variable since I changed its type.
Your answer
Follow this Question
Related Questions
Make a Gun fire multiple times 3 Answers
How can I shooting script c#? 0 Answers
Shooting Script Instantiate Problems. Help? 0 Answers
Audio when I shoot 3 Answers
My gun won't fire. ( Javascript ) 1 Answer