我在看这个令人困惑的教程,讲的是如何制作一个跟随玩家对象的简单AI。当在视频中这样做时,它告诉我做这个尚未定义的"castPoint"变量。我知道。。。我知道这听起来很简单,但这个家伙的教程太慢了,这让我这个有阅读障碍的人很难集中注意力并很好地理解。我在C#方面也不是最好的,所以是的。所以请有人帮帮我!
这是代码,视频,如果我是@t:
第1部分:https://youtu.be/nEYA3hzZHJ0
第二部分(如果我被遗漏了(15:05(,我想不太确定(:https://www.youtube.com/watch?v=2VX8uD_xUlM&list=PLnDBHcU45HjxLrObJRO-V_eiTOmJsIj0&index=4&t=264s
我的代码:
`using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Presentsaswehear : MonoBehaviour
{
// Start is called before the first frame update
//ignore this code!!!
private Animator Enemtestone;
public GameObject FirstTrig;
public static bool Izintyloip;
private bool Thecheckert;
//ignore this code!!!
//Enemy part tracking blablablaaaaa
[SerializeField]
public Transform Player;
[SerializeField]
float agroRange;
[SerializeField]
float moveSpeed;
private Rigidbody2D rb2det;
//End of ai part
void Start()
{
//ignore this code!!!
Enemtestone = GetComponent<Animator>();
//FirstTrig.SetActive(true);
Izintyloip = false;
Thecheckert = false;
//ignore this code!!!
rb2det = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Izintyloip == true) {
Enemtestone.SetInteger("Wakeup", 1);
Thecheckert = true;
}
if (CanSeePlayer(agroRange))
{
ChasePlayer();
}
else
{
StopChasingPlayer();
}
/*if (Thecheckert == true)
{
float distToPlayer = Vector2.Distance(transform.position, Player.position);
if (distToPlayer < agroRange)
{
ChasePlayer();
}
else
{
StopChasingPlayer();
}
}*/
}
bool CanSeePlayer(float distance)
{
bool val = false;
float castDist = distance;
Vector2 endPos = castPoint.position + Vector3.right * distance;
RaycastHit2D hit = Physics2D.Linecast(castPoint.position, endPos, 1 << LayerMask.NameToLayer("Action"));
if(hit.collider != null)
{
if (hit.collider.gameObject.CompareTag("Player"))
{
val = true;
}
else
{
val = false;
}
}
return val;
}
void ChasePlayer()
{
if(transform.position.x < Player.position.x)
{
rb2det.velocity = new Vector2(moveSpeed, 0);
transform.eulerAngles = new Vector3(0, 180, 0);
}
else
{
rb2det.velocity = new Vector2(-moveSpeed, 0);
transform.eulerAngles = new Vector3(0, 0, 0);
// if(transform.position.x > player.postion.x)
}
Enemtestone.SetBool("GFW", true);
}
void StopChasingPlayer()
{
rb2det.velocity = new Vector2(0, 0);
Enemtestone.SetBool("GFW", false);
}
}
`
您错过了7:49中他添加名为castPoint
的转换的步骤。