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);
    }    
}
相关推荐
weixin_4093831240 分钟前
godot 调用class方法得用实例 不能用脚本引用
游戏引擎·godot
风酥糖43 分钟前
Godot游戏练习01-第32节-国际化
游戏·游戏引擎·godot
weixin_5206498744 分钟前
Winform创建与启动
开发语言·c#
武藤一雄1 小时前
WPF中逻辑树(Logical Tree)与可视化树(Visual Tree)到底是什么
microsoft·c#·.net·wpf·.netcore
周杰伦fans1 小时前
深入 C# 匿名类型:从 `new { Ask = ask }` 说起
开发语言·c#
魔士于安1 小时前
Unity类似博物馆场景
前端·unity·游戏引擎·贴图·模型
不会编程的懒洋洋1 小时前
C# IDisposable 和 using
开发语言·笔记·机器学习·c#·.net·visual studio·c#基础
家有娇妻张兔兔2 小时前
Apache POI 导出 Word 踩坑实录:Word 分栏为什么做不好左右平铺
c#·word·apache·poi·分栏
小拉达不是臭老鼠2 小时前
Unity数据持久化_XML
学习·unity
RReality2 小时前
【Unity Shader URP】模板遮罩 / 传送门 实战教程
ui·unity·游戏引擎·图形渲染·材质