【问题标题】:Top down shooter bullet not accurate at all自上而下的射击子弹根本不准确
【发布时间】:2019-05-22 19:28:11
【问题描述】:

我查看了 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;
}

`

【问题讨论】:

  • 为什么你使用鼠标运动来计算你的瞄准点,而不仅仅是原始的鼠标 X 和鼠标 Y 位置? Input.mousePosition
  • 感谢您的帮助和快速回答。尝试了所有内容,不确定我是否正确编辑了它,但无法使其正常工作,不确定在哪里进行更改以及所有需要更改的内容很难记住所有内容的含义我喜欢全新的,所以很抱歉,但感谢您的帮助

标签: unity3d 2d topdown


【解决方案1】:

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

实例化子弹后,您只需要做两件事: 立即旋转它,使其面向与玩家相同的方向。 (这可以通过 transform.rotation= playerobject.rotation 来完成) 施加向前的力量。 (因为物体正对着你的玩家面对的地方,因此当你“发射”弹丸时瞄准,你不需要瞄准,只需将它发送到它的前进轨迹上)

transform.forward 将在这里工作。

【讨论】:

  • 感谢您的回答,因为我很痛苦,我没有成功实施您建议的更改,无论我做错与否我不知道:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多