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);
    }    
}
相关推荐
阿正的梦工坊1 小时前
【Rust】09-泛型、Trait 与生命周期基础
开发语言·rust·c#
z落落1 小时前
C# 事件(Event)+自定义带参数事件例子
开发语言·分布式·c#
FlYFlOWERANDLEAF1 小时前
DevExpress Office File API使用记录
开发语言·c#·devoffice
richard_yuu2 小时前
C#工业上位机项目实战第九篇:可视化流程引擎完整落地,节点拖拽、连线渲染与自动化调度
c#·自动化
njsgcs3 小时前
c# solidworks 创建装配体工程图+bom
开发语言·c#·solidworks
njsgcs3 小时前
c# solidworks 工程图获得展开视图不在固定面螺纹特征的位置
开发语言·c#·solidworks
深海潜水员6 小时前
【从零开始的C#游戏开发课程】- FarmStory1.0 日志系统和游戏资源的管理
游戏·c#·monogame
叶帆6 小时前
【YFIOs】用C#开发硬件之WiFi网络
开发语言·网络·c#
天下无敌笨笨熊7 小时前
C# LINQ开发心得
c#·linq
mxwin8 小时前
Unity Shader URP:法线如何进行光照计算
unity·游戏引擎·shader