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);
    }    
}
相关推荐
张人玉11 小时前
C#如何使用FreeSql连接数据库
数据库·c#·freesql
吴可可1231 天前
C#打断多段线实现方法
c#
Kari111 天前
医药和制造行业的外文文档批量翻译,排版怎么保住?
开发语言·c#·制造
Warson_L2 天前
.NET 8 速成笔记 1:用 VS Code 做 C# / .NET 开发
c#·.net
Warson_L2 天前
.NET 8 速成笔记 9:C# 项目常见场景(基础设施篇)
c#·.net
Warson_L2 天前
.NET 8 速成笔记 8:C# 项目常见业务场景与写法(数据与业务篇)
c#·.net
Warson_L2 天前
.NET 8 速成笔记 3:作业清单
c#·.net
Warson_L2 天前
.NET 8 速成笔记 2:C# / .NET 核心概念(结合本项目代码)
c#·.net
Warson_L2 天前
.NET 8 速成笔记 7:CRUD 速成,10 步加一个新资源
c#·.net
Warson_L2 天前
.NET 8 速成笔记 6:C# 项目结构与依赖管理
c#·.net