shoot game - 2 problems

Hi there ... I'm trying to make a shooter game but I have 2 problems

1- I'm using this code for shooting :


var BulletPerfab : Rigidbody;
var speed = 50;
var FireSpeed : float = 0.2;
private var NextFire : float = 0.0;

function Update () 
{
    if (Input.GetButton("Fire1") && Time.time > NextFire)
    {
        // create a new projectile, use the same position and rotation as the Launcher.
        var new_bullet : Rigidbody = Instantiate(BulletPerfab, transform.position, transform.rotation );

        // Vector3.X = speed , becuse the Global rotation of the player is (X = forward) , some time we put it Z that depeandes to the Global rotation
        new_bullet.velocity = transform.TransformDirection( Vector3( speed , 0 , 0 ) );

        // Ignore collisions between the missile and the character controller
        Physics.IgnoreCollision( new_bullet.collider, transform.root.collider );

        NextFire = Time.time + FireSpeed;   
    }
}


but the problem that the bullet need time to arrive to the wall , and if I increase the speed time of the bullet it's cross the wall with out make any collision, so is there a way to directly put the bullet on the wall, like OnMouseDown on the wall or Enemy the effect directly will show ?

2- how can I let the bullet when I shoot it ,go to the position(X,Y)/Direction of the shoot pointer(GUI). (The GUI it's not on the middle of the screen).

thanks....

  1. Use Continous Physics or Raycast instead.
  2. Create a ray from the mouse position or another screen point and get the direction of the ray.

Something like this...

var prefab : Rigidbody;

function Update () {
    if (Input.GetButtonDown ("Fire1")) {
        var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        var instance : Rigidbody = Instantiate (prefab, transform.position, transform.rotation);
        instance.AddForce (ray.direction * 100);
    }
}