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);
    }    
}
相关推荐
星之擎26 分钟前
Task 很优雅的取消方式是什么?C# 里面。
开发语言·c#
czhc11400756632 小时前
一周概念复盘:预热、漂移与门禁
c#
云中飞鸿2 小时前
.Net和C#有什么关系?(从C++视角观察)
开发语言·c#·.net
Oiiouui3 小时前
Godot-GridGame架构逻辑层与实体/物品层篇
架构·游戏引擎·godot
天天进步20153 小时前
Unity 2020.3 反复提示安全模式:一次 CS0009 与 DLL 文件占用的排查记录
unity·1024程序员节
true...3 小时前
虚幻还原默认视角
游戏引擎·虚幻
淡海水5 小时前
07-04-源码-.NET Core GC清扫与压缩
unity·架构·c#·.netcore·gc·1024程序员节
zwm26988881513 小时前
hd 3#负一西送风,能远程启不能远程停
c#
Ai-_Man13 小时前
Meta Muse还是不可以导出左侧所有对话,只能点进去单条内容导出,方法不对吗?
开发语言·人工智能·ai·小程序·c#·1024程序员节
小贺儿开发19 小时前
《长征 · 征途》Unity 三维互动沙盘
科技·3d·unity·1024程序员节·地形·互动·沙盘