将GameObject附加到枪管上

问题描述 投票:-1回答:1

我目前正在开发2D射击游戏。我的问题是子弹应该从那里退出的GameObject。当我翻转精灵时,GameObject不会翻转它,即使它是我的武器的子代。红色箭头是子弹的轨迹。

参见下图:enter image description here

enter image description here

[当我转动角色时,我会翻转手以使其看起来不那么晃动。我为此使用的代码很简单:

void Flip()
{
    facingRight = !facingRight; //Works as a toggle
    GetComponent<SpriteRenderer>().flipX = !facingRight; //Fliping my character
    child = GameObject.Find("Hand"); //Finds the hand sprite
    m4a1_child = GameObject.Find("M4A1_on_hand");  //Find wep sprite
    child.GetComponent<SpriteRenderer>().flipY = !facingRight;  //flips hand
    m4a1_child.GetComponent<SpriteRenderer>().flipY = !facingRight;  //flips weapon
}

我如何将游戏对象“粘”在我的枪管上?

用于移动手+武器的代码是:

    void Update()
{
    Vector3 cursorPos =
    Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0));
    Vector3 playerPos = Player.transform.position;

    Vector3 playerToCursor = cursorPos - playerPos;
    Vector3 dir = playerToCursor.normalized;
    Vector3 cursorVector = dir * radius;

    if (playerToCursor.magnitude < cursorVector.magnitude) //detect if mouse is in inner radius
        cursorVector = playerToCursor;

    transform.position = playerPos + cursorVector;

    float angle = Mathf.Atan2(playerToCursor.y, playerToCursor.x) * Mathf.Rad2Deg;
    transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

}
c# unity3d
1个回答
0
投票

实际上,由于您可能要根据枪支子弹的方向来进行计算,因此最好不要取其比例尺。因此,最好忽略我写的第一条评论。

© www.soinside.com 2019 - 2024. All rights reserved.