c# - transform.position 在 Unity 中无法正常工作
问题描述
我正在统一制作游戏,我想获得玩家的位置(女巫是预制件)所以我使用敌人的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyScript : MonoBehaviour
{
public Transform player;
void Start()
{
Debug.Log(player.position.x);
}
}
我有一个生成器,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SpawnEnemies : MonoBehaviour
{
public GameObject enemy;
float randX;
float randY;
Vector2 whereToSpawn;
public float spawnRate = 2f;
float nextSpawn = 0.0f;
void Update()
{
if (Time.time > nextSpawn)
{
nextSpawn = Time.time + spawnRate;
randX = Random.Range(-6.36f, 6.36f);
randY = Random.Range(-4.99f, 4.99f);
whereToSpawn = new Vector2(randX, randY);
Instantiate (enemy, whereToSpawn, Quaternion.identity);
}
}
}
但是当我运行它时,它总是给我 (0, 0, 0)。为什么我得到 0 以及如何修复它(获取玩家的当前位置)?
解决方案
推荐阅读
- css - 按钮垂直组,在引导程序 4 中与底部对齐
- python - 如何将修改后的图像列表保存到另一个文件夹中?
- azure - 在 Azure ML 工作区中部署 autoML StackEnsemble 模型时出错
- python - 无法再在 jinja2 中创建 packageloader
- docker - 如何配置 docker-compose.yaml 以访问 Ubuntu 18 中的主机 Postgres?
- angular - 使用按钮而不是滑动来导航 radcalender
- excel - 如何复制粘贴,收到 VBA 错误
- c# - 如果.Count == 0,条件运算符不显示
- java - JAVA - 为扩展另一个类的子类的字段设置值
- vba - 如何使用 RunCode 运行函数?