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);
       
    }
    
}


上一篇:字体图标和变形 transform属性的使用


下一篇:transform,@keyframes