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);
    }    
}
相关推荐
kylezhao20193 小时前
第1章:第一节 开发环境搭建(工控场景最优配置)
开发语言·c#
钰fly6 小时前
C#文件与数据操作核心概念手册
c#
阿蒙Amon6 小时前
C#每日面试题-简述C#访问修饰符
windows·microsoft·c#
酩酊仙人8 小时前
ABP+Hangfire实现定时任务
后端·c#·asp.net·hangfire
阿蒙Amon9 小时前
C#每日面试题-属性和特性的区别
java·面试·c#
要记得喝水10 小时前
某公司C#-WPF面试题-来自nowcoder(含答案和解析)--2
c#·wpf
沉默金鱼11 小时前
Unity实用技能-模型
unity·游戏引擎
阿里云云原生12 小时前
AgentRun:如何利用 AI Agent 构建现代化的舆情分析解决方案?
人工智能·unity·游戏引擎
在路上看风景12 小时前
2.8 预渲染
unity
老朱佩琪!13 小时前
Unity代理模式
unity·游戏引擎·代理模式