Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Sight(FPS)

Discussion in 'Scripting' started by angeji_96, Apr 9, 2016.

  1. angeji_96

    angeji_96

    Joined:
    Oct 15, 2015
    Posts:
    8
    Hello, please tell me how you can implement auto-aim?Wanted to make through function RaycastHit[] hits = Physics.SphereCastAll (transform.position, 3.0f, transform.forward, 1000f);
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class FPSMouseLook : MonoBehaviour
    5. {
    6.  
    7.     public enum RotationAxes { MouseX, MouseY, MouseOrbit}
    8.     public RotationAxes axes = RotationAxes.MouseX;
    9.     public float dist;
    10.     [HideInInspector]
    11.     public Transform target; //This needed to be assigned in case of MouseOrbit option selected
    12.  
    13.     float currentSensitivityX = 1.5F;
    14.     float currentSensitivityY = 1.5F;
    15.  
    16.     float sensitivityX = 1.5F;
    17.     float sensitivityY = 1.5F;
    18.  
    19.     float minimumY = -60F;
    20.     float maximumY = 60F;
    21.    
    22.     float rotationX = 0F;
    23.     float rotationY = 0F;
    24.  
    25.     float minimumX = -360F;
    26.     float maximumX = 360F;
    27.    
    28.     Quaternion originalRotation;
    29.  
    30.     bool doingRecoil = false;
    31.     float recoilAmount = 0.15f;
    32.     float tempRecoil = 0;
    33.  
    34.     float distance = 5.0f;
    35.     bool reachedTarget = false;
    36.  
    37.     void Start ()
    38.     {
    39.         // Make the rigid body not change rotation
    40.         if (GetComponent<Rigidbody>())
    41.         {
    42.             GetComponent<Rigidbody>().freezeRotation = true;
    43.         }
    44.  
    45.         originalRotation = transform.localRotation;
    46.     }
    47.    
    48.     void LateUpdate ()
    49.     {
    50.         if(GameSettings.menuOpened)
    51.             return;
    52.  
    53.         if(currentSensitivityX != GameSettings.mouseSensitivity || currentSensitivityY != GameSettings.mouseSensitivity)
    54.         {
    55.             currentSensitivityX = currentSensitivityY = GameSettings.mouseSensitivity;
    56.         }
    57.  
    58.         sensitivityX = currentSensitivityX * (GameSettings.currentFOV/GameSettings.defaultFOV);
    59.         sensitivityY = currentSensitivityY * (GameSettings.currentFOV/GameSettings.defaultFOV);
    60.  
    61.  
    62.         RaycastHit[] hits = Physics.SphereCastAll (transform.position, 3.0f, transform.forward, 1000f); //!!!!!!
    63.         if (axes == RotationAxes.MouseX)
    64.         {
    65.  
    66. #if UNITY_ANDROID || UNITY_IOS || UNITY_WP8 || UNITY_WP8_1
    67.             rotationX += GameSettings.lookDirection.x * sensitivityX;
    68. #else
    69.                
    70.                 for (int k = 0; k < hits.Length; k++) {
    71.                     if (hits [k].transform.CompareTag ("ray")) {
    72.                         dist = Vector3.Distance (hits [k].transform.position, transform.position);
    73.  
    74.                     }
    75.                 }
    76.            
    77.             rotationX += Input.GetAxis("Mouse X") * sensitivityX;
    78.            
    79.  
    80. #endif
    81.  
    82.             rotationX = ClampAngle(rotationX, minimumX, maximumX);
    83.             Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
    84.             transform.localRotation = originalRotation * xQuaternion;
    85.         }
    86.  
    87.         if (axes == RotationAxes.MouseY)
    88.         {
    89.  
    90. #if UNITY_ANDROID || UNITY_IOS || UNITY_WP8 || UNITY_WP8_1
    91.             rotationY += GameSettings.lookDirection.y * sensitivityY;
    92. #else
    93.             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    94.  
    95. #endif
    96.  
    97.             rotationY = ClampAngle(rotationY, minimumY, maximumY);
    98.             Quaternion yQuaternion = Quaternion.AngleAxis (-rotationY, Vector3.right);
    99.             transform.localRotation = originalRotation * yQuaternion;
    100.         }
    101.  
    102.         if (axes == RotationAxes.MouseOrbit && reachedTarget && target)
    103.         {
    104.  
    105. #if UNITY_ANDROID || UNITY_IOS || UNITY_WP8 || UNITY_WP8_1
    106.             rotationX += GameSettings.lookDirection.x * sensitivityX;
    107.             rotationY += GameSettings.lookDirection.y * sensitivityY;
    108. #else
    109.             rotationX += Input.GetAxis("Mouse X") * sensitivityX;
    110.             rotationY -= Input.GetAxis("Mouse Y") * sensitivityY;
    111. #endif
    112.  
    113.             rotationX = ClampAngle (rotationX, minimumX, maximumX);
    114.             rotationY = ClampAngle (rotationY, minimumY, maximumY);
    115.  
    116.             Quaternion rotation = Quaternion.Euler(rotationY, rotationX, 0);
    117.             Vector3 position = rotation * new Vector3(0.0f, 0.0f, -distance) + target.position;
    118.    
    119.             transform.rotation = rotation;
    120.             transform.position =  position;
    121.         }
    122.     }
    123.    
    124.     public static float ClampAngle (float angle, float min, float max)
    125.     {
    126.         if (angle < -360F)
    127.         {
    128.             angle += 360F;
    129.         }
    130.  
    131.         if (angle > 360F)
    132.         {
    133.             angle -= 360F;
    134.         }
    135.  
    136.         return Mathf.Clamp (angle, min, max);
    137.     }
    138.  
    139.     public void Recoil()
    140.     {
    141.         //currentRecoil += recoilAmount;
    142.         tempRecoil = 0;
    143.  
    144.         if(!doingRecoil)
    145.         {
    146.             StartCoroutine(SmoothRecoil());
    147.         }
    148.     }
    149.  
    150.     IEnumerator SmoothRecoil()
    151.     {
    152.         doingRecoil = true;
    153.  
    154.         while(tempRecoil < recoilAmount - 0.05f)
    155.         {
    156.             if(tempRecoil < recoilAmount/1.5f)
    157.             {
    158.                 tempRecoil = Mathf.Lerp(tempRecoil, recoilAmount, Time.deltaTime * 15);
    159.             }
    160.             else
    161.             {
    162.                 tempRecoil = Mathf.Lerp(tempRecoil, recoilAmount, Time.deltaTime * 9);
    163.             }
    164.  
    165.             rotationY += tempRecoil;
    166.  
    167.             yield return null;
    168.         }
    169.  
    170.         //tempRecoil = 0;
    171.         //currentRecoil = 0;
    172.  
    173.         doingRecoil = false;
    174.     }
    175.  
    176.     public void AssignTarget(Transform t1)
    177.     {
    178.         if(t1)
    179.         {
    180.             rotationX = t1.eulerAngles.y;
    181.             rotationY = 25;
    182.             target = t1;
    183.             reachedTarget = false;
    184.         }
    185.         else
    186.         {
    187.             rotationX = transform.eulerAngles.y;
    188.             rotationY = 0;
    189.             target = null;
    190.             reachedTarget = true;
    191.         }
    192.  
    193.         if(target)
    194.         {
    195.             StopCoroutine("MoveToTarget");
    196.             StartCoroutine("MoveToTarget");
    197.         }
    198.     }
    199.  
    200.     IEnumerator MoveToTarget ()
    201.     {
    202.         Quaternion rotation = Quaternion.Euler(rotationY, rotationX, 0);
    203.         transform.rotation = rotation;
    204.         while(target && Vector3.Distance(rotation * new Vector3(0.0f, 0.0f, -distance) + target.position, transform.position) > 0.1f)
    205.         {
    206.             transform.position = Vector3.MoveTowards(transform.position,  rotation * new Vector3(0.0f, 0.0f, -distance) + target.position, Time.deltaTime * 79);
    207.             yield return null;
    208.         }
    209.  
    210.         reachedTarget = true;
    211.     }
    212. }
    213.