I just tried to convert the following script, http://wiki.unity3d.com/index.php/ShootingWeaponScript
from C# to Java script and I got everything working except for one part and I am not sure what I am doing wrong. Here is the code that I am working with.
var projecile : Rigidbody;
var speed = 20;
var projMuzzleVelocity : float = 6.0; // in metres per second
var RateOfFire : float = 6.0;
var Inaccuracy : float = 6.0;
var fireTimer : float = 6.0;
function Start()
{
fireTimer = Time.time + RateOfFire;
}
function Update ()
{
if( Input.GetButtonUp("Fire1"))
{
Debug.DrawLine(transform.position, transform.position + transform.forward, Color.red);
if (Time.time> fireTimer)
{
//GameObject.projectile;
var muzzleVelocity : Vector3 = transform.forward;
if (Inaccuracy != 0)
{
var rand : Vector2 = Random.insideUnitCircle;
muzzleVelocity += new Vector3(rand.x, rand.y, 0) * Inaccuracy;
}
muzzleVelocity = muzzleVelocity.normalized * projMuzzleVelocity;
//Debug.Log("Fire1");
clone = Instantiate(projecile, transform.position, transform.rotation);
//clone.velocity = transform.TransformDirection(muzzleVelocity);
clone.velocity = transform.TransformDirection(Vector3(0,0,speed));
fireTimer = Time.time + RateOfFire;
Destroy(clone.gameObject,1.5);
}
}
}
So I have tracked the problem to the following line but I am not sure how to make this work.
clone.velocity = transform.TransformDirection(Vector3(0,0,speed));
If I make the above like this
clone.velocity = transform.TransformDirection(muzzleVelocity);
everything will work but the player will only shot in one direction. So for example they will always shoot forward, even if they are standing backwards. Like I stated before I am pretty sure that it has something to do with how I am using muzzleVelocity but I am not sure that I am doing wrong.