- Home /
how to make the cannon fire yourself
I've got this code for it to shoot:
using UnityEngine;
using System.Collections;
public class controleBalaC : MonoBehaviour {
public Transform ballCannon;
public float forceShot;
private Transform pointShot;
// Use this for initialization
void Start () {
pointShot = transform.Find("Shooter");
}
// Update is called once per frame
void Update () {
Transform instancia = (Transform) Instantiate(ballCannon, pointShot.position, pointShot.rotation);
instancia.rigidbody.AddForce(pointShot.TransformDirection(Vector3.forward) * forceShot);
}
}
But I want him to shoot yourself every 5 seconds. You can do this?
Comment
Best Answer
Answer by whydoidoit · Jun 14, 2012 at 07:00 PM
using UnityEngine;
using System.Collections;
public class controleBalaC : MonoBehaviour {
public Transform ballCannon;
public float forceShot;
private Transform pointShot;
// Use this for initialization
void Start () {
pointShot = transform.Find("Shooter");
}
float lastTime = 0;
// Update is called once per frame
void Update () {
if(Time.time - lastTime >= 5)
{
Transform instancia = (Transform) Instantiate(ballCannon, pointShot.position, pointShot.rotation);
instancia.rigidbody.AddForce(pointShot.TransformDirection(Vector3.forward) * forceShot);
lastTime = Time.time;
}
}
}
Your answer
Follow this Question
Related Questions
Calculate parabolic shot... 1 Answer
Reload after certain amount of shots? 1 Answer