一、实现效果

功能描述:
- 在不操作的情况下,规定蛇头按一定方向一直移动
- 鼠标点击游戏手柄,拖动游戏手柄,蛇头转动相应的角度
二、知识点学习
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

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

被折叠的 条评论
为什么被折叠?



