unity---unity编译手机apk人物角色移动

方法一:

导航栏Windows---package manager ---unity registr安装cinemachine

点击导航栏gameobject----ciinemachine---2d camera。

点击virtual camera,将所要跟随的人物物体拖到follow那里。

挂载001物体移动脚本move:

复制代码
using UnityEngine;

public class move : MonoBehaviour
{
    public float speed = 5f;
    private Animator animator;

    void Start()
    {
        animator = GetComponent<Animator>();
    }

    void Update()
    {
        // 获取水平(A/D)、垂直(W/S)输入
        float x = Input.GetAxisRaw("Horizontal");
        float y = Input.GetAxisRaw("Vertical");

        // 判断是否正在移动(任意方向有输入就播放走路动画)
        bool isMoving = (x != 0 || y != 0);
        animator.SetBool("isWalk", isMoving);

        // 构建移动向量
        Vector3 moveDir = new Vector3(x, y, 0);
        // 归一化:防止斜向移动速度变快
        if (moveDir.magnitude > 1)
        {
            moveDir.Normalize();
        }

        transform.Translate(moveDir * speed * Time.deltaTime);

        // 左右翻转(只有左右输入的时候翻转角色朝向)
        if (x > 0)
        {
            transform.localScale = new Vector3(1, 1, 1);
        }
        else if (x < 0)
        {
            transform.localScale = new Vector3(-1, 1, 1);
        }
    }
}

move2脚本可替换成move脚本。

move2脚本:

复制代码
using UnityEngine;

public class move2 : MonoBehaviour
{
    public float speed = 5f;

    private Animator animator;


    void Start()
    {
        animator = GetComponent<Animator>();
    }


    void Update()
    {
        Vector3 moveDir = Vector3.zero;


        // 手机触摸
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);

            Vector2 touchPos = touch.position;


            // 屏幕中心点
            Vector2 center =
                new Vector2(
                    Screen.width / 2,
                    Screen.height / 2
                );


            // 手指相对屏幕中心方向
            Vector2 direction =
                touchPos - center;


            // 转换成方向向量
            moveDir =
                new Vector3(
                    direction.x,
                    direction.y,
                    0
                ).normalized;
        }



        // 是否移动
        bool isMoving =
            moveDir.magnitude > 0;


        animator.SetBool(
            "isWalk",
            isMoving
        );



        // 移动
        transform.Translate(
            moveDir *
            speed *
            Time.deltaTime
        );



        // 左右翻转
        if (moveDir.x > 0)
        {
            transform.localScale =
                new Vector3(1, 1, 1);
        }
        else if (moveDir.x < 0)
        {
            transform.localScale =
                new Vector3(-1, 1, 1);
        }

    }
}

测试:鼠标操作左右键摄像头跟着移动。

方式二:

直接使用脚本让摄像头跟随。

脚本camerafollow

复制代码
using UnityEngine;

public class CameraFollow : MonoBehaviour
{
    public Transform player;
    public float smoothSpeed = 5f;

    void LateUpdate()
    {
        // 同时跟随 X、Y;Z保持相机自身不变(适合2D游戏)
        Vector3 target = new Vector3(
            player.position.x,
            player.position.y,    // ← 这里改成玩家Y,实现上下跟随
            transform.position.z
        );

        transform.position = Vector3.Lerp(
            transform.position,
            target,
            smoothSpeed * Time.deltaTime
        );
    }
}

测试:鼠标操作左右键摄像头跟着移动。

配置apk信息:

软件图标:

导航栏edit---project settings ----Player选中default icon

软件名称:

导航栏edit---project settings ----Player选中product name

构建运行apk:

点击导航栏file----build and run

相关推荐
一线灵15 小时前
跨平台游戏引擎 Axmol-2.11.6 发布
游戏引擎
两水先木示17 小时前
【Unity】Lens Flare光晕简单使用案例
unity·游戏引擎
专注仿真18 小时前
Unity 小游戏实战:2D 横版闯关 / 3D 跑酷 / 塔防 / 解谜四类项目工程化拆解与复用实践
游戏·游戏引擎·游戏程序
郝学胜-神的一滴1 天前
游戏设计、原型与开发 26:四种循环
开发语言·数据结构·游戏·游戏引擎·游戏策划
两水先木示1 天前
【Unity】刮刮乐案例-RenderTexture实现思路
unity·游戏引擎
学术小李2 天前
用虚幻引擎(UE)打开Obj模型
游戏引擎·虚幻
张不无-Unity2 天前
Unity的四种灯光Directional、Point、Spot 和 Area
unity·游戏引擎
淡海水3 天前
02-02-原理篇-Scriban模板引擎原理深度解析
unity·excel·游戏开发·luban
ttod_qzstudio4 天前
系统架构设计师论文逐句拆解(三):ATAM 架构评估,怎样把“方案比较”写成质量属性权衡
unity·架构·系统架构·软考
小贺儿开发5 天前
Unity 轻量级外部素材加载器(热更新)
科技·学习·unity·程序员·工具·热更新·ugui