Search Unity

Bug Crashing when updating two bone ik target positions

Discussion in 'Animation Rigging' started by Its_Just_Lowe, Jan 18, 2023.

  1. Its_Just_Lowe

    Its_Just_Lowe

    Joined:
    Apr 18, 2019
    Posts:
    1
    Hi all, not sure if there's already an answer to this issue but I've been searching for a few hours and can't find anything.

    I have a third-person player character, and both arms have the two-bone IK constraint on them. I have their target transform constantly following game objects that are attached to the weapon. However, Unity seems to crash when I play test. It seems to only be when the player is aiming in or out so I've assumed it's something to do with updating the positions of the targets as I know they use jobs behind the scenes.

    I've added my code below that is responsible for the aiming in and out. As you can see, I tried fixing it by adding an aim timer but it was no use.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Cinemachine;
    5. using StarterAssets;
    6.  
    7. public class ThirdPersonShooterController : MonoBehaviour
    8. {
    9.     [SerializeField]CinemachineVirtualCamera aimVirtualCamera;
    10.     [SerializeField]float normalSensitivity;
    11.     [SerializeField]float aimSensitivity;
    12.     [SerializeField]GameObject crosshair;
    13.     [SerializeField]LayerMask aimColliderLayerMask;
    14.     [SerializeField]Transform debugTransform;
    15.     [SerializeField]GameObject projectile;
    16.     [SerializeField]Weapon currentWeapon;
    17.     [SerializeField]Transform weaponHolsteredPosition;
    18.     [SerializeField]Transform weaponDrawnPosition;
    19.     [SerializeField]IK_Target iK_Targets;
    20.     [SerializeField]Transform leftHandIK_Hint;
    21.     [SerializeField]Transform rightHandIK_Hint;
    22.  
    23.     StarterAssetsInputs starterAssetsInputs;
    24.     ThirdPersonController thirdPersonController;
    25.     bool lastAimState = false;
    26.     float aimTimer;
    27.     float minAimTime = 0.1f;
    28.  
    29.     public void SetCurrentWeapon(Weapon _weapon)
    30.     {
    31.         currentWeapon.transform.parent = null;
    32.         currentWeapon = _weapon;
    33.     }
    34.  
    35.     void Awake()
    36.     {
    37.         thirdPersonController = GetComponent<ThirdPersonController>();
    38.         starterAssetsInputs = GetComponent<StarterAssetsInputs>();  
    39.     }
    40.  
    41.     void Start()
    42.     {
    43.         SetHolsterPositions();  
    44.     }
    45.  
    46.     void Update()
    47.     {
    48.         aimTimer += Time.deltaTime;
    49.         Debug.Log(aimTimer);
    50.  
    51.         Vector3 mouseWorldPosition = Vector3.zero;
    52.  
    53.         Vector2 screenCenterPoint = new Vector2(Screen.width / 2f, Screen.height / 2f);
    54.         Ray ray = Camera.main.ScreenPointToRay(screenCenterPoint);
    55.         Transform hitTransform = null;
    56.         if(Physics.Raycast(ray, out RaycastHit raycastHit, 999f, aimColliderLayerMask))
    57.         {
    58.             debugTransform.position = raycastHit.point;
    59.             mouseWorldPosition = raycastHit.point;
    60.             hitTransform = raycastHit.transform;
    61.         }
    62.  
    63.         SetIK_Values();
    64.  
    65.         if(starterAssetsInputs.aim)
    66.         {
    67.             Vector3 worldAimTarget = mouseWorldPosition;
    68.             worldAimTarget.y = transform.position.y;
    69.             Vector3 aimDirection = (worldAimTarget - transform.position).normalized;
    70.  
    71.             transform.forward = Vector3.Lerp(transform.forward, aimDirection, Time.deltaTime * 20f);
    72.  
    73.             if(starterAssetsInputs.aim != lastAimState)
    74.             {
    75.                 if(aimTimer > minAimTime)
    76.                 {
    77.                     aimVirtualCamera.gameObject.SetActive(true);
    78.                     thirdPersonController.SetSensitivity(aimSensitivity);
    79.                     thirdPersonController.SetRotateOnMove(false);
    80.                     thirdPersonController.SetCanSprint(false);
    81.                     crosshair.SetActive(true);
    82.  
    83.                     SetAimPositions();
    84.  
    85.                     aimTimer = 0f;
    86.                 }
    87.             }
    88.         }
    89.         else
    90.         {
    91.  
    92.             if(starterAssetsInputs.aim != lastAimState)
    93.             {
    94.                 if(aimTimer > minAimTime)
    95.                 {
    96.                     aimVirtualCamera.gameObject.SetActive(false);
    97.                     thirdPersonController.SetSensitivity(normalSensitivity);
    98.                     thirdPersonController.SetRotateOnMove(true);
    99.                     thirdPersonController.SetCanSprint(true);
    100.                     crosshair.SetActive(false);
    101.  
    102.                     SetHolsterPositions();
    103.  
    104.                     aimTimer = 0f;
    105.                 }
    106.             }
    107.         }
    108.  
    109.         if(starterAssetsInputs.shoot)
    110.         {
    111.             if(hitTransform != null)
    112.             {
    113.                 if(hitTransform.GetComponent<BulletTarget>() != null)
    114.                 {
    115.                     Debug.Log("Hit!");
    116.                 }
    117.                 else
    118.                 {
    119.                     Debug.Log("Miss!");
    120.                 }
    121.             }
    122.  
    123.             Vector3 aimDir = (mouseWorldPosition - currentWeapon.GetBulletSpawnPosition().position).normalized;
    124.             GameObject newProjectile = Instantiate(projectile, currentWeapon.GetBulletSpawnPosition().position, Quaternion.LookRotation(aimDir, Vector3.up));
    125.             newProjectile.GetComponent<BulletRaycastProjectile>().Setup(raycastHit.point);
    126.  
    127.             starterAssetsInputs.shoot = false;
    128.         }
    129.     }
    130.  
    131.     void SetIK_Values()
    132.     {
    133.         iK_Targets.GetLeftHandIK_Target().position = currentWeapon.GetIK_Targets().GetLeftHandIK_Target().position;
    134.         iK_Targets.GetLeftHandIK_Target().rotation = currentWeapon.GetIK_Targets().GetLeftHandIK_Target().rotation;
    135.  
    136.         iK_Targets.GetRightHandIK_Target().position = currentWeapon.GetIK_Targets().GetRightHandIK_Target().position;
    137.         iK_Targets.GetRightHandIK_Target().rotation = currentWeapon.GetIK_Targets().GetRightHandIK_Target().rotation;
    138.     }
    139.  
    140.     void SetAimPositions()
    141.     {
    142.         while(currentWeapon.transform.position != weaponDrawnPosition.position && currentWeapon.transform.forward != weaponDrawnPosition.forward)
    143.         {
    144.             currentWeapon.transform.position = Vector3.Lerp(currentWeapon.transform.position, weaponDrawnPosition.position, Time.deltaTime * 20f);
    145.             currentWeapon.transform.forward = Vector3.Lerp(currentWeapon.transform.forward, weaponDrawnPosition.forward, Time.deltaTime * 20f);
    146.         }
    147.         lastAimState = starterAssetsInputs.aim;
    148.  
    149.         currentWeapon.transform.parent = weaponDrawnPosition;
    150.  
    151.         leftHandIK_Hint.position = currentWeapon.GetIK_Hints().GetLeftHandIK_AimHint().position;
    152.         leftHandIK_Hint.localRotation = currentWeapon.GetIK_Hints().GetLeftHandIK_AimHint().localRotation;
    153.  
    154.         rightHandIK_Hint.position = currentWeapon.GetIK_Hints().GetRightHandIK_AimHint().position;
    155.         rightHandIK_Hint.localRotation = currentWeapon.GetIK_Hints().GetRightHandIK_AimHint().localRotation;
    156.     }
    157.  
    158.     void SetHolsterPositions()
    159.     {
    160.         while(currentWeapon.transform.position != weaponHolsteredPosition.position + currentWeapon.GetHolsterPositionOffset() &&
    161.         currentWeapon.transform.forward != weaponHolsteredPosition.forward + currentWeapon.GetHolsterRotationOffset())
    162.         {
    163.             currentWeapon.transform.position = Vector3.Lerp(currentWeapon.transform.position, weaponHolsteredPosition.position + currentWeapon.GetHolsterPositionOffset(), Time.deltaTime * 20f);
    164.             currentWeapon.transform.forward = Vector3.Lerp(currentWeapon.transform.forward, weaponHolsteredPosition.forward + currentWeapon.GetHolsterRotationOffset(), Time.deltaTime * 20f);
    165.         }
    166.  
    167.         lastAimState = starterAssetsInputs.aim;
    168.  
    169.         currentWeapon.transform.parent = weaponHolsteredPosition;
    170.         currentWeapon.transform.localPosition += currentWeapon.GetHolsterPositionOffset();
    171.         currentWeapon.transform.forward += currentWeapon.GetHolsterRotationOffset();
    172.  
    173.         leftHandIK_Hint.position = currentWeapon.GetIK_Hints().GetLeftHandIK_HolsterHint().position;
    174.         leftHandIK_Hint.localRotation = currentWeapon.GetIK_Hints().GetLeftHandIK_HolsterHint().localRotation;
    175.  
    176.         rightHandIK_Hint.position = currentWeapon.GetIK_Hints().GetRightHandIK_HolsterHint().position;
    177.         rightHandIK_Hint.localRotation = currentWeapon.GetIK_Hints().GetRightHandIK_HolsterHint().localRotation;
    178.     }
    179. }
     
  2. CorroX_

    CorroX_

    Joined:
    Oct 30, 2019
    Posts:
    1
    Hey, I'm currently encountering the same issue, have you found any fixes / workarounds?