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

Resolved Unity suddenly stops responding when I put the Ground tag as the GroundMask and press play.

Discussion in 'Scripting' started by Tortilladog, May 2, 2023.

  1. Tortilladog

    Tortilladog

    Joined:
    Jul 29, 2022
    Posts:
    2
    Unity suddenly stops responding when I put the Ground tag as the GroundMask and press play, and I can not figure out why. It does not even throw an error, just stops responding. It happens as soon as the ground check detects something. Does anyone here know what the problem could be? Thank you!



    The files I currently have:


    ------------------------------------------------------------------------------------------------------------------------------

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class CustomGravity : MonoBehaviour
    6. {
    7.     public float gravityStrength = 9.8f;
    8.     public Vector3 gravityDirection;
    9.     public Vector3 gravity;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         gravityDirection = -Vector3.up;
    14.      
    15.     }
    16.     // Update is called once per frame
    17.     void Update()
    18.     {
    19.         Rigidbody rb = GetComponent<Rigidbody>();
    20.         gravity = gravityDirection.normalized * gravityStrength;
    21.         rb.AddForce(gravity);
    22.     }
    23. }
    24.  
    25.  
    26.  
    ------------------------------------------------------------------------------------------------------------------------------

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class GravityChangeToForward : MonoBehaviour
    6. {
    7.     Vector3 newGravity;
    8.     Vector3 closestPoint;
    9.     GameObject hitObject;
    10.     float radiusOfDetectionSphere;
    11.     private int LayerGround;
    12.     GameObject player;
    13.     public LayerMask GroundMask;
    14.     float[] movementAndSurfaceAngleArray;
    15.     Vector3[] touchingSurfaces;
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         player = GameObject.Find("Player");
    20.         LayerGround = LayerMask.NameToLayer("Ground");
    21.     }
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         float playerRadius = GetComponentInChildren<SphereCollider>().radius * Mathf.Max(transform.lossyScale.x, transform.lossyScale.y, transform.lossyScale.z);
    26.         Rigidbody rb = GetComponent<Rigidbody>();
    27.         radiusOfDetectionSphere = playerRadius + 0.01f;
    28.         Collider[] hitColliders = Physics.OverlapSphere(rb.transform.position, radiusOfDetectionSphere, GroundMask);
    29.         if (hitColliders != null && hitColliders.Length > 0)
    30.         {
    31.             print(1);
    32.             int i = 0;
    33.             while (i < hitColliders.Length)
    34.             {
    35.                 print(2);
    36.                 RaycastHit touchingObjectData;
    37.                 closestPoint = hitColliders.ClosestPoint(rb.transform.position);
    38.                 Ray distanceToObject = new Ray(rb.transform.position, closestPoint);
    39.                 if (Physics.Raycast(distanceToObject, out touchingObjectData, 3))
    40.                 {
    41.                     print(3);
    42.                     PlayerMovement playerMovement = player.GetComponent<PlayerMovement>();
    43.                     var movementAndSurfaceAngleDecimal = Vector3.Angle(touchingObjectData.normal, playerMovement.moveDirection);
    44.                     movementAndSurfaceAngleArray = (float)movementAndSurfaceAngleDecimal;
    45.                 }
    46.             }
    47.             var bestSurface = Mathf.Max(movementAndSurfaceAngleArray);
    48.             print(4);
    49.             foreach (int z in movementAndSurfaceAngleArray)
    50.             {
    51.                 print(5);
    52.                 if (movementAndSurfaceAngleArray[z] == bestSurface && z > 0)
    53.                 {
    54.                     print(6);
    55.                     newGravity = touchingSurfaces[z];
    56.                 }
    57.             }
    58.             CustomGravity customGravity = player.GetComponent<CustomGravity>();
    59.             customGravity.gravityDirection = -newGravity;
    60.             var rotation = Quaternion.LookRotation(newGravity) * Quaternion.FromToRotation(Vector3.right, Vector3.forward) * Quaternion.FromToRotation(Vector3.up, Vector3.right);
    61.             player.transform.rotation = rotation;
    62.         }
    63.      
    64.     }
    65. }
    66.  
    67.  
    68.  
    ------------------------------------------------------------------------------------------------------------------------------

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class MoveCamera : MonoBehaviour
    6. {
    7.     [SerializeField] Transform cameraPosition = null;
    8.     void Update()
    9.     {
    10.         transform.position = cameraPosition.position;
    11.     }
    12. }
    13.  
    14.  
    15.  
    ------------------------------------------------------------------------------------------------------------------------------

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class PlayerLook : MonoBehaviour
    6. {
    7.     [Header("References")]
    8.     [SerializeField] WallRun wallRun;
    9.     [SerializeField] private float sensX = 100f;
    10.     [SerializeField] private float sensY = 100f;
    11.     [SerializeField] Transform cam = null;
    12.     [SerializeField] Transform orientation = null;
    13.     float mouseX;
    14.     float mouseY;
    15.     float multiplier = 0.01f;
    16.     float xRotation;
    17.     float yRotation;
    18.     private void Start()
    19.     {
    20.         Cursor.lockState = CursorLockMode.Locked;
    21.         Cursor.visible = false;
    22.     }
    23.     private void Update()
    24.     {
    25.         mouseX = Input.GetAxisRaw("Mouse X");
    26.         mouseY = Input.GetAxisRaw("Mouse Y");
    27.        
    28.         yRotation += mouseX * sensX * multiplier;
    29.         xRotation -= mouseY * sensY * multiplier;
    30.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    31.         cam.transform.rotation = Quaternion.Euler(xRotation, yRotation, wallRun.tilt);
    32.         orientation.transform.rotation = Quaternion.Euler(0, yRotation, 0);
    33.     }
    34. }
    35.  
    36.  
    37.  
    ------------------------------------------------------------------------------------------------------------------------------

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class PlayerMovement : MonoBehaviour
    6. {
    7.     float playerHeight = 2f;
    8.     [SerializeField] Transform orientation;
    9.     [Header("Movement")]
    10.     [SerializeField] float moveSpeed = 6f;
    11.     [SerializeField] float airMultiplier = 0.4f;
    12.     float movementMultiplier = 10f;
    13.     [Header("Sprinting")]
    14.     [SerializeField] float walkSpeed = 4f;
    15.     [SerializeField] float sprintSpeed = 6f;
    16.     [SerializeField] float acceleration = 10f;
    17.     [Header("Jumping")]
    18.     public float jumpForce = 5f;
    19.     [Header("Keybinds")]
    20.     [SerializeField] KeyCode jumpKey = KeyCode.Space;
    21.     [SerializeField] KeyCode sprintKey = KeyCode.LeftShift;
    22.     [Header("Drag")]
    23.     [SerializeField] float groundDrag = 6f;
    24.     [SerializeField] float airDrag = 2f;
    25.     float horizontalMovement;
    26.     float verticalMovement;
    27.     [Header("Ground Detection")]
    28.     [SerializeField] Transform groundCheck;
    29.     [SerializeField] LayerMask groundMask;
    30.     [SerializeField] float groundDistance = 0.2f;
    31.     public bool isGrounded { get; private set; }
    32.     public Vector3 moveDirection;
    33.     Vector3 slopeMoveDirection;
    34.     Rigidbody rb;
    35.     RaycastHit slopeHit;
    36.     private bool OnSlope()
    37.     {
    38.         if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight / 2 + 0.5f))
    39.         {
    40.             if (slopeHit.normal != Vector3.up)
    41.             {
    42.                 return true;
    43.             }
    44.             else
    45.             {
    46.                 return false;
    47.             }
    48.         }
    49.         return false;
    50.     }
    51.     private void Start()
    52.     {
    53.         rb = GetComponent<Rigidbody>();
    54.         rb.freezeRotation = true;
    55.     }
    56.     private void Update()
    57.     {
    58.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    59.         MyInput();
    60.         ControlDrag();
    61.         ControlSpeed();
    62.         if (Input.GetKeyDown(jumpKey) && isGrounded)
    63.         {
    64.             Jump();
    65.         }
    66.         slopeMoveDirection = Vector3.ProjectOnPlane(moveDirection, slopeHit.normal);
    67.     }
    68.     void MyInput()
    69.     {
    70.         horizontalMovement = Input.GetAxisRaw("Horizontal");
    71.         verticalMovement = Input.GetAxisRaw("Vertical");
    72.         moveDirection = orientation.forward * verticalMovement + orientation.right * horizontalMovement;
    73.     }
    74.     void Jump()
    75.     {
    76.         if (isGrounded)
    77.         {
    78.             rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
    79.             rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
    80.         }
    81.     }
    82.     void ControlSpeed()
    83.     {
    84.         if (Input.GetKey(sprintKey) && isGrounded)
    85.         {
    86.             moveSpeed = Mathf.Lerp(moveSpeed, sprintSpeed, acceleration * Time.deltaTime);
    87.         }
    88.         else
    89.         {
    90.             moveSpeed = Mathf.Lerp(moveSpeed, walkSpeed, acceleration * Time.deltaTime);
    91.         }
    92.     }
    93.     void ControlDrag()
    94.     {
    95.         if (isGrounded)
    96.         {
    97.             rb.drag = groundDrag;
    98.         }
    99.         else
    100.         {
    101.             rb.drag = airDrag;
    102.         }
    103.     }
    104.     private void FixedUpdate()
    105.     {
    106.         MovePlayer();
    107.     }
    108.     void MovePlayer()
    109.     {
    110.         if (isGrounded && !OnSlope())
    111.         {
    112.             rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
    113.         }
    114.         else if (isGrounded && OnSlope())
    115.         {
    116.             rb.AddForce(slopeMoveDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
    117.         }
    118.         else if (!isGrounded)
    119.         {
    120.             rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier * airMultiplier, ForceMode.Acceleration);
    121.         }
    122.     }
    123. }
    124.  
    125.  
    126.  
    ------------------------------------------------------------------------------------------------------------------------------

    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class WallRun : MonoBehaviour
    6. {
    7.     public float tilt { get; private set; }
    8. }
    9.  
    10.  
    11.  
    ------------------------------------------------------------------------------------------------------------------------------
     
    Last edited: May 3, 2023
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,318
    Please edit your post to use code-tags as a wall of plain text is very difficult to understand.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,318
    Likely because you're causing an infinite loop. Always look for "while" loops then debug your logic. You can attach the a debugger and step through the code to see if what you think is happening, is actually happening.

    Code (CSharp):
    1. while (i < hitColliders.Length)
    Check that is being incremented correctly? At first glance, I can't see it anywhere.
     
  4. Tortilladog

    Tortilladog

    Joined:
    Jul 29, 2022
    Posts:
    2
    You are right about the incrementing not being present, I must have accidentally deleted it while making the if statement. I don't know how I didn't notice this in four hours of troubleshooting, thank you!