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);
    }    
}
相关推荐
黑夜中的潜行者2 小时前
构建高性能 WPF 大图浏览器:TiledViewer 技术解密
性能优化·c#·.net·wpf·图形渲染
LongtengGensSupreme3 小时前
C# 中监听 IPv6 回环地址(Loopback Address)----socket和tcp
c#·ipv6 回环地址
就是有点傻3 小时前
C#中如何和西门子通信
开发语言·c#
海底星光3 小时前
c#进阶疗法 -jwt+授权
c#
液态不合群3 小时前
如何提升 C# 应用中的性能
开发语言·算法·c#
多多*4 小时前
计算机网络相关 讲一下rpc与传统http的区别
java·开发语言·网络·jvm·c#
阿蒙Amon4 小时前
C#每日面试题-简述反射
开发语言·面试·c#
缺点内向4 小时前
告别“复制粘贴”:用C#和模板高效生成Word文档
开发语言·c#·word
海底星光5 小时前
c#进阶疗法 -自定义鉴权
c#
FuckPatience5 小时前
C# .csproj Baseoutputpath/Outputpath、AppendTargetFrameworkToOutputPath
c#