一些游戏逻辑

本文探讨了游戏开发中AI的追捕与攻击逻辑,详细介绍了如何通过计算玩家与AI的位置差来决定AI的行为,包括攻击与追捕的判断条件。同时,文中还讲解了镜头跟随玩家的实现方法,以及地图消除行的检查算法。此外,文章还涉及了不用动画状态机而使用代理的方法,以及玩家控制角色的完美旋转和速度调整。最后,提到了分数缓动、无限地形生成和触屏方向判断等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 追捕AI
        Vector2 offset = player.position - transform.position;
        if (offset.magnitude < 1.1f)
        {
            //攻击
        }
        else
        {
            //追捕
            float x = 0, y = 0;
            if (Mathf.Abs(offset.y) > Mathf.Abs(offset.x))
            {
                //按照y移动
                if (offset.y < 0)
                {
                    y = -1;
                }
                else
                {
                    y = 1;
                }
            }
            else
            {
                if (offset.x > 0)
                {
                    x = 1;
                }
                else
                {
                    x = -1;
                }
                //按照x移动
            }
            targetPosition += new Vector2(x, y);
        }
  1. 镜头跟随
    private Transform player;
    private Vector3 offsetPosition;//位置偏移
    // Use this for initialization
    void Start()
    {
        player = GameObject.FindGameObjectWithTag("Player").transform;
        transform.LookAt(player.position);
        offsetPosition = transform.position - player.position;
    }

    // Update is called once per frame
    void Update()
    {
        transform.position = offsetPosition + player.position;
    }
  1. 检查地图是否消除行
    private bool CheckMap()
    {
        int count = 0;
        for (int i = 0; i < MAX_ROWS; i++)
        {
            bool isFull = CheckIsRowFull(i);
            if (isFull)
            {
                count++;
                DeleteRow(i);
                MoveDownRowsAbove(i + 1);
                i--;
            }
        }
        if (count > 0)
        {
            score += (count * 100);
            if (score > highScore)
            {
                highScore = score;
            }
            isDataUpdate = true;
            return true;
        }
        else return false;
    }
  1. 不用动画状态机用代理
        if (animation[TurnLeft.name].normalizedTime > 0.95f)
        {
            animationHandler = PlayRun;
        }
  1. 完美旋转
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        if (Mathf.Abs(h) > 0.1 || Mathf.Abs(v) > 0.1)
        {
            float newSpeed = Mathf.Lerp(anim.GetFloat("Speed"), 5.6f, moveSpeed * Time.deltaTime);
            anim.SetFloat("Speed", newSpeed);
            Vector3 targetDir = new Vector3(h, 0, v);
            Quaternion newRotation = Quaternion.LookRotation(targetDir, Vector3.up);
            transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, rotateSpeed * Time.deltaTime);
        }
        else
        {
            //float newSpeed=Mathf.Lerp(anim.GetFloat("Speed"),0f,stopSpeed*Time.deltaTime);
            anim.SetFloat("Speed", 0);
        }
  1. 分数缓动
        IEnumerator AddScoreIE(float p_tonum)
        {
            while (m_CurrentScore < p_tonum)
            {
                //插值运算
                m_CurrentScore = Mathf.Lerp(m_CurrentScore, p_tonum, m_changeSpeed * Time.deltaTime);

                m_Score_Text.text = ((int)m_CurrentScore).ToString();

                yield return 0;
            }
        }
  1. 无限地型相关
	Vector3 zigzag = transform.right *(Mathf.PingPong(Time.time,2.0f)-1.0f);
  1. 触屏方向
        Vector2 touchDeltaPostion = Input.GetTouch(0).deltaPosition;
        if (Mathf.Abs(touchDeltaPostion.x) > Mahtf.Abs(touchDeltaPostion.y))
        {
            if (touchDeltaPostion.x > 10)
            {
                dirX = 1;
            }
            else if (touchDeltaPostion.x < -10)
            {
                dirX = -1;
            }
        }
        else
        {
            if (touchDeltaPostion.y > 10)
            {
                dirY = 1;
            }
            else if (touchDeltaPostion.y < -10)
            {
                dirY = -1;
            }
        }
  1. 指针设置
    private Vector2 hotspot = Vector2.zero;
    private CursorMode mode = CursorMode.Auto;

    public void SetNormal()
    {
        Cursor.SetCursor(cursor_normal, hotspot, mode);
    }
  1. 镜头视角拉近拉远
        distance = offsetPosition.magnitude;
        distance += Input.GetAxis("Mouse ScrollWheel") * scrollSpeed;
        offsetPosition = offsetPosition.normalized * distance;
  1. collider设置成相机的子物体就无法后退了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值