My script is not working and i dont understand why it says “The type arguments for method ‘Component.GetComponent()’ cannot be inferred from the usage. Try specifying the type arguments explicitly.”
The script is here
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class Player : MonoBehaviour
{
public float Speed = 0f; //add these values in editor
public float JumpSpeed = 0f;
public float Gravity = 0f;
public Camera PlayerCam;
public float LookSpeed = 0f;
public float XLimit = 0f;
CharacterController characterController;
Vector3 moveDirection = Vector3.zero;
Vector2 rotation = Vector2.zero;
//optional
public bool canMove = true;
private void Start()
{
characterController = GetComponent();
rotation.y = transform.eulerAngles.y;
}
private void Update()
{
if (characterController.isGrounded)
{
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 right = transform.TransformDirection(Vector3.right);
float curSpeedX = canMove ? Speed * Input.GetAxis(“Vertical”) : 0;
float curSpeedY = canMove ? Speed * Input.GetAxis(“Horizontal”) : 0;
moveDirection = (forward * curSpeedX) + (right * curSpeedY);
if (Input.GetButton(“Jump”) && canMove)
{
moveDirection.y = JumpSpeed;
}
}
moveDirection.y -= Gravity * Time.deltaTime;
characterController.Move(moveDirection * Time.deltaTime);
if (canMove)
{
rotation.y += Input.GetAxis(“MouseX”) * LookSpeed;
rotation.x += -Input.GetAxis(“MouseY”) * LookSpeed;
rotation.x = Mathf.Clamp(rotation.x, -XLimit, XLimit);
PlayerCam.transform.localRotation = Quaternion.Euler(rotation.x, 0, 0);
transform.eulerAngles = new Vector2(0, rotation.y);
}
}
}