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

本文介绍了如何使用Unity3D实现蛇游戏,通过鼠标控制蛇头转向,并详细讲解了Rotation赋值和反三角函数计算。中心圆脚本与蛇头脚本配合,确保蛇头在移动方向上的响应。

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

一、实现效果

在这里插入图片描述

功能描述:

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

二、知识点学习

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值