I have a problem with adding velocity via JavaScript. This is that an object which is instantiated with the correct rotation and position is only given velocity as if the character were facing straight(level with ground).
I am doing this for a class in video game developement, and this assignment is already late, so it is ‘imperative’ for me to get it finished soon.
My Coding:
function Shoot(){
if(CanBeShot==true&&Reloading==false){
//Insantiate a bullet at the tip of the gun
var firedBullet:Rigidbody=Instantiate(Bullet,gunPos.position,gunPos.rotation);
//Gun position(gunPos) is set to the tip of the gun, which has no collider
firedBullet.velocity=transform.TransformDirection(Vector3(0,0,BulletSpeed));
//Instantiate a flash effect
Instantiate(MuzzleFlash,gunPos.position,gunPos.rotation);
Physics.IgnoreCollision(firedBullet.collider,transform.root.collider);
//Subtract from remaining ammo
AmmoRemaining--;
//Irrelevant to the problem:
CanBeShot=false;
yield WaitForSeconds(0.5);
CockingSound.audio.Play();
yield WaitForSeconds(0.5);
CanBeShot=true;
}
}
Code in tutorial:
var instantiatedProjectile:Rigidbody=Instantiate(projectile,transform.position,Vector3(0,-1,0),transform.rotation);
instantiatedProjectile.velocity = transform.TransformDirection(Vector3(0,0,speed));
Physics.IgnoreCollision(instantiatedProjectile.collider,transform.root.collider);