x


Player cannot shoot

Hey, I'm trying to make my FPS character shoot a bullet but it is not working, their are no errors or anything here is my moving and shooting script

/

/Moving around
var speed = 5.0;
var rotateSpeed = 3.0;
var HealthControl;


var isWalking: boolean;

//shooting
var bullitPrefab:Transform;
var parBullitHit:Transform;
var bullit;

//dying
private var dead = false;

//Getting hit
var tumbleSpeed = 800;
var decreaseTime = 0.01;
var decayTime = 0.01;
static var gotHit = false;
private var backup = [tumbleSpeed, decreaseTime, decayTime];


//function OnControllerColliderHit(hit : ControllerColliderHit)
function OnTriggerEnter( hit : Collider )
{
    if(hit.gameObject.tag == "fireBall")
    {
        dead = true;
        //substract life here
        HealthControl.LIVES -= 1;
        Destroy(hit.gameObject);
    }

    if(hit.gameObject.tag == "enemyProjectile")
    {
        gotHit = true;
    }   

}

function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);

if(Input.GetButton("Mouse"))
{

var bullitPrefab = Instantiate(bullitPrefab, transform.Find("Spawnpoint").transform.position, Quaternion.identity); 

bullitPrefab.rigidbody.AddForce(transform.forward * 5000);
}

    if(Input.GetButtonDown("Jump"))
{
var parBullitHit = Instantiate(parBullitHit, transform.Find("spawnpoint").transform.position, Quaternion.identity);

parBullitHit.rigidbody.AddForce(transform.forward * 1500);

}
}



function LateUpdate()
{
    if(dead)
    {

        transform.position = Vector3(0, 1.753925, -2.850807);
        gameObject.Find("Main Camera"). transform.position = Vector3(0.2640705, 45.65512, -2.402526);
        dead = false;
    }

the "mouse" part is what I named the left click button.

more ▼

asked Apr 26 '10 at 11:01 PM

DTJ Productions gravatar image

DTJ Productions
57 19 20 25

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Just use this:

// Put this at the top of your script
var projectile : Rigidbody;
var speed = 10;

function Update () {
    // Put this in your update function
    if (Input.GetButtonDown("Fire1")) {

    // Instantiate the projectile at the position and rotation of this transform
    var clone : Rigidbody;
    clone = Instantiate(projectile, transform.position, transform.rotation);

    // Give the cloned object an initial velocity along the current
    // object's Z axis
    clone.velocity = transform.TransformDirection (Vector3.forward * speed);
    }
}

You're instantiating a projectile at wherever 'spawnpoint' is, which, I'm assuming, is where you spawn? If that's the case, that's why your script isn't working.

more ▼

answered Apr 26 '10 at 11:18 PM

e.bonneville gravatar image

e.bonneville
5.7k 100 116 165

Ah thanks but the prob is as you see in my script I already have a speed var, and thats for my characters speed. How do I make two? One for running and one for shooting.

Apr 27 '10 at 03:19 AM DTJ Productions

Oh and it is weird the bullets are not shooting in curtain areas, and also the more I move left the more the bullets spawn from the left.

Apr 27 '10 at 03:21 AM DTJ Productions

Well, to make two speed vars, name your bullet speed bulSpeed or something of that sort. Unity figures out the physX for you, don't worry about adding the player's running speed to the bullet speed or anything like that.

Apr 27 '10 at 11:50 AM e.bonneville

Also, put that script on a separate file and attach it either to your camera or a point you want your bullets to spawn from, like in front of your gun or wherever your gun should be.

Apr 27 '10 at 11:51 AM e.bonneville
(comments are locked)
10|3000 characters needed characters left

You can use Raycasts and a delay based on the ray distance to make bullets. It is more appropriate because bullets generally have a ridiculous speed and a small size and physics don't update in a perfect manner. they have "holes" in the path.

more ▼

answered Apr 04 '11 at 09:28 PM

Kroltan gravatar image

Kroltan
74 8 10 14

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3570
x1198
x285

asked: Apr 26 '10 at 11:01 PM

Seen: 1141 times

Last Updated: Apr 26 '10 at 11:01 PM