通过字符串获取转换组件以跟踪播放器



im试图通过t.find获取转换组件,然后通过转换组件,获取游戏对象组件,然后t.LookAt可以工作,并查看玩家。

这是代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class gunner : MonoBehaviour
{
//legacy statements: isShot
//these statements initialize all the main variables used in the project. most of these are for the "ai".
#region variables
[Header("gun stats")]
[Space(10)]
public float damage = 10f;
public float range = 100f;
[Space(10)]
[Header("Camera")]
[Space(10)]
public Camera cam;
[Space(10)]
[Header("AI Variables")]
[Space(10)]
public float shootlength;
public bool aiactive;
public float Speed;
public target self;
public Transform t;
public Rigidbody rb;
public string playername = "Player";
public GameObject PlayerObject;
public Transform PlayerTrans;
[Header("Decor")]
public ParticleSystem ps;
AudioSource audioData;
#endregion
#region gunmanager
private void Start()
{
GameObject PlayerObject = GameObject.Find(playername);
Transform PlayerTrans = PlayerObject.GetComponent<Transform>();
}
void Update()
{
if(aiactive && PlayerTrans != null)
{
Debug.Log("player trans found!");
}
if(aiactive && PlayerObject != null)
{
Debug.Log("player gameObject found!");
}
if(aiactive && PlayerTrans != null && PlayerObject != null)
{
Debug.Log("test passed!");
}
audioData = GetComponent<AudioSource>();
if (Input.GetButtonDown("Fire1") && !aiactive)
{
audioData.Play(0);
Shoot();
}
if(aiactive)
{

StartCoroutine(aiman());
}
}
public void Shoot()
{
ps.Play();
RaycastHit hit;
if(Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range))
{
Debug.Log("Hit:" + hit.transform.name);
target targ = hit.transform.GetComponent<target>();

if (targ != null)
{   
if(!aiactive)
{
self.hp += 50;
}
targ.TakeDamage(damage);

}
}
}
public void aiActivity()
{
if(self.hp >= 0)
{
t.LookAt(PlayerTrans);
rb.AddRelativeForce(Vector3.forward * Speed * Time.deltaTime);
RaycastHit hitplayer;
if(Physics.Raycast(cam.transform.position, cam.transform.forward, out hitplayer, shootlength))
{
if(hitplayer.transform == PlayerObject)
{
Shoot();
}
}
}
else
{
rb.useGravity = true;
aiactive = false;   
}

}
IEnumerator aiman() 
{
yield return new WaitForSeconds(1f);
aiActivity();
}
#endregion
}

所以没有编译器错误,但它无法找到播放器并向其浮动,因此,我无法使其工作。

以下是更新后的代码片段的一些问题,不确定它们是否能修复您遇到的所有错误。

您正在重新使用一个名为t的变量。我也不会把任何全局变量命名为非常短的随机字母,因为这会让人困惑。这是什么的Transform?如果它是玩家的变换,可以将其命名为PlayerTransfom

正如我提到的,你正在重新使用一个变量名,这是不允许的,尤其是在不同类型以及重新初始化的情况下。可以重新分配一个变量,使其更新、操作、更改等值,但在c#中不允许重新声明值,我认为在任何其他语言中都不允许(至少据我所知(。

具体地说,行target t = hit.transform.GetComponent<target>();t作为脚本组件类型target进行了分解,但您已经将其上面名为tTransform声明为public Transform t;

另一个问题是,您试图在全局设置中使用非静态方法分配变量。具体线路为public Transform player = Transform.Find(playername);。由于Find方法不是静态的,因此不能在方法之外设置这样的变换。改成

public Transform player;
private void Start()
{
player = Transform.Find(playername);
}

第二个错误是由于相同的原因,但由于这条线路public GameObject PlayerObject = player.GetComponent<GameObject>();。同样,GetComponent是一个非静态方法,因此不能在全局设置中调用。另一个问题是GameObject不是一个组件,而是一种类型。GameObjects具有组件,因此您无法获得其本身的组件,因为它不存在。您已经在使用Transform.Find来查找变换,我建议您只查找GameObject,存储该引用,然后从gameObject中获取Transform。如果您有GameObject引用,那么您实际上不需要将转换存储为其自己的引用。

public Transform player;
public GameObject PlayerObject;
private void Start()
{
PlayerObject = GameObject.Find(playername);
player = PlayerObject.transform;
}

输出的最后一个错误是指定您未正确使用LookAt。您需要传入Transform的类型,但您正在传入GameObject的类型。将其更改为…

t.LookAt(player);

这些问题大多是琐碎而直接的。我会跟随教程或阅读文档,以更好地理解C#/Unity/Programming。作为一个初学者是可以的,但当错误就在那里时,StackOverflow不应该仅仅用作调试帮助。我不知道我提到的4条建议是否会完全修复你的程序,因为我没有运行它。可能会有比我提到的更多的问题,但遵循错误应该会告诉你什么是错误的,为什么是错误的。它正在打印行号、文件以及断开的原因。

最新更新