[英]transform.lookat behaving weirdly OR I am missing something (unity3d c#)
I am trying to instantiate a prefab, a fireball, from a baddie, and at the time of instantiation pass the current player position to the fireball and use this to target the fireball at the player. 我正在尝试从坏家伙实例化一个预制的火球,并在实例化时将当前玩家位置传递到火球,并使用该位置将火球瞄准玩家。 The fireball translates/moves forward on its transform.forward vector.
火球在其transform.forward向量上平移/向前移动。 Thus the fireball should head from the baddie towards the player's position at the time the fireball was created.
因此,在创建火球时,火球应从坏蛋朝向玩家的位置前进。
What I am finding is that the direction is being set (via the transform.lookat()
function on the fireball), but it is almost always the wrong direction. 我发现的是方向已设置(通过火球上的
transform.lookat()
函数),但这几乎总是错误的方向。 It is not 90 or 180 off. 它不是90或180折。 How much it is off by depends on where the player is in respect to the baddie.
多少钱取决于玩家相对于坏蛋的位置。 The pattern is symmetrical around the z axis.
图案围绕z轴对称。
Assuming the baddie is at (0,0,0): 假设坏蛋位于(0,0,0):
These values move smoothly as the player moves through these angles. 这些值随着玩家在这些角度上的移动而平滑移动。 It only ever hits the player in one direction.
它只会向一个方向击中玩家。
I have tried the following (some of these were probably not required): 我尝试了以下方法(其中一些可能不是必需的):
lookat
variable to be: lookat
变量设置为:
player.transform.position - baddie.transform.position
baddie.transform.position - player.transform.position
These all yield the same results. 这些都产生相同的结果。
Here is my code. 这是我的代码。
Code to Instantiate the fireball from the baddie. 编写代码以从Baddie实例化火球。
public class Baddie : MonoBehaviour {
public GameObject fireballPrefab;
public GameObject playerGameObject;
public float countDownForFireball;
private float currentCountDownForFireball;
void Start () {
playerGameObject = GameObject.FindGameObjectWithTag("Player");
currentCountDownForFireball = 0;
}
void Update () {
if(currentCountDownForFireball <= 0)
{
GameObject newFireball = GameObject.Instantiate(fireballPrefab, transform.position, Quaternion.identity);
newFireball.GetComponent<Fireball>().SetTarget(playerGameObject.transform.position);
currentCountDownForFireball = countDownForFireball;
}
else
{
currentCountDownForFireball -= Time.deltaTime;
}
}
}
Code on the fireball 火球代码
public class Fireball : MonoBehaviour {
private float moveSpeed;
private Vector3 target;
// Use this for initialization
void Start () {
moveSpeed = 15f;
}
// Update is called once per frame
void Update (){
transform.Translate(transform.forward * Time.deltaTime * moveSpeed);
}
public void SetTarget(Vector3 newTarget){
target = newTarget;
transform.LookAt(target);
}
}
The error is in the usage of Transform.Translate
. 错误在于使用
Transform.Translate
。 Its secondary parameter is a Space
argument that defaults to Space.Self
. 它的第二个参数是一个
Space
参数,默认为Space.Self
。 You want the space of your direction to match the space you're translating in so you want to pass in Space.World
: 您希望方向的空间与您要转换的空间相匹配,以便传递给
Space.World
:
// transform.forward is the transform's forward vector in "world space"
transform.Translate(transform.forward * Time.deltaTime * moveSpeed, Space.World);
Alternatively, you can keep using the default Space.Self
with Vector3.forward
instead of transform.forward
: 另外,您可以继续使用
Space.Self
的默认Vector3.forward
而不是transform.forward
:
// Vector3.forward is (0,0,1). In "self space" this is "forward"
transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed); // Space.Self
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.