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);
    }    
}
相关推荐
农村小镇哥11 小时前
C#中的字符串格式化
服务器·开发语言·c#
weixin_4242946711 小时前
Unity的测试Edit Mode和Play Mode,有什么区别?
unity
Python私教15 小时前
我用 AI 做出了第一个 Godot 贪吃蛇
人工智能·游戏引擎·godot
czhc114007566315 小时前
719:StartPoint;Margin;ResourceDictionar;Component.model;lds;
c#
玖玥拾15 小时前
Unity 3D 笔记(八)ScrollRect 滚动视图、NavMesh 自动寻路系统
笔记·3d·unity
-银雾鸢尾-17 小时前
C#中的万物之父——Object类
开发语言·c#
水晶石NSIS专栏17 小时前
Geek_Studio - 安卓终端调试工作台
android·c#·apk签名·coloros主题
-银雾鸢尾-17 小时前
C#中的继承
开发语言·c#
吴可可1231 天前
C#AutoCAD二次开发打断多段线
c#