【问题标题】:Unity - problem with arms rotation and flip, body flip and shotting - 2d shooterUnity - 手臂旋转和翻转、身体翻转和射击的问题 - 2d 射手
【发布时间】:2019-06-08 14:30:00
【问题描述】:

我从一个小型射击游戏开始,但我的角色有问题。手臂必须旋转 360º,但身体只能向右或向左旋转(取决于鼠标旋转手臂的位置)。

到目前为止,我得到的就是您在下面的视频中看到的,但我有两个大问题,并且在教程的帮助下。

  • 我可以旋转和翻转我的手臂,但不能旋转身体。

  • 此外,当它向右射击时,子弹正确地从我创建的火点射出,但在手臂向左翻转后,子弹(和武器射击)不再对齐。

我尝试过的这种方法不是解决这个问题的最佳方法吗?

感谢您的帮助。

游戏链接:https://vimeo.com/310853740

这是我的手臂旋转脚本:

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

public class ArmRotation : MonoBehaviour
{
    SpriteRenderer spriteRend;

    void Awake()
    {
        spriteRend = GetComponent<SpriteRenderer>();
    }

    void Update()
    {
        AimArmAtMouse();
    }

    void AimArmAtMouse()
    {
        Vector2 mousePosition = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);  
        Vector2 armToMouse = mousePosition - (Vector2)transform.position;
        float rotationZ = Vector2.SignedAngle(transform.right, armToMouse);  
        transform.Rotate(0f, 0f, rotationZ);  
        FlipArm(Vector2.SignedAngle(transform.right, Vector2.right));
    }

    void FlipArm(float rotation)
    {
        if (rotation < -90f || rotation > 90f)
        {
            spriteRend.flipY = true;
        }
        else
        {
            spriteRend.flipY = false;
        }
    }
}

【问题讨论】:

    标签: unity3d 2d flip


    【解决方案1】:

    这是因为你在翻转精灵时没有翻转火点。 我重写了你的脚本来包含对火点的引用。我还添加了一个由您的“FlipArm”函数调用的“FlipFirePoint”函数。它应该可以解决您的对齐问题。

    using UnityEngine;
    
    public class ArmRotation : MonoBehaviour
    {
    SpriteRenderer spriteRend;
    public Transform firePoint;
    
    void Awake()
    {
        spriteRend = GetComponent<SpriteRenderer>();
    }
    
    void Update()
    {
        AimArmAtMouse();
    }
    
    void AimArmAtMouse()
    {
        Vector2 mousePosition = (Vector2)Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2 armToMouse = mousePosition - (Vector2)transform.position;
        float rotationZ = Vector2.SignedAngle(transform.right, armToMouse);
        transform.Rotate(0f, 0f, rotationZ);
        FlipArm(Vector2.SignedAngle(transform.right, Vector2.right));
    }
    
    void FlipArm(float rotation)
    {
        if (rotation < -90f || rotation > 90f)
        {
            spriteRend.flipY = true;
            FlipFirePoint(true);
        }
        else
        {
            spriteRend.flipY = false;
            FlipFirePoint(false);
        }
    }
    
    void FlipFirePoint(bool flip)
    {
        var pos = firePoint.localPosition;
        pos.x = Mathf.Abs(pos.x) * (flip ? -1 : 1);
        firePoint.localPosition = pos;
    }
    
    }
    

    【讨论】:

    • 谢谢,@Sean。我用你的修改应用了代码,现在我得到了这个:ibb.co/42mnSQ7。它在翻转,但不在枪口附近。我错过了什么??
    • @tigrev 在 FlipFirePoint 函数中,将 pos.x = Mathf.Abs(pos.x) * (flip ? -1 : 1); 替换为 pos.y = Mathf.Abs(pos.y) * (flip ? -1 : 1);
    • 是的,@Sean。而已!现在它工作得很好。 :) 再次感谢你!你认为我可以用同样的方法让主体翻转到另一边,当手臂翻转时,不用指向和旋转鼠标?
    • @tigrev 我很高兴它的工作:) 如果手臂是主体的孩子,那么你可以访问其父母的精灵组件并翻转它的 y 值。让我知道这是否有效,如果有效,请点击“接受”:)
    • @Sean...现在我让身体旋转和翻转,而不是只在手臂转到另一侧时翻转。也许主体不能是 arm 的父级并且有单独的代码?看这里:im.ezgif.com/tmp/ezgif-1-480284950a10.gif void FlipMainBody(bool flip) { var pos = mainBody.localPosition; pos.y = Mathf.Abs(pos.y) * (翻转 ? -1 : 1); mainBody.localPosition = 位置; } }
    【解决方案2】:

    @Sean,我将 main_body 与手臂分开,并为身体旋转制作了一个新脚本,但现在发生在我身上:

    My test char

    代码:

    void Update()
    {
        Flip();
    }
    
    void Flip() 
    {
        Vector3 theScale = transform.localScale;
        Vector3 pos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
        float WorldXPos = Camera.main.ScreenToWorldPoint(pos).x; 
    
        if (WorldXPos > gameObject.transform.position.x)
        {
            theScale.x = 1;
            transform.localScale = theScale;
        }
        else
        {
            theScale.x = -1;
            transform.localScale = theScale;
        }
    }}
    

    快到了,但我还不需要?

    【讨论】:

    • 问题解决了!!我调整了玩家和手臂精灵的原点,使它们适合,现在都可以正常工作。 :)
    猜你喜欢
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-27
    相关资源
    最近更新 更多