unity2D-发射箭矢

先制作箭矢预制体

将Rigidbody2D改为:

Arrow.cs:

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

public class Arrow : MonoBehaviour
{
    public float speed;//速度
    public int damage;//伤害

    private Rigidbody2D rb2d;
    //private Vector3 startPos;

    // Start is called before the first frame update
    void Start()
    {
        speed = 50f;
        rb2d = GetComponent<Rigidbody2D>();
        rb2d.velocity = transform.right * speed;//transform.right代表方向即x轴
        //startPos = transform.position;

        transform.localScale = new Vector2(-1, 1);
    }

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

    private void FixedUpdate()
    {
        
    }
}

将箭矢的预制体拖入代码PlayerShoot.cs的ArrowPrefab(GameObject类型)当中,

PlayerShoot.cs:

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

public class PlayerShoot : MonoBehaviour
{
    public Animator anim;
    public GameObject arrowPrefab;

    bool shootPressed;
    Vector3 arrowPos = new Vector3(0.11f, -0.38f, 0f);//箭矢发射的位置
    Quaternion arrowRotation = Quaternion.identity;//箭矢朝向

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.J))
        {
            shootPressed = true;
        }
    }

    private void FixedUpdate()
    {
        Shoot();
        SwitchAnim();
    }

    void Shoot()
    {
        if(shootPressed == true)
        {
            anim.SetBool("Shooting", true);

            float faceDirection = transform.localScale.x;//角色朝向
            

            if (faceDirection > 0)//角色朝左
            {
                arrowRotation = Quaternion.Euler(0.0f, 180.0f, 0.0f);
                Instantiate(arrowPrefab, transform.position + arrowPos, arrowRotation);
            }
            else//角色朝右
            {
                arrowRotation = Quaternion.Euler(0.0f, 0.0f, 0.0f);
                Instantiate(arrowPrefab, transform.position + arrowPos, arrowRotation);
            }
            
            shootPressed = false;
        }
    }

    void SwitchAnim()
    {
        
    }
}

效果如图:

(动作动画尚未完善)

-----------------------------------------------------------------------------------------------------

完善射箭动作:Unity使用动画关键帧事件完成射箭(攻击)时的射箭(出招)时机及完成攻击间隔冷却

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity Mega-Fiers是一款强大的Unity插件,它为开者提供了许多功能和工具,以帮助他们创建优秀的游戏体验。 首先,Unity Mega-Fiers具有强大的形变功能。开者可以通过该插件实现各种形状的变换,包括弯曲、挤压、拉伸等。这为游戏中的人物、物体和环境提供了更多的自由度和个性化选项,使其更具动态和真实感。 其次,Unity Mega-Fiers还提供了高度的粒子系统控制能力。开者可以使用该插件来创建更加逼真的粒子效果,并具有更准确的控制。这包括粒子的大小、颜色、速度等方面,使游戏中的特效更加出色。 另外,Unity Mega-Fiers还支持可编程网格。这意味着开者可以通过脚本来控制网格的生成和变形,从而实现各种复杂的效果。无论是地形生成、水体模拟还是其他物体的变形,都可以通过该插件实现,并使游戏更加逼真和具有个性化。 除了以上功能,Unity Mega-Fiers还提供了其他诸多辅助工具,如动画控制、特效编辑等,为开者提供了更丰富的开选项和提升游戏质量的手段。 总结而言,Unity Mega-Fiers是一款功能强大的Unity插件,为开者提供了丰富的形变、粒子系统控制和可编程网格等功能。它能够大幅提升游戏的真实感和品质,并为开者提供更多的创作空间和个性化选项。无论是初学者还是有经验的开者,都能够从中受益,并创造出出色的游戏作品。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值