简体   繁体   中英

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. 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. It is not 90 or 180 off. 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.

实际问题

Assuming the baddie is at (0,0,0):

  • when the player is at (0,0,10) the fireballs head directly towards the player
  • 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))
  • 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)
  • 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)

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:
    • player transform
    • the vector 3 world coordinate of the player (this is what is in the code below)
    • 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.

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 . Its secondary parameter is a Space argument that defaults to 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 :

// 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 :

// Vector3.forward is (0,0,1). In "self space" this is "forward"
transform.Translate(Vector3.forward * Time.deltaTime * moveSpeed); // Space.Self

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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