- Home /
First Person Controller Script not working
Hello, I am using standard assets for first person controller. when I drag the FPS prefab onto the scene, it says 'the associated script cannot be loaded. Please fix any compile errors and assign a valid script'. Pls help thank uu
hay this is how I do first person controller with mouse look
create a empty object (call this your "Player" give it the "Player" tag), add character controller, add the player movement script, add as a child a capsule taller than wide ( offset the "Player" at the top of the capsule ), add a second child with camera and the mouse look script (also place at top of capsule)
PlayerMovement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 5f;
public float gravity = -15f;
Vector3 velocity;
bool isGrounded;
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
MouseLook :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseXSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseXSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseXSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
hope it helps
You need to make sure that there are no syntax errors and that the FPS controller is compatible with the same version as unity that you are using. If yes, make sure that the script class matches the script name. If yes, then something is just wrong with the script in general.
Answer by beck8 · May 12, 2020 at 10:29 AM
I am having the same problem, may be due to the new unity editor update.,I have the same problem I am wondering if this is due to the new unity editor update.
Answer by shurtleffja · Sep 25, 2020 at 08:08 PM
https://answers.unity.com/questions/1638555/guitexture-adn-guitext-are-obsolete-standard-asset.html
I had the same problem and this answer really helped
so what did you actually do, because that's a solution to what appears to be an entirely different problem?
Answer by rayith · Feb 20 at 04:45 PM
hay this is how I do first person controller with mouse look
create a empty object (call this your "Player" give it the "Player" tag), add character controller, add the player movement script, add as a child a capsule taller than wide ( offset the "Player" at the top of the capsule ), add a second child with camera and the mouse look script (also place at top of capsule)
PlayerMovement:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public CharacterController controller;
public float speed = 5f;
public float gravity = -15f;
Vector3 velocity;
bool isGrounded;
// Update is called once per frame
void Update()
{
float x = Input.GetAxis("Horizontal");
float z = Input.GetAxis("Vertical");
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * speed * Time.deltaTime);
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
}
}
Mouse look:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MouseLook : MonoBehaviour
{
public float mouseXSensitivity = 100f;
public Transform playerBody;
float xRotation = 0f;
// Start is called before the first frame update
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * mouseXSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseXSensitivity * Time.deltaTime;
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
playerBody.Rotate(Vector3.up * mouseX);
}
}
hope it helps