Unity自学记录

本文记录了使用Unity进行2D游戏开发的过程,重点在于角色运动的实现。作者通过跟随B站M_studio的教程,学习并实践了Unity2D的基础操作,包括角色的左右移动、方向切换和跳跃功能。代码中展示了如何利用Rigidbody2D组件和Input.GetAxis方法控制角色移动,并响应跳跃按钮。
摘要由CSDN通过智能技术生成

系列文章目录

第一章 角色运动的实现


前言

准备学习unity。未来可能工作用或者。。。不一定。
从B站M_studio博主的视频,开始跟着做学习。目前做到第五集弹跳部分,可以实现。


一、Unity 2D游戏入门学习?

直接从应用商店安装了unity,然后配了编辑器,跟着视频开始尝试,素材来自unity官方。

二、实操过程

1.代码

代码如下(示例):

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

public class playercontroller : MonoBehaviour
{

    public Rigidbody2D rb;
    public float speed;
    public float jumpforce;

    // Start is called before the first frame update
    void Start()
    {
        
    }

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

        Movement();

    }


    void Movement() 
    {
        float Horizontalmove = Input.GetAxis("Horizontal");
        float facedirection = Input.GetAxisRaw("Horizontal");
       
        if (Horizontalmove != 0)
        {

            rb.velocity = new Vector2(Horizontalmove * Time.deltaTime, rb.velocity.y);
        
        }

        if (facedirection != 0)
        {
            transform.localScale = new Vector3(facedirection, 1, 1);
        }

        if (Input.GetButtonDown("Jump"))
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpforce * Time.deltaTime);        }
    }
}


总结

`
主要定义角色和背景,只贴了代码和结果视频,在unity里面有很多操作。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值