using System.Collections.Generic;
using UnityEngine;
public class enemybullet : MonoBehaviour
{
Transform player;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
Vector3 lastpos = player.position;
Destroy(gameObject, 4f);
}
gets last pos of player
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, player.position, 10f);
}
走向玩家
我希望它拍摄玩家的方向,并且不允许我在movetowards功能中使用lastpos
答案 0 :(得分:1)
这里的问题是您的变量scope
。
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
Vector3 lastpos = player.position;
Destroy(gameObject, 4f);
}
正如您在此处看到的那样,您已经在{...}
(作用域)中声明了变量,这意味着该变量将仅在其创建的范围内可见(并且实际上执行一离开{...}
要解决此问题,您需要在整个类的范围内声明变量
public Vector3 lastpos;
void Start()
{
player = GameObject.FindGameObjectWithTag("Player").transform;
lastpos = player.position;
Destroy(gameObject, 4f);
}
您现在可以在班级内(甚至班级外)的任何地方访问lastpos