简体   繁体   English

表现怪异的transform.lookat或我错过了一些东西(unity3d C#)

[英]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):

  • when the player is at (0,0,10) the fireballs head directly towards the player 当玩家在(0,0,10)时,火球直接朝玩家
  • when the player is at (0,0,-10) the fireballs head away from the player (180 degrees. The exact opposite direction. The same direction as when the player was at (0,0,10)) 当玩家处于(0,0,-10)时,火球朝玩家离开(180度。正好相反的方向。与玩家处于(0,0,10)时的方向相同)
  • when the player is at (10,0,0) the fireballs head 90 degrees away from the player (to the players right if facing the baddie) 当玩家处于(10,0,0)时,火球会与玩家成90度角(如果面对坏蛋,则指向玩家右侧)
  • when the player is at (-10,0,0) the fireballs head 90 degrees away from the player (to the players left if facing the baddie) 当玩家处于(-10,0,0)时,火球朝着玩家90度角(朝向面对坏蛋的玩家向左)

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): 我尝试了以下方法(其中一些可能不是必需的):

  • re-import all assets 重新导入所有资产
  • create new project on a new machine to reproduce the issue. 在新机器上创建新项目以重现该问题。
  • I've tried setting the lookat variable to be: 我尝试将lookat变量设置为:
    • player transform 球员转型
    • the vector 3 world coordinate of the player (this is what is in the code below) 玩家的向量3世界坐标(这就是下面的代码)
    • 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM