最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Billboarding in Unity 2D - Stack Overflow

programmeradmin1浏览0评论

I wanted to make it so a sprite always looks at player but I can't figure out what I am doing wrong.I tried making the player controller as an instance and then using transform to change the vector3 but it isn't seem to be working.

This is the code for the player controller:

     using System.Net.Http.Headers;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{

    public static PlayerController instance;
    public Rigidbody2D theRB;

    public float moveSpeed =5f;

    private Vector2 moveInput;

    private Vector2 mouseInput;

    public float mouseSensitivity = 4f; 

    public Camera viewCam;

    public GameObject bulletImpact;

    public int currnetAmmo;

    void Awake()
    {
        instance = this;
    }
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        // player movement
        moveInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));

        Vector3 moveHorizontal = transform.up * moveInput.y;

        Vector3 moveVertical = transform.right * moveInput.x;

        theRB.linearVelocity = (moveHorizontal + moveVertical) * moveSpeed;



        //player view control

        mouseInput = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * mouseSensitivity;

        transform.rotation *= Quaternion.Euler(0, 0, -mouseInput.x);
        
        viewCam.transform.localRotation *= Quaternion.Euler(-mouseInput.y, 0f, 0f);

        //shooting

        if(Input.GetMouseButtonDown(0)){

            if(currnetAmmo > 0)
            {                
                Ray ray = viewCam.ViewportPointToRay(new Vector3(.5f, .5f, 0f));
                RaycastHit hit;
                if(Physics.Raycast(ray, out hit)){
                    
                    //Debug.Log("Its a hit at " + hit.transform.name);
                    Instantiate(bulletImpact, hit.point, transform.rotation);

                } else{
                    
                    Debug.Log("I am looking at none");
                }
                currnetAmmo--;
            }
        }

    }
}

and the following the code for the Billboard

using UnityEngine;

public class Billoard : MonoBehaviour
{

    private SpriteRenderer theSR;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        theSR = GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        transform.LookAt(PlayerController.instance.transform.position, -Vector3.right);
    }
}

I wanted to make it so a sprite always looks at player but I can't figure out what I am doing wrong.I tried making the player controller as an instance and then using transform to change the vector3 but it isn't seem to be working.

This is the code for the player controller:

     using System.Net.Http.Headers;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour
{

    public static PlayerController instance;
    public Rigidbody2D theRB;

    public float moveSpeed =5f;

    private Vector2 moveInput;

    private Vector2 mouseInput;

    public float mouseSensitivity = 4f; 

    public Camera viewCam;

    public GameObject bulletImpact;

    public int currnetAmmo;

    void Awake()
    {
        instance = this;
    }
    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        // player movement
        moveInput = new Vector2(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"));

        Vector3 moveHorizontal = transform.up * moveInput.y;

        Vector3 moveVertical = transform.right * moveInput.x;

        theRB.linearVelocity = (moveHorizontal + moveVertical) * moveSpeed;



        //player view control

        mouseInput = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * mouseSensitivity;

        transform.rotation *= Quaternion.Euler(0, 0, -mouseInput.x);
        
        viewCam.transform.localRotation *= Quaternion.Euler(-mouseInput.y, 0f, 0f);

        //shooting

        if(Input.GetMouseButtonDown(0)){

            if(currnetAmmo > 0)
            {                
                Ray ray = viewCam.ViewportPointToRay(new Vector3(.5f, .5f, 0f));
                RaycastHit hit;
                if(Physics.Raycast(ray, out hit)){
                    
                    //Debug.Log("Its a hit at " + hit.transform.name);
                    Instantiate(bulletImpact, hit.point, transform.rotation);

                } else{
                    
                    Debug.Log("I am looking at none");
                }
                currnetAmmo--;
            }
        }

    }
}

and the following the code for the Billboard

using UnityEngine;

public class Billoard : MonoBehaviour
{

    private SpriteRenderer theSR;

    // Start is called once before the first execution of Update after the MonoBehaviour is created
    void Start()
    {
        theSR = GetComponent<SpriteRenderer>();
    }

    // Update is called once per frame
    void Update()
    {
        transform.LookAt(PlayerController.instance.transform.position, -Vector3.right);
    }
}
Share Improve this question asked Mar 3 at 11:56 Guard PlayzGuard Playz 154 bronze badges 1
  • 4 When you say "its not working" what do you mean? Is not following the player? Its not moving at all? In cases like this you may want that the billboard follow the camera, not the player usually. Also you may debug what the PlayerController.instance.transform.position is giving you inside the Billboard class – TSDrake Commented Mar 3 at 12:31
Add a comment  | 

1 Answer 1

Reset to default 1

Change "transform.LookAt(PlayerController.instance.transform.position, -Vector3.right);" to "transform.LookAt(PlayerController.instance.viewCam.transform.position, -Vector3.right);" so that the object looks at the camera instead of the player. Also, you should use LateUpdate for the billboard code, not Update, since if you use Update it will be consistently one frame behind. Also make sure you remember to drag and drop the billboard script onto the gameobject that you want to billboard the player before you enter playmode.

发布评论

评论列表(0)

  1. 暂无评论