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);
    }    
}
相关推荐
hez20102 小时前
C# 15 类型系统改进:Union Types
c#·.net·.net core
FL16238631292 小时前
基于C#winform部署软前景分割DAViD算法的onnx模型实现前景分割
开发语言·算法·c#
mxwin2 小时前
Unity URP 热更新兼容性:Shader 在 IL2CPP 打包下的注意事项
unity·游戏引擎
C#程序员一枚3 小时前
高可用(High Availability, HA)
数据库·c#
weixin_520649874 小时前
C#进阶-特性全知识点总结
开发语言·c#
fengyehongWorld5 小时前
C# 创建vba用的类库
c#
澄澈青空~5 小时前
有一个叫R2C,也有一个叫G2C
java·数据库·人工智能·c#
mxwin7 小时前
Unity shader中TransformWorldToShadowCoord原理解析
unity·游戏引擎·shader
mxwin7 小时前
Unity Shader 中 ShadowCaster的作用和疑问
unity·游戏引擎
PGFA7 小时前
深度剖析 C# LINQ 底层执行机制:别让你的应用内存莫名其妙“爆”掉!
c#·solr·linq