• Unity 人物跟谁手指的移动(第一种方式)


    长夜漫漫无心睡眠,敲敲代码,越敲越来劲! 我发现好多小朋友都在玩熊出没之xxxx这个游戏,居然打了一下午都没玩通第2关,我把测试也叫来陪我一起玩!

    结果他也打不通,我再去叫策划,他也没打过,我去叫主管,他还是没打过,我再回去叫测试使用游戏修改大师,修改宝石买完全部的装备!结果他还是没打过!

    (这款游戏,让我人生都变得没有了意义!)

    2154591iw981vvilxllvvk

    人物跟随手指的移动而移动:

    第一种方式: 手指移动了多少,主角就移动多少!(我使用的这种方式,存在问题,手指过快滑动,主角会慢慢跟手指有了距离!)

    第二种方式: 手指移动到哪里,主角就慢慢移动到手指所在的位置!(还没写代码,理论上不会出现第一种问题),建议采用这种方式实现

    第一种方式的部分代码实现:

    using UnityEngine;
    using System.Collections;
    
    
    /// <summary>
    /// 人物的移动
    /// </summary>
    public class PlayerMove : MonoBehaviour
    {
    
        private bool isMove;
        private Vector3 recordMouseDownPosition;        //记录鼠标单击的位子
         public float speed = 1;
        public UISprite gameBackground;
        public UI2DSprite player;
        public UI2DSpriteAnimation playerAnimation;
        public Sprite playerLeftState;
        public Sprite playerRightState;
    
    
        private float areaWidth;
        private float areaHeight;
    
        //角色移动的边界
    
        void Awake()
        {
            //获取到背景Sprite的宽度和高度
             areaWidth = gameBackground.drawingDimensions.z - player.drawingDimensions.z;
            areaHeight = gameBackground.drawingDimensions.w - player.drawingDimensions.w;
        }
    
    
        //开始移动
        public void PlayerStartMove()
        {
            isMove = true;
            recordMouseDownPosition = Input.mousePosition;
        }
    
        //结束移动
        public void PlayerEndMove()
        {
            isMove = false;
        }
    
        void LateUpdate()
        {
            if (isMove)
            {
                //鼠标当前坐标-单击被记录的坐标
                  Vector3 dir = Input.mousePosition - recordMouseDownPosition;
    
                //切换左右人物图片状态
                  if (dir.x > 0)
                {
                    Debug.Log("切换成左");
                    playerAnimation.enabled = false;
                    player.nextSprite = playerRightState;
                }
                else if (dir.x < 0)
                {
                    Debug.Log("右转弯图片");
                    playerAnimation.enabled = false;
                    player.nextSprite = playerLeftState;
    
                }
                else if (dir.x == 0)
                {
                    playerAnimation.enabled = true;
                }
    
                //让人物跟随手指的方向移动
                   this.transform.localPosition = new Vector3(
                        transform.localPosition.x + dir.x * 0.7f,
                        transform.localPosition.y + dir.y * 0.7f,
                        transform.localPosition.z);
    
                //防止跑出右边界
                  if (transform.localPosition.x > areaWidth && dir.x > 0)
                {
                    this.transform.localPosition = new Vector3(
                            areaWidth,
                            transform.localPosition.y,
                            transform.localPosition.z);
                }
    
                //防止跑出左边界
                  if (transform.localPosition.x < -areaWidth && dir.x < 0)
                {
                    this.transform.localPosition = new Vector3(
                            -areaWidth,
                            transform.localPosition.y,
                            transform.localPosition.z);
                }
    
                //防止跑出上边界
                  if (transform.localPosition.y > areaHeight && dir.y > 0)
                {
                    this.transform.localPosition = new Vector3(
                            transform.localPosition.x,
                            areaHeight,
                            transform.localPosition.z);
                }
    
                //防止跑出下边界
                  if (transform.localPosition.y < -areaHeight && dir.y < 0)
                {
                    this.transform.localPosition = new Vector3(
                            transform.localPosition.x,
                            -areaHeight,
                            transform.localPosition.z);
                }
    
                recordMouseDownPosition = Input.mousePosition;
            }
        }
    }
    如果你感兴趣,你可以把你妹妹介绍给我
  • 相关阅读:
    openssl rsautl和openssl pkeyutl(文件的非对称加密)
    Card Stacking 队列模拟
    poj 2456 Aggressive cows 贪心+二分
    E. MaratonIME does (not do) PAs
    B. Yet Another Crosses Problem
    hdu 2578 Dating with girls(1) 满足条件x+y=k的x,y有几组
    poj 3262 Protecting the Flowers 贪心 牛吃花
    hdu 1789 Doing Homework again 贪心
    贪心总结
    E. Third-Party Software
  • 原文地址:https://www.cnblogs.com/plateFace/p/4235097.html
Copyright © 2020-2023  润新知