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);
    }    
}
相关推荐
WiChP1 分钟前
【V0.1B7】从零开始的2D游戏引擎开发之路
游戏引擎
qq_431280701 小时前
工作经验总结:半导体上位机软件开发与互联网开发的不同
c#·.net
Metaphor6921 小时前
使用 Python 查找并替换 Word 文档中的文本
python·c#·word
chen_2271 小时前
kanzi插件之节点树可视化
c#·kanzi
傻啦嘿哟2 小时前
管好PPT的“骨架”:用Python控制页面与文档属性
开发语言·javascript·c#
Densen20142 小时前
企业H5站点升级PWA (三)
前端·nginx·c#
Sparkle Star3 小时前
Unity VRTK4+SteamVR传送组件使用和层级关系
unity·游戏引擎
cheniie3 小时前
Windows下Unity开发VisionPro应用
windows·unity·vision pro
伽蓝_游戏3 小时前
UGUI源码剖析 (24):常用插件扩展介绍
ui·unity·c#·游戏引擎·游戏程序
空中海14 小时前
第二篇:Unity中级阶段(核心开发能力)
unity·游戏引擎