Hey guys,
I am working on a puzzle game that involves a cannon. I have tried all known AddForce, AddExplosionForce, etc, modes that I know of. Yet none of them works. Can someone please tell me what I am doing wrong?
Here is the code:
#pragma strict
var rotateSpeed: int = 10;
var cannon: Transform; // A reference to the cannon in the scene
var projectile: GameObject; // A reference to the projectile prefab
var projectileSpeed: float; // How fast the bullet should shoot
var heightOffGround: float = 1.996635f; // How far from the ground should the projectile spawn
var projectileSpawnPoint: Transform; //Transform variable referencing the spawn point in-game
var projectileSpawnPoint2: Vector3; //Vector3 variable that is updated constantly so that the Instantiate can use it
var lastShotTime: int;
var reloadTime: int = 1;
var power: float = 2f;
var explosionPos: Transform; //Same thing here as the projectileSpawnPoint
var explosionPos2: Vector3;
var radius: float = 2;
function Start () {
}
function Update () {
projectileSpawnPoint2 = new Vector3(projectileSpawnPoint.position.x, projectileSpawnPoint.position.y, projectileSpawnPoint.position.z);
explosionPos2 = new Vector3(explosionPos.position.x, explosionPos.position.y, explosionPos.position.z);
if(Input.GetAxis("Horizontal")) {
if(Input.GetKey("right")) {
this.transform.Rotate(Vector3.forward, (Time.deltaTime * rotateSpeed) * -1);
}
if(Input.GetKey("left")) {
this.transform.Rotate(Vector3.forward, Time.deltaTime * rotateSpeed);
}
}
if(Input.GetKey("space")) {
if(lastShotTime + reloadTime < Time.fixedTime) {
var spawnPoint: Vector3 = new Vector3(cannon.position.x - 1.5f, heightOffGround, cannon.position.z);
var clone: GameObject = Instantiate(projectile, projectileSpawnPoint2, this.transform.rotation);
clone.rigidbody.AddForce(clone.transform.forward * projectileSpeed, ForceMode.Impulse);
lastShotTime = Time.fixedTime;
}
}
}
And here it is with AddExplosionForce() {
#pragma strict
var rotateSpeed: int = 10;
var cannon: Transform; // A reference to the cannon in the scene
var projectile: GameObject; // A reference to the projectile prefab
var projectileSpeed: float; // How fast the bullet should shoot
var heightOffGround: float = 1.996635f; // How far from the ground should the projectile spawn
var projectileSpawnPoint: Transform; //Transform variable referencing the spawn point in-game
var projectileSpawnPoint2: Vector3; //Vector3 variable that is updated constantly so that the Instantiate can use it
var lastShotTime: int;
var reloadTime: int = 1;
var power: float = 2f;
var explosionPos: Transform; //Same thing here as the projectileSpawnPoint
var explosionPos2: Vector3;
var radius: float = 2;
function Start () {
}
function Update () {
projectileSpawnPoint2 = new Vector3(projectileSpawnPoint.position.x, projectileSpawnPoint.position.y, projectileSpawnPoint.position.z);
explosionPos2 = new Vector3(explosionPos.position.x, explosionPos.position.y, explosionPos.position.z);
if(Input.GetAxis("Horizontal")) {
if(Input.GetKey("right")) {
this.transform.Rotate(Vector3.forward, (Time.deltaTime * rotateSpeed) * -1);
}
if(Input.GetKey("left")) {
this.transform.Rotate(Vector3.forward, Time.deltaTime * rotateSpeed);
}
}
if(Input.GetKey("space")) {
if(lastShotTime + reloadTime < Time.fixedTime) {
var spawnPoint: Vector3 = new Vector3(cannon.position.x - 1.5f, heightOffGround, cannon.position.z);
var clone: GameObject = Instantiate(projectile, projectileSpawnPoint2, this.transform.rotation);
clone.rigidbody.AddExplosionForce(power, explosionPos2, radius, 3.0);
lastShotTime = Time.fixedTime;
}
}
}
I can provide screenshots if needed.
I already have another thread that could provide more info on the question:
http://forum.unity3d.com/threads/164497-Instantiation?p=1124665&posted=1#post1124665
Thanks guys. You’ve already been a great help.
-Silent