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);
    }    
}
相关推荐
不绝1919 小时前
导航系统/NavMeshAgent组件
unity
武藤一雄9 小时前
19个核心算法(C#版)
数据结构·windows·算法·c#·排序算法·.net·.netcore
不会编程的懒洋洋10 小时前
C# Task async/await CancellationToken
笔记·c#·线程·面向对象·task·同步异步
mxwin11 小时前
Unity Shader 屏幕空间 UVScreen Space UV 完全指南
unity·游戏引擎·uv
lhbian13 小时前
AI编程革命:Codex让脚本开发提速10倍
开发语言·汇编·jvm·c#
LF男男15 小时前
TouchManager
unity·c#
mxwin16 小时前
Unity Shader 径向模糊与径向 UV 变形速度感 · 冲击波效果完全指南
unity·游戏引擎·shader·uv
weixin_4239950016 小时前
unity 微信开发小游戏,网络资源获取数据
unity·游戏引擎
xiaoshuaishuai816 小时前
C# Submodule 避坑指南
服务器·数据库·windows·c#
Yasin Chen16 小时前
Unity TMP_SDF 分析(五)片元着色器
unity·游戏引擎·着色器