自上而下的射手子弹根本不准确

时间:2018-12-22 22:33:15

标签: unity3d 2d topdown

我通过Youtube,Google和以前的答案进行了检查,但无法使我的游戏朝着鼠标射击。玩家旋转以跟随鼠标,当他射击时,它要么不准确,要么有时朝另一个方向射击。我真的不想发送大量的代码,但是我觉得我混合了不同教程中的代码,尽管我有点组织起来,但最好还是让您看一下,所以对不起,先谢谢您

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public int playerId = 0;
    public bool useController;

    public float Curserspeed = 5f;
    public Animator animator;
    public float speed;
    public GameObject crossHair;

    public GameObject bulletPrefab;

    private Vector2 moveVelocity;

    Vector3 movement;
    Vector3 aim;
    bool isAiming;
    bool endOfAiming;

    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        ProcessInputs();
        AimAndShoot();
        Animate();
        Move();

        Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
        Quaternion rotation = Quaternion.AngleAxis(angle, Vector3.forward);
        transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Curserspeed* Time.deltaTime);
    }

    private void ProcessInputs() {
        if (useController) {
            movement = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"), 0.0f);
            aim = new Vector3(Input.GetAxisRaw("MoveHorizontal"), Input.GetAxisRaw("MoveVertical"), 0.0f);
            aim.Normalize();
            isAiming = Input.GetButton("Fire1");
            endOfAiming = Input.GetButtonUp("fire1");
        }
        else {
            movement = new Vector3(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"), 0.0f);
            Vector3 mouseMovement = new Vector3(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"), 0.0f);
            aim = aim + mouseMovement;
            if (aim.magnitude > 0f)
            {
                aim.Normalize();
            }
            isAiming = Input.GetButton("Fire1");
            endOfAiming = Input.GetButtonUp("Fire1");
        }

        if (movement.magnitude > 1.0f)
        {
            movement.Normalize();
        }
    }
    private void Move()
    {
        transform.position = transform.position + movement * speed * Time.deltaTime;
    }
    private void Animate()
    {

        animator.SetFloat("vmovement", Input.GetAxisRaw("Horizontal"));
        animator.SetFloat("movement", Input.GetAxisRaw("Vertical"));
        animator.SetFloat("magnitude", movement.magnitude);
    }

    private void AimAndShoot()
    {
        Vector2 shootingDirection = new Vector2(aim.x, aim.y);
        if (aim.magnitude > 0.0f)

            shootingDirection.Normalize();
            if (Input.GetButtonDown("Fire1")) {
                GameObject bullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity);

                bullet.GetComponent<Rigidbody2D>().velocity = shootingDirection * 3.0f;
                bullet.transform.Rotate(0.0f, 0.0f, Mathf.Atan2(shootingDirection.y, shootingDirection.x) * Mathf.Rad2Deg);
                Destroy(bullet, 3.0f);
            }
int main()
{
    return 0;
}

`

1 个答案:

答案 0 :(得分:0)

如果您的玩家正在看着鼠标,而您正试图在那儿射击,这似乎有点复杂...

实例化项目符号后,只需要做两件事:   立即旋转它,使其面对与播放器相同的方向。      (这可以通过transform.rotation = playerobject.rotation来完成)   向前施加力量。      (由于该物体面向您的玩家所面对的位置,因此在“发射”该弹丸时瞄准,因此您无需瞄准,只需将其沿前进轨迹发送即可)

transform.forward将在这里工作。