【问题标题】:Bullet rotation unwanted offset Unity子弹旋转不需要的偏移 Unity
【发布时间】:2021-02-15 18:47:48
【问题描述】:

我试图让一个敌人包围玩家并向该玩家发射子弹。但是,每当敌人射击时,子弹就会稍微远离玩家,并继续射击距离玩家位置更远的地方。

这是敌人的行为

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

public class CirclingScript : MonoBehaviour, IPooledObject
{

public Transform tf;
public Transform Target;

public GameObject Ship;
public GameObject WaveMaker;
public EnemyScript enemyscript;

public string BulletName;

ObjectPools objectPooler;

private int I; // Wave number
public int BulletAmount;

public float angle;
public float Radius;
public float RotateSpeed;
private float Timer;
public float Health;

private Quaternion BulletRot;

public void OnObjectSpawn()
{

    WaveMaker = GameObject.Find("EnemyWaveMaker");//gets the game object
    enemyscript = WaveMaker.GetComponent<EnemyScript>();// gets the scripts for the wave makers
    I = enemyscript.i;

    Ship = GameObject.Find("Ship");//gets the game object
    Target = Ship.GetComponent<Transform>();// gets the Transform 

    //https://answers.unity.com/questions/1068513/place-8-objects-around-a-target-gameobject.html thank you ^-^
    angle = enemyscript.RotSpace * Mathf.PI * 2f / enemyscript.Waves[I].Amount;

    RotateSpeed = enemyscript.Waves[I].RotateSpeed;
    Radius = enemyscript.Waves[I].Radius;

}

void Start()
{

    objectPooler = ObjectPools.Instance;

}

void OnTriggerEnter2D(Collider2D coll)
{
    if (coll.gameObject.CompareTag("Bullet"))
    {
        Health -= coll.GetComponent<DamageScript>().Damage;

    }

    if (Health <= 0)
    {

        objectPooler.SpawnFromPool("Boom", tf.position, Quaternion.identity);
        gameObject.SetActive(false);

    }
}

// Update is called once per frame
void Update()
{

    angle += RotateSpeed * Time.deltaTime;

    Timer += Time.deltaTime;

    Vector2 offset = new Vector2(Mathf.Sin(angle), Mathf.Cos(angle)) * Radius;
    tf.position = (Vector2)Target.position + offset;

    if (Timer >= 2f)
    {
        Vector3 difference = Target.position - tf.position;
        float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
        BulletPatternsModule.ShootArc(angle, BulletAmount, BulletName, tf, rotationZ);
        Timer = 0f;
    }
}
}

以及发射子弹的代码

    public static void ShootArc(float ArcSize, int BulletAmount, string BulletName, Transform tf, float Offset)//All arcs are in angles, not radians
{
    float angle = 0;
    angle = Offset;//Offset is to the left
    for (int i = 0; i < BulletAmount; i++)
    {
        float AngleStep = ArcSize / BulletAmount;//Gets the step size for arc
        angle += AngleStep;
        objectPooler.SpawnFromPool(BulletName, tf.position, Quaternion.Euler(0, 0, angle));//Shoots the bullet
    }
}

偏移使附近的敌人无用

任何帮助将不胜感激:) (更多文字以便我发布问题。这不是很有趣吗?)

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    使用此功能更容易让敌人看向玩家

    transform.LookAt(target.position);
    

    【讨论】:

    • 是子弹的旋转不正确。我可以理解你来自哪里
    • 所以你的子弹是一个物理体?您是否尝试过首先将子弹速度设置为 Vector3(0, 0, 0);然后给它一个前进的力量?
    • 子弹使用翻译进行移动。问题在于旋转的代码。子弹工作正常
    • 子弹来自不是敌人中心的地方吗?对此有一个快速而肮脏的解决方案。生成子弹后,只需将其朝向玩家旋转即可。
    • 我可以设置子弹的位置。它使用一般的子弹类,所以快速旋转并不是最好的。子弹还必须处理以潜在弧线产生的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-17
    • 1970-01-01
    • 2011-12-20
    • 2014-01-24
    • 2015-05-06
    • 1970-01-01
    相关资源
    最近更新 更多