【问题标题】:Fire projectile at targets coordinates在目标坐标处发射弹丸
【发布时间】:2019-05-12 20:21:22
【问题描述】:

目标:

我正在创建一个冰球射手,射手每 3 秒生成一个冰球并向球门射门区域内的目标点开火;射门区域是横跨球门表面的平面,作为射门区域,因为并非所有射门都会在网上 - 冰球射手在该射门区域内生成一个随机点,这就是它获得目标 X、Y 和 Z 的方式.

我查看了移动对象的不同方式,例如 transform.forward、transform.up 等,但我认为我正在寻找的是 puckRB.MovePosition?

据我了解,将“Is Kinematic”设置为 true 时,MovePosition 方法会在冰球在环境中移动时将物理添加到冰球中,否则它只会传送它。我只是不确定如何将目标坐标与它需要的其他东西一起放入其中?

void GetTarget()
{
    float scale = 1f;

    // Grabs the aiming plane
    Transform aimingPlane = GameObject.Find("AimingPlane").gameObject.transform.GetChild(1);

    // Gets the moving X & Y area of the plane
    float moveAreaX = aimingPlane.GetComponent<Renderer>().bounds.size.x / 2;
    float moveAreaY = aimingPlane.GetComponent<Renderer>().bounds.size.y / 2;

    // Generates a random value within the move area (X, Y)
    float ranX = Random.Range(-moveAreaX * scale, moveAreaX * scale);
    float ranY = Random.Range(-moveAreaY * scale, moveAreaY * scale);

    // Grabs the center of the AimingPlane
    Vector3 center = aimingPlane.GetComponent<Renderer>().bounds.center;

    // Gets the targets coordinates (X and Y)
    targetCoordsX = ranX + center.x;
    targetCoordsY = ranY + center.y;

    // Grabs AimingDot and places at target coordinates
    Transform aimingDot = GameObject.Find("AimingPlane").gameObject.transform.GetChild(0);
    aimingDot.position = new Vector3(targetCoordsX, targetCoordsY, center.z);
}


void FirePuck()
{
    GameObject puckCopy = Instantiate(puck, shotPos.transform.position, shotPos.transform.rotation) as GameObject;

    puckRB = puckCopy.GetComponent<Rigidbody>();

    Vector3 test = new Vector3(targetCoordsX, targetCoordsY);
    puckRB.MovePosition(test * Time.deltaTime);
}

总而言之,到目前为止,程序在射击区域内生成了一个随机目标,每 3 秒产生一个新的冰球,我现在只是试图从射手那里得到一个冰球到那个目标;冰球目前所做的只是传送到射手面前的一个点。

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    Rigidbody.MovePosition 所做的只是瞬间将身体移动到新位置(传送)。

    刚体用于在游戏对象上模拟物理。启用IsKinematic 会禁用冰球的物理功能,允许其他物体检测到它们何时与冰球碰撞并从冰球反弹,但使冰球不受碰撞影响。

    你想要的是禁用 IsKinematic,并设置冰球速度(速度):

    puckRB.velocity = (destinationPos - currentPosition).normalized * speed;
    

    从目标位置减去球的当前位置,得到方向向量。 .normalized * speed 会将其转换为单位向量(大小为 1 的向量,并将其乘以所需的行驶速度)。

    public float puckSpeed = 1f;
    
    void FirePuck()
    {
        GameObject puckCopy = Instantiate(puck, shotPos.transform.position, shotPos.transform.rotation) as GameObject;
    
        puckRB = puckCopy.GetComponent<Rigidbody>();
    
        puckRB.velocity = (new Vector3(targetCoordsX, targetCoordsY, targetCoordsZ) - puckRB.transform.position).normalized * puckSpeed;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多