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

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

作者:互联网

一、实现效果

在这里插入图片描述

功能描述:

二、知识点学习

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