When I press play my character controller turns black, the scale on the z axis is 0 and all the children are not visible. Now the thing this only happens when I press play — if I don't, everything is fine in my scene and game view.
Player move script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
public CharacterController controller;
public float gravity = -30;
public float sphereRadius = 0.2f;
public Transform groundCheck;
public LayerMask groundMask;
public float jumpHeight = 3f;
public float sprintSpeed = 20f;
public float walkSpeed = 15f;
public float crouchHeight = 0.5f;
public float crouchSpeed;
public float startHeight;
private Vector3 velocity;
private bool isGrounded = true;
private float speed;
private void Start() {
velocity.y = 0f;
speed = walkSpeed;
crouchSpeed = speed / 2;
startHeight = transform.localScale.y;
}
// Update is called once per frame
private void Update()
{
isGrounded = Physics.CheckSphere(groundCheck.position, sphereRadius, groundMask);
float x = Input.GetAxisRaw("Horizontal");
float z = Input.GetAxisRaw("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move.normalized * speed * Time.deltaTime);
if (velocity.y < -0.35f)
{
velocity.y = -0.35f;
}
// Jumping and crouching
if (Input.GetButtonDown("Jump") && isGrounded)
{
velocity.y = jumpHeight;
} else if (Input.GetKey(KeyCode.LeftShift)) {
speed -= 7.5f;
transform.localScale = new Vector3(transform.localScale.x, crouchHeight, transform.localRotation.z);
} else if (!Input.GetKey(KeyCode.LeftShift)) {
speed = walkSpeed;
transform.localScale = new Vector3(transform.localScale.x, startHeight, transform.localRotation.z);
}
// Sprinting
if (Input.GetKey(KeyCode.LeftControl))
{
speed = sprintSpeed;
}
else if (!Input.GetKey(KeyCode.LeftControl))
{
speed = walkSpeed;
}
velocity.y += gravity * Time.deltaTime * Time.deltaTime;
controller.Move(velocity);
}
}
Player script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public float health = 100f;
public void TakeDamage(float amount)
{
health -= amount;
if (health <= 0)
Destroy(gameObject);
}
}
Camera look script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraLook : MonoBehaviour
{
public float sensitivity = 100f;
public Transform playerBody;
private float xRotation;
private void Start() {
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * sensitivity * Time.fixedDeltaTime;
float mouseY = Input.GetAxis("Mouse Y") * sensitivity * Time.fixedDeltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
Here is a comparison for when I am playing versus when not:
localRotation.z
as the z component of yourlocalScale
, which does not make much sense. Did you mean to use scale here, not rotation? \$\endgroup\$