Unity【角色/摄像机移动控制】【1.角色移动】

本文主要总结实现角色移动的解决方案。
1. 创建脚本:PlayerController
2. 创建游戏角色Player,在Player下挂载PlayerController脚本
3. 把Camera挂载到Player的子物体中,调整视角,以实现相机跟随效果
3. PlayerController脚本代码如下:

csharp 复制代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float speed = 5.0f;
    // Start is called before the first frame update
    void Start()
    {
        Transform transform = GetComponent<Transform>();
    }

    // Update is called once per frame
    void Update()
    {      
        Move();
    }
    
    private void Move()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        float verticalInput = Input.GetAxis("Vertical");
        
        Vector3 moveingVec = new Vector3(horizontalInput, 0f, verticalInput) * Time.deltaTime * speed;
        transform.Translate(moveingVec);
    }    
}
相关推荐
波力海苔夹心脆6752 小时前
C# StringBuilder 详解:字符串拼接为什么总说要用它?
开发语言·笔记·c#·.net·字符串·stringbuilder·入门教程
淡海水2 小时前
06-03-源码-时间分片与调度策略
unity·c#·gc
丑梨数据3 小时前
法律合同知识库怎么搭:从图片压缩到私有化 RAG 的工程笔记
开发语言·笔记·c#
1-1=04 小时前
.NET Core 输出日志
c#·.net·.netcore
曹牧4 小时前
C#里引用ActiveX控件
开发语言·c#
宇卿.4 小时前
C#委托详解
开发语言·c#
努力,努力再努力5 小时前
零依赖 C# Office 库到底怎么用——四个场景讲透 Wisdom.Office
c#
2501_930707788 小时前
使用C#代码在 PDF 文档中创建交互操作
pdf·c#·交互
zimoyin18 小时前
在 C# 里嵌入 JavaScript:用 Jint 做可热更新的自动化脚本(从入门到踩坑)
javascript·c#·自动化
小羊没烦恼!20 小时前
异步编程:线程概述及使用
java·开发语言·mysql·算法·c#