unity初步学习开发日志(二)

#’仅以此文记录我学习unity中的问题和进程#

#学习案例一:基于unity——Ruby案例中的修改和扩展#

记录时间“2024/3/20”

一.今日问题:暂无疑难杂症

二.今日完成

1.完成了子弹的自动发射即转向

2.添加了一段提示子弹发射的提示ui

3.制作了一段简单的入场动画

三.今日学习

(1)学习了有关组建UnityEngine.InputSystem的初步使用

关键句:1.mousePos = cam.ScreenToWorldPoint(Mouse.current.position.ReadValue());

(意为将屏幕中鼠标的位置转化威尔游戏世界中的空间位置。)

关键词:1.Mathf.Atan2:作为一种计算方法用于计算在一点与坐标轴中心x轴的夹角。

2.Mathf.Rad2Deg:从角度到弧度转变的常量值

(2)学习了最为另一个简单的2d射击方式——鼠标射击

鼠标代码设置如下

public class MouseShot : MonoBehaviour
{
    public GameObject bullet;     //   子弹
    public Transform shotbeging;  //      枪口子对象

    public Camera cam;          //      挂载摄像机
    private Vector3 mousePos;   //        鼠标位置
    private Vector2 weapenDirection;     //     枪口朝向

    public AudioSource audioSource;    //音效播放器
    public AudioClip attackSoundClip;   //音乐唱片


    public float rechargetime;
    private float Rechargetime;//换弹时间
    public bool open_shot;      //武器开关
    public bool isInbvincible;  // 用于倒计时的开关
    // Start is called before the first frame update
    void Start()
    {
        Rechargetime = rechargetime;
        isInbvincible = true;
    }

    // Update is called once per frame
    void Update()
    {
        mousePos = cam.ScreenToWorldPoint(Mouse.current.position.ReadValue());   //  让获得当前屏幕内鼠标的位置
        weapenDirection = (mousePos - transform.position).normalized;            //   计算鼠标和武器只见的距离向量
        float angle = Mathf.Atan2(weapenDirection.y, weapenDirection.x) * Mathf.Rad2Deg;      //   由xy轴来计算武器的变化度数
        transform.eulerAngles = new Vector3(0, 0, angle);          //         赋值



        if (Mouse.current.leftButton.wasPressedThisFrame)
        {
            if (!UIhealthbar.instance.hasTask)
            {
                return;
            }
            open_shot = !open_shot;                               
        }
        if (open_shot == true)
        {
            autoshot();
        }
        if (angle <= 90 && angle >= -90)
        {
            transform.eulerAngles = new Vector3(0, 0, angle);
        }
        else if (angle > 90 || angle < -90)
        {
            transform.eulerAngles = new Vector3(-180, 0, -angle);
        }
        

    }
    public void PlaySound(AudioClip audioClip)
    {
        audioSource.PlayOneShot(audioClip);

    }

    private void autoshot()
    {

                Rechargetime = Rechargetime-Time.deltaTime;
               if (Rechargetime <= 0)
                {
                PlaySound(attackSoundClip);
                Instantiate(bullet, shotbeging.position, Quaternion.Euler(transform.eulerAngles));
                Rechargetime = rechargetime;

                
                }      
    }
}

子弹代码设计如下

public class Projectile : MonoBehaviour
{
    // Start is called before the first frame update

    private Rigidbody2D rigidbody2d;

    void Awake()
    {

        rigidbody2d = GetComponent<Rigidbody2D>();
    }


    private void Update()
       {
            
        if (transform.position.magnitude > 100)
            {
                Destroy(gameObject);
            }
        }

        // Update is called once per frame
    public void Launch(Vector2 direction, float force)
        {
            rigidbody2d.AddForce(direction * force);
        }
    private void OnCollisionEnter2D(Collision2D collision)
        {
            Enamycontroller enamycontroller = collision.gameObject.GetComponent<Enamycontroller>();
            if (enamycontroller != null)
            {
                enamycontroller.Fix();
            }
            Destroy(gameObject);
        }

    
}

关键点:子弹一直都是朝着自身位置的右侧以恒定速度发出,在生成为武器的子物体之后,随着枪口盒(父类:武器——子类:枪口)的旋转变化设定了子弹发出的旋转角度。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值