I am a complete Unity beginner. For my first project I'm trying to do a zombie shooter. I got new enemies to spawn and a player and everything, but the enemy prefab I have isn't taking my player object in it's script 'inputs' for a character to chase. If I do put it in, it says 'type mismatch'. How do I fix this??
Enemy script
using System.Runtime.CompilerServices;
using Unity.VisualScripting;
using UnityEditor.UI;
using UnityEngine;
using UnityEngine.UIElements;
public class EnemyFollow : MonoBehaviour
{
public float moveSpeed = 5f; // The speed at which the enemy moves
public float stoppingDistance = 2f; // The distance at which the enemy should stop following
public GameObject target;
void Start() {
target = GameObject.FindWithTag("Player");
}
private void Update()
{
if (transform.position.y <= -200) {
Destroy(gameObject);
}
if (target != null)
{
// Calculate the direction to the target
Vector3 direction = target.transform.position - transform.position;
// Check if the distance to the target is greater than the stopping distance
if (direction.magnitude > stoppingDistance)
{
// Move towards the target
transform.Translate(direction.normalized * moveSpeed * Time.deltaTime, Space.World);
}
// Stop moving when close to the target
// You can add additional logic here, such as attacking the target
}
}
}
Player script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public class playerScript : MonoBehaviour
{
public Camera playerCamera;
public float walkSpeed = 6f;
public float runSpeed = 12f;
public float jumpPower = 7f;
public float gravity = 10f;
public float lookSpeed = 2f;
public float lookXLimit = 45f;
public float defaultHeight = 2f;
public float crouchHeight = 1f;
public float crouchSpeed = 3f;
private Vector3 moveDirection = Vector3.zero;
private float rotationX = 0;
private CharacterController characterController;
private bool canMove = true;
void Start()
{
characterController = GetComponent<CharacterController>();
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update()
{
Vector3 forward = transform.TransformDirection(Vector3.forward);
Vector3 right = transform.TransformDirection(Vector3.right);
bool isRunning = Input.GetKey(KeyCode.LeftShift);
float curSpeedX = canMove ? (isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Vertical") : 0;
float curSpeedY = canMove ? (isRunning ? runSpeed : walkSpeed) * Input.GetAxis("Horizontal") : 0;
float movementDirectionY = moveDirection.y;
moveDirection = (forward * curSpeedX) + (right * curSpeedY);
if (Input.GetButton("Jump") && canMove && characterController.isGrounded)
{
moveDirection.y = jumpPower;
}
else
{
moveDirection.y = movementDirectionY;
}
if (!characterController.isGrounded)
{
moveDirection.y -= gravity * Time.deltaTime;
}
if (Input.GetKey(KeyCode.R) && canMove)
{
characterController.height = crouchHeight;
walkSpeed = crouchSpeed;
runSpeed = crouchSpeed;
}
else
{
characterController.height = defaultHeight;
walkSpeed = 6f;
runSpeed = 12f;
}
characterController.Move(moveDirection * Time.deltaTime);
if (canMove)
{
rotationX += -Input.GetAxis("Mouse Y") * lookSpeed;
rotationX = Mathf.Clamp(rotationX, -lookXLimit, lookXLimit);
playerCamera.transform.localRotation = Quaternion.Euler(rotationX, 0, 0);
transform.rotation *= Quaternion.Euler(0, Input.GetAxis("Mouse X") * lookSpeed, 0);
}
}
}
I've tried some things with nothing new and am completely clueless. Sorry if things in the script are messy or not necessary at all, I've been throwing everything at the wall to fix it and plan to polish after.
If you're a beginner I would advise not asking questions here because people on here are typically quite rude to beginners. Though your question is a bit confusing still.
A "type" is like a gameobject, int, string, etc. It's a type of variable, what the variable being referenced is basically. So what's clearly happening is that wherever your error is happening that line is trying to assign an object of some kind to a variable type that does not match what the object is.
So specifically if the script is trying to assign a collider as a character controller variable or a gameobject as a character controller.
Also you're best off looking up "brackeys navmesh" tutorials especially if you really don't have much experience, because trying to code your own pathfinding will be frustrating. I see you're directly using the transform to move the npcs and that's a big nono. That causes a lot of problems.
I hope this helps.