AI scripting question (NOT problem)

Well, I’ve finally figured out a little of java script. It is a rt of script of an AI. I have two questions. But first here is the raycast script part:

function Update(){
  if(Input.GetMouseButtonDown(0)){ // if the LEFT button pressed
    // casts a ray from the mouse pointer
    var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    var hit: RaycastHit;
    if(Physics.Raycast(ray, hit)){ // if something hit, send the message
      hit.collider.SendMessageUpwards("ApplyDamage", 20, SendMessageOptions.DontRequireReceiver);

Now the questions. One is how do I make it act more like a machine gun. You know, like maybe if you press and hold it, it will take him down. Like a real machine gun also, I need range. I need it to have range, like if i’m standing kinda far (Maybe 500 yards) it will destroy him. But now if i try, i have to press the left mouse button and i have to go right up to it and click the button 5 times!


You’re question’s only been up for 3 hours so it’s not like you’ve had to wait that long, and its a sunday and I’ve noticed traffic is always slower on sundays.

The answer to the first part of your question is easy, just remove the “Down” from GetMouseButtonDown(). The second part is a little more complicated. In the second part, you need to rotate your camera based on the mouse movements. That means you might have to change your current rotation scheme some.

I’m not a big fan of writing code for people, but I actually just wrote something like this for a different project so it didn’t take that much modification to get it ready for you. Here’s everything with comments.

public var cameraTransform : Transform;
//This is the camera's transform
public var rootTransform : Transform;
//This is the character's transform.
//The reason for this, the camera can rotate up and down on its own.  We want the whole body to rotate.
//But when we turn side to side the whole character turns.
//The other solution is to make 2 rotation scripts: one for each part.

public var sensitivity : int = 90;
//How quickly do we turn in degrees per second.
public var range = 400;
//What's the range of our gun.

private var midScreen : Vector3;
//we cache the center of our screen so we know what to shoot at.

private var curXAngle : float = 0;
//This is used to clamp the rotation angle of the camera.
//we store them in our own variable because reading euler angles doesn't return constant values.

function Start () {
     cameraTransform = Camera.main.transform;
     //find the camera.  A convienence feature.
     midScreen = new Vector3 ( Screen.width / 2 , Screen.height / 2 , 0);
     //Find the center of the screen for raycasting.
     curXAngle = cameraTransform.localEulerAngles.x;
     //grab the current angle.

function Update () {
    var xTurn = Input.GetAxis("Mouse X");
    //horizontal movement of the mouse.
    var yTurn = Input.GetAxis("Mouse Y");
    //vertical movement of the mouse.
    var curAngle = Quaternion.Euler ( Mathf.Clamp(curXAngle + (-yTurn * Time.deltaTime * sensitivity) , -90 , 90) , 0 , 0);
    //Create a rotation for the camera. Here's what we're doing:
    //Add the vertical mouse movement * rotateSpeed to the current angle.
    //Clamp it between -90 and 90.  (Straight up or down) to prevent the character from looking behind themselves.
    curXAngle += (-yTurn * Time.deltaTime * sensitivity);
    //Account for the new rotation in our storage variable.
    cameraTransform.localRotation = curAngle;
    //set the camera's local rotation to the one we just calculated.  This way we don't have to worry about rotating it around the other axises.
    rootTransform.Rotate( 0 , xTurn * sensitivity * Time.deltaTime , 0 );
    //rotate the entire player by the horizontal movement of the mouse.
    if(Input.GetMouseButton(0) ) {
    	var ray = Camera.main.ScreenPointToRay(midScreen);
    	//Point a ray at the middle of the screen.
    	var hit: RaycastHit;
    	if(Physics.Raycast(ray, hit, range)){
    	//There is an optional parameter for range.
    		hit.collider.SendMessageUpwards("ApplyDamage", 20, SendMessageOptions.DontRequireReceiver);
    		Debug.Log("Hello" +;