Projectiles spawning with incorrect trajectory

笑着哭i 提交于 2021-02-11 15:30:32

问题


I wrote a script for gun and projectile but whenever I fire it only fires in the right direction when im pointing upwards, if i'm pointing to the sides it shoots downwards and if i'm pointing downwards it shoots up.

I've been trying to get a simple script that makes the gun rotate to face the mouse (works) and then to spawn bullets going in the firection of the gun, however, they spawn weirdly when not facing upwards, i've tried changing the object that has the script but nothing else since i'm not exactly sure what the error is.

Gun script:

using UnityEngine;
using System.Collections;

public class Gun : MonoBehaviour
{

    public float offset;

    public GameObject projectile;
    public Transform shotPoint;

    private float timeBtwShots;
    public float startTimeBtwShots;

    private void Update()
    {
        Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);

        if (timeBtwShots <= 0)
        {
            if (Input.GetMouseButtonDown(0))
            {
                Instantiate(projectile, shotPoint.position, transform.rotation);
                timeBtwShots = startTimeBtwShots;

            }
        }
        else
        {
            timeBtwShots -= Time.deltaTime;
        }

    }

}

Projectile script:

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

public class Projectile : MonoBehaviour
{
    public float pSpeed;

    public float lifeTime;

    public GameObject destroyEffect;

    private void Start()
    {
        Invoke("DestroyProjectile", lifeTime);
    }

    private void Update()
    {
        transform.Translate(transform.up * pSpeed * Time.deltaTime);
    }

    void DestroyProjectile()
    {

        Destroy(gameObject);
    }
}

I know that in the projectile script i change the transform.up inside the void update to transform.right or left the effect reverses and oly shoots correctly in the direction that I typed but I have no idead how to make it shoot properly in all directions.


回答1:


I would hold a reference to the direction fired in the projectile and then move each frame based on that direction. So after we Instantiate the projectile, we calculate the direction Vector based on the gun/players rotation. The following code should do exactly what you are looking for, but is untested as I am currently using my phone! Hope this helps!

Inside Projectile.

public Vector3 directionFired;

Inside Gun.

GameObject proj = Instantiate(projectile, shotPoint.position, transform.rotation);
proj.directionFired = Quaternion.Euler(transform.rotation) * Vector3.forward;
timeBtwShots = startTimeBtwShots;

Inside Projectile Update

transform.Translate(directionFired * pSpeed * Time.deltaTime)


来源:https://stackoverflow.com/questions/58583373/projectiles-spawning-with-incorrect-trajectory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!