Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

[Solved] Aim Assist code

Discussion in 'Editor & General Support' started by Fourthings, May 23, 2010.

  1. Fourthings

    Fourthings

    Joined:
    Feb 15, 2010
    Posts:
    313
    I was tryna figure out how to lower the mouselook sensitivity as the crosshair passed over an enemy at a set distance, as an aim assit, so here it is.

    Code (csharp):
    1. public var sensitivityX : float = 2;        //Mouse look sensitivity
    2. public var sensitivityY : float = 2;
    3. public var defaultSensX : float = 2;        //Default mouse sensitivity
    4. public var defaultSensY : float = 2;
    5. public var aimSensitivityX : float = 0.3;   //Aim-assist sensitivity
    6. public var aimSensitivityY : float = 0.3;
    7.  
    8. function Update()
    9. {
    10.     //If Ray collides with object tagged Player within 15 and 100 units from this object
    11.     //then lower aim sensitivity, else, don't
    12.     var hit : RaycastHit;
    13.     var fwd = transform.TransformDirection(Vector3.forward);
    14.    
    15.     if (Physics.Raycast(this.transform.position, fwd, hit, aimFarthestPoint))
    16.     {
    17.         if ((hit.collider.gameObject.tag =="Player")  (hit.distance >= aimNearestPoint))
    18.         {
    19.             sensitivityX = aimSensitivityX;
    20.             sensitivityY = aimSensitivityY;
    21.         }
    22.         else
    23.         {
    24.             sensitivityX = defaultSensX;
    25.             sensitivityY = defaultSensY;
    26.         }
    27.     }  
    28.     else
    29.     { //If Ray hits nothing return to default
    30.         sensitivityX = defaultSensX;
    31.         sensitivityY = defaultSensY;
    32.     }  
    33. }  
    34.