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

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

一、实现效果

在这里插入图片描述

功能描述:

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

二、知识点学习

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值