Ruby‘s Adventrue游戏制作笔记(二)Unity控制ruby移动

17 篇文章 0 订阅
17 篇文章 1 订阅
本文是Unity游戏开发系列的第二篇,主要介绍了如何在Unity中创建PlayerController脚本,通过键盘控制Ruby角色进行上下左右移动。通过设置Input.GetAxisRaw获取用户输入,结合Time.deltaTime调整平滑移动。系列教程涵盖了从项目创建到游戏元素、碰撞检测、动画等多个方面。
摘要由CSDN通过智能技术生成


前言

本文章是我学习Unity官方项目项目所做笔记,作为学习Unity的游戏笔记,在最后一章会发出源码,如果等不及可以直接看源码,里面也有很多注释相关,话不多说,让Ruby动起来!
游戏引擎:Unity2020.3

一、将Ruby拖入MainScene中

在这里插入图片描述
在这里插入图片描述

二、 创建player脚本

在这里插入图片描述

三、 开始编辑

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

public class NewBehaviourScript : MonoBehaviour
{

    // 玩家速度
    public float speed = 5f;

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

    // Update is called once per frame
    void Update()
    {
        // 测试
        transform.Translate(transform.right * speed * Time.deltaTime);
    }
}

此时ruby 向屏幕右边移动

在这里插入图片描述

四、通过上下左右键实现移动

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

public class PlayerController : MonoBehaviour
{

    // 玩家速度
    public float speed = 5f;

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

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

        float moveX = Input.GetAxisRaw("Horizontal");  // 控制水平移动方向 按下A时返回 -1,按下D时返回 1
        float moveY = Input.GetAxisRaw("Vertical"); // 控制垂直方向 W:1 S:-1 

        // 创建position坐标,存储玩家输入的坐标
        Vector2 position = transform.position;
        position.x += moveX * speed * Time.deltaTime;
        position.y += moveY * speed * Time.deltaTime;

        // 将最终坐标赋值给玩家
        transform.position = position;

    }
}

系列链接

Ruby‘s Adventrue游戏制作笔记(一)Unity创建项目

Ruby‘s Adventrue游戏制作笔记(二)Unity控制ruby移动

Ruby‘s Adventrue游戏制作笔记(三)Unity使用tilemap绘制场景

Ruby‘s Adventrue游戏制作笔记(四)Unity绘制其他元素

Ruby‘s Adventrue游戏制作笔记(五)Unity解决碰撞抖动和旋转问题

Ruby‘s Adventrue游戏制作笔记(六)Unity相机跟随玩家移动

Ruby‘s Adventrue游戏制作笔记(七)Unity采集生命道具

Ruby‘s Adventrue游戏制作笔记(八)Unity伤害陷阱

Ruby‘s Adventrue游戏制作笔记(九)Unity添加敌人

Ruby‘s Adventrue游戏制作笔记(十)Unity添加动画

Ruby‘s Adventrue游戏制作笔记(十一)Unity角色攻击——发射子弹

Ruby‘s Adventrue游戏制作笔记(十二)Unity给角色添加简单的特效

Ruby‘s Adventrue游戏制作笔记(十三)Unity血条UI的显示

Ruby‘s Adventrue游戏制作笔记(十四)Unity播放游戏音效

Ruby‘s Adventrue游戏制作笔记(十五)UnityNPC对话

Ruby‘s Adventrue游戏制作笔记(十六)Unity子弹数量及其UI

Ruby‘s Adventrue游戏制作笔记(十七)Unity添加游戏胜利条件和失败条件和导出游戏

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值