我是一名彻头彻尾的 Unity 初学者。对于我的第一个项目,我正在尝试制作一款僵尸射击游戏。我有新的敌人要生成,还有一个玩家和所有东西,但是我拥有的敌人预制件并没有将我的玩家对象放入其脚本“输入”中以供角色追逐。如果我把它放进去,它会说“类型不匹配”。我该如何解决这个问题??
敌人脚本
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
}
}
}
玩家脚本
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);
}
}
}
我尝试过一些没有什么新意的东西,而且完全一无所知。抱歉,如果脚本中的内容很混乱或根本没有必要,我已经把所有东西都扔到墙上来修复它并计划在之后进行完善。
获取组件时需要指定对象
characterController = characterControllerObject.GetComponent<CharacterController>();