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);
    }    
}
相关推荐
贾斯汀frank4 小时前
C# 15 类型系统改进:Union Types
开发语言·windows·c#
时代的狂5 小时前
如何理解 C# 的 async 和 await
c#·.netcore·async·await
xiaoshuaishuai88 小时前
C# AI实现PR处理、单元测试
开发语言·c#·log4j
LONGZHIQIN9 小时前
C#基础复习笔记
开发语言·笔记·c#
时代的狂11 小时前
如何理解 C#/.NET 的依赖注入与生命周期
c#·.net·依赖注入·控制反转
品尚公益团队11 小时前
C#在asp.net网页开发中的带cookie登录实现
前端·c#·asp.net
吴可可12311 小时前
C#自定义CAD多段线零件实体
c#
CallmeFoureyes11 小时前
使用 C# 提取 Word 文档中的表格数据
开发语言·c#·word
思麟呀12 小时前
在C++基础上理解CSharp-7
java·jvm·c++·c#
杰佛史彦明113 小时前
ElasticSearch中的分词器详解
大数据·elasticsearch·c#