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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CustomGravity : MonoBehaviour
{
public float gravityStrength = 9.8f;
public Vector3 gravityDirection;
public Vector3 gravity;
// Start is called before the first frame update
void Start()
{
gravityDirection = -Vector3.up;
}
// Update is called once per frame
void Update()
{
Rigidbody rb = GetComponent<Rigidbody>();
gravity = gravityDirection.normalized * gravityStrength;
rb.AddForce(gravity);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GravityChangeToForward : MonoBehaviour
{
Vector3 newGravity;
Vector3 closestPoint;
GameObject hitObject;
float radiusOfDetectionSphere;
private int LayerGround;
GameObject player;
public LayerMask GroundMask;
float[] movementAndSurfaceAngleArray;
Vector3[] touchingSurfaces;
// Start is called before the first frame update
void Start()
{
player = GameObject.Find("Player");
LayerGround = LayerMask.NameToLayer("Ground");
}
// Update is called once per frame
void Update()
{
float playerRadius = GetComponentInChildren<SphereCollider>().radius * Mathf.Max(transform.lossyScale.x, transform.lossyScale.y, transform.lossyScale.z);
Rigidbody rb = GetComponent<Rigidbody>();
radiusOfDetectionSphere = playerRadius + 0.01f;
Collider[] hitColliders = Physics.OverlapSphere(rb.transform.position, radiusOfDetectionSphere, GroundMask);
if (hitColliders != null && hitColliders.Length > 0)
{
print(1);
int i = 0;
while (i < hitColliders.Length)
{
print(2);
RaycastHit touchingObjectData;
closestPoint = hitColliders.ClosestPoint(rb.transform.position);
Ray distanceToObject = new Ray(rb.transform.position, closestPoint);
if (Physics.Raycast(distanceToObject, out touchingObjectData, 3))
{
print(3);
PlayerMovement playerMovement = player.GetComponent<PlayerMovement>();
var movementAndSurfaceAngleDecimal = Vector3.Angle(touchingObjectData.normal, playerMovement.moveDirection);
movementAndSurfaceAngleArray = (float)movementAndSurfaceAngleDecimal;
}
}
var bestSurface = Mathf.Max(movementAndSurfaceAngleArray);
print(4);
foreach (int z in movementAndSurfaceAngleArray)
{
print(5);
if (movementAndSurfaceAngleArray[z] == bestSurface && z > 0)
{
print(6);
newGravity = touchingSurfaces[z];
}
}
CustomGravity customGravity = player.GetComponent<CustomGravity>();
customGravity.gravityDirection = -newGravity;
var rotation = Quaternion.LookRotation(newGravity) * Quaternion.FromToRotation(Vector3.right, Vector3.forward) * Quaternion.FromToRotation(Vector3.up, Vector3.right);
player.transform.rotation = rotation;
}
}
}
------------------------------------------------------------------------------------------------------------------------------
*_ <em>*using System.Collections; using System.Collections.Generic; using UnityEngine; public class MoveCamera : MonoBehaviour { [SerializeField] Transform cameraPosition = null; void Update() { transform.position = cameraPosition.position; } }*</em> _*
------------------------------------------------------------------------------------------------------------------------------
```
*using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerLook : MonoBehaviour
{
[Header(“References”)]
[SerializeField] WallRun wallRun;
[SerializeField] private float sensX = 100f;
[SerializeField] private float sensY = 100f;
[SerializeField] Transform cam = null;
[SerializeField] Transform orientation = null;
float mouseX;
float mouseY;
float multiplier = 0.01f;
float xRotation;
float yRotation;
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
private void Update()
{
mouseX = Input.GetAxisRaw(“Mouse X”);
mouseY = Input.GetAxisRaw(“Mouse Y”);
yRotation += mouseX * sensX * multiplier;
xRotation -= mouseY * sensY * multiplier;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
cam.transform.rotation = Quaternion.Euler(xRotation, yRotation, wallRun.tilt);
orientation.transform.rotation = Quaternion.Euler(0, yRotation, 0);
}
}
_*_ _*------------------------------------------------------------------------------------------------------------------------------*_ _*
_
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
float playerHeight = 2f;
[SerializeField] Transform orientation;
[Header(“Movement”)]
[SerializeField] float moveSpeed = 6f;
[SerializeField] float airMultiplier = 0.4f;
float movementMultiplier = 10f;
[Header(“Sprinting”)]
[SerializeField] float walkSpeed = 4f;
[SerializeField] float sprintSpeed = 6f;
[SerializeField] float acceleration = 10f;
[Header(“Jumping”)]
public float jumpForce = 5f;
[Header(“Keybinds”)]
[SerializeField] KeyCode jumpKey = KeyCode.Space;
[SerializeField] KeyCode sprintKey = KeyCode.LeftShift;
[Header(“Drag”)]
[SerializeField] float groundDrag = 6f;
[SerializeField] float airDrag = 2f;
float horizontalMovement;
float verticalMovement;
[Header(“Ground Detection”)]
[SerializeField] Transform groundCheck;
[SerializeField] LayerMask groundMask;
[SerializeField] float groundDistance = 0.2f;
public bool isGrounded { get; private set; }
public Vector3 moveDirection;
Vector3 slopeMoveDirection;
Rigidbody rb;
RaycastHit slopeHit;
private bool OnSlope()
{
if (Physics.Raycast(transform.position, Vector3.down, out slopeHit, playerHeight / 2 + 0.5f))
{
if (slopeHit.normal != Vector3.up)
{
return true;
}
else
{
return false;
}
}
return false;
}
private void Start()
{
rb = GetComponent();
rb.freezeRotation = true;
}
private void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
MyInput();
ControlDrag();
ControlSpeed();
if (Input.GetKeyDown(jumpKey) && isGrounded)
{
Jump();
}
slopeMoveDirection = Vector3.ProjectOnPlane(moveDirection, slopeHit.normal);
}
void MyInput()
{
horizontalMovement = Input.GetAxisRaw(“Horizontal”);
verticalMovement = Input.GetAxisRaw(“Vertical”);
moveDirection = orientation.forward * verticalMovement + orientation.right * horizontalMovement;
}
void Jump()
{
if (isGrounded)
{
rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z);
rb.AddForce(transform.up * jumpForce, ForceMode.Impulse);
}
}
void ControlSpeed()
{
if (Input.GetKey(sprintKey) && isGrounded)
{
moveSpeed = Mathf.Lerp(moveSpeed, sprintSpeed, acceleration * Time.deltaTime);
}
else
{
moveSpeed = Mathf.Lerp(moveSpeed, walkSpeed, acceleration * Time.deltaTime);
}
}
void ControlDrag()
{
if (isGrounded)
{
rb.drag = groundDrag;
}
else
{
rb.drag = airDrag;
}
}
private void FixedUpdate()
{
MovePlayer();
}
void MovePlayer()
{
if (isGrounded && !OnSlope())
{
rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
}
else if (isGrounded && OnSlope())
{
rb.AddForce(slopeMoveDirection.normalized * moveSpeed * movementMultiplier, ForceMode.Acceleration);
}
else if (!isGrounded)
{
rb.AddForce(moveDirection.normalized * moveSpeed * movementMultiplier * airMultiplier, ForceMode.Acceleration);
}
}
}
_*_ _*------------------------------------------------------------------------------------------------------------------------------*_ _*
_
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WallRun : MonoBehaviour
{
public float tilt { get; private set; }
}
_```_
_------------------------------------------------------------------------------------------------------------------------------*_