ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Unity学习笔记(8)仿手游贪吃蛇第四章「设置触屏游戏手柄」「通过触屏手柄控制物体移动方向」「顺滑」

2021-11-30 16:30:31  阅读:161  来源: 互联网

标签:mousePos MyPos 手柄 thlta float transform Unity using 触屏


一、实现效果

在这里插入图片描述

功能描述:

  • 在不操作的情况下,规定蛇头按一定方向一直移动
  • 鼠标点击游戏手柄,拖动游戏手柄,蛇头转动相应的角度

二、知识点学习

1.Rotation的赋值

transform.rotation = Quaternion.Euler(0, 0, thlta_z);

2.计算反三角函数

1.using Unity.Mathematics;
2.float thltaText= math.atan((mousePos.x - MyPos.x) / (mousePos.y - MyPos.y))/math.PI * 180;
即 math.atan(XXX)/math.PI180;*

三、代码实现

1.中心圆上挂脚本CenterCircle.cs
2.蛇头上挂脚本 SnakeHead.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Mathematics;

public class CenterCircle : MonoBehaviour
{
    Vector3 mousePos;
    Vector3 MyPos;
    public Vector3 MoveDerectionPos;
    public float thlta;
    // Start is called before the first frame update
    void Start()
    {
        MyPos = transform.position;  
      //  Debug.Log(MyPos.x + "," + MyPos.y + "," + MyPos.z);
    }

    // Update is called once per frame
    void Update()
    {
        if(Input.GetMouseButton(0))
        {
            mousePos = Input.mousePosition;
            mousePos = Camera.main.ScreenToWorldPoint(mousePos);
            mousePos.z = MyPos.z;
           
            if (((mousePos.x - MyPos.x) * (mousePos.x - MyPos.x) + (mousePos.y - MyPos.y) * (mousePos.y - MyPos.y)) < 1)
            {

                transform.position = mousePos;
                float thltaText= math.atan((mousePos.x - MyPos.x) / (mousePos.y - MyPos.y))/math.PI*180;
                
                //Debug.Log("thltaText:" + thltaText);
                MoveDerectionPos = mousePos - MyPos;
                if ( MoveDerectionPos.y >= 0)
                    thlta = -thltaText;
                if (MoveDerectionPos.x >= 0 && MoveDerectionPos.y < 0)
                    thlta = -thltaText - 180;
                if (MoveDerectionPos.x <= 0 && MoveDerectionPos.y < 0)
                    thlta = 180-thltaText;
               // Debug.Log("thlta:" + thlta);
               //Debug.Log("鼠标坐标:" + mousePos);
               //Debug.Log("物体坐标:" + MyPos);
               //Debug.Log("移动坐标:" +MoveDerectionPos);
               //  Debug.Log("在范围内");

            }
          
        }
    }
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public class SnakeHead : MonoBehaviour
{
    float interval = 0.4f;
    float count;
    float speed = 1f;
    Vector3 PlanePos;
   
    void Start()
    {
        PlanePos = transform.position;
       // Center = gameObject.transform.Find("中心圆").transform;
        
    }

    void Update()
    {
        
        count += speed * Time.deltaTime;
        if(count>interval)
        {
            float y = Time.deltaTime;
            transform.Translate(0, y, 0);
        }
        //从中心圆获取Rotation的z值
        float thlta_z = GameObject.Find("中心圆").GetComponent<CenterCircle>().thlta;
        //Debug.Log("转角:"+thlta_z);
        transform.rotation = Quaternion.Euler(0, 0, thlta_z);
       
    }
    
}


标签:mousePos,MyPos,手柄,thlta,float,transform,Unity,using,触屏
来源: https://blog.csdn.net/Zz_daianna/article/details/121635535

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有