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);
    }    
}
相关推荐
F20226974863 小时前
西门子 PLC 与 C# 通信
开发语言·c#
吴可可1238 小时前
C#实现CAD框选闭合图元外偏移1毫米
c#
꒰ঌ 安卓开发໒꒱8 小时前
.NET CAP入门到入土
后端·c#·.net
逝水无殇12 小时前
C# 枚举(Enum)详解
开发语言·后端·c#
Java面试题总结14 小时前
C# 源生成器使用方法
windows·ui·c#
影寂ldy14 小时前
C# WinForms 窗体继承
开发语言·c#
苍狼唤1 天前
WinForm练习知识补充(多线程)
c#
真鬼1231 天前
【Unity WebGL】内嵌网页与双向通信
unity·游戏引擎·webgl
逝水无殇1 天前
C# 字符串(String)详解
开发语言·后端·c#
小巧的砖头1 天前
C#会重蹈覆辙吗?系列之2:反射及元数据的性能问题
开发语言·前端·c#