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);
    }    
}
相关推荐
lijingguang2 小时前
在C#中根据URL下载文件并保存到本地,可以使用以下方法(推荐使用现代异步方式)
开发语言·c#
¥-oriented2 小时前
【C#中路径相关的概念】
开发语言·c#
ArabySide2 小时前
【WCF】通过AOP实现基于JWT的授权与鉴权的实践
c#·jwt·aop·wcf
Thomas_YXQ3 小时前
Unity3D DOTS场景流式加载技术
java·开发语言·unity
xiaowu0803 小时前
C# Task异步的常用方法
c#
阿蒙Amon3 小时前
C# Linq to Objects 详解:集合处理的终极方案
c#·solr·linq
钢铁男儿3 小时前
C# 委托(调用带引用参数的委托)
java·mysql·c#
番茄小能手4 小时前
【全网唯一】C# 纯本地离线文字识别Windows版dll插件
开发语言·c#
葬歌倾城4 小时前
waferMap图像渲染
c#·wpf
甄天5 小时前
WPF路由事件:冒泡、隧道与直接全解析
c#·wpf·visual studio