- Home /
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.
Answer by e-bonneville · Apr 26, 2010 at 11:18 PM
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.
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.
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.
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.
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.
Answer by Kroltan · Apr 04, 2011 at 09:28 PM
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.