system
1
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.
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.
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.