unity 物体转动一周前进一个单位

该博客介绍了一个Unity脚本,实现物体通过鼠标拖动旋转并沿Z轴平移,同时检测物体位置与用户输入值的差值,当差值小于特定阈值时判断为正确输入。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;




public class Kedu : MonoBehaviour
{


    private float zz;
    //垂直方向的增量
    private float number;
    //位置数值
    private float numberChange;
    //读数显示数值
    private float kk = 0;
    //角度变量
    public InputField inputTxt;
    //输入框
    private float inputTxtNumber;
    public GameObject ban;
    //对错版
    public Image dui;
    //对
    public Image cuo;
    //错
    public Button btn;
    //按钮


    private void Start()
    {
        ban.SetActive(false);
        //刚开始对错版不显示
    }


    private void OnMouseDown()
    {
        zz = 0;
        //增量为零
    }


    private void Update()
    {
        zz = -Input.GetAxis("Mouse Y");
        
    }






    private void OnMouseDrag()
    {
        //算法
        // print("开始拖拽!");
        transform.Rotate(zz * 3, 0, 0);
        kk += zz * 3;
        transform.localPosition = new Vector3(-kk / 7200, 0, 0);
     


    }


    //数值检测
    public void numberText()
    {


        //实际读数,获取子物体的坐标
        number = 25 + transform.localPosition.x * 10;
        //保留三位小数
        numberChange = (float)(Mathf.Round(number * 1000)) / 1000;
        print("实际数值:" + numberChange);




        if (inputTxt.text == "")
        {
            inputTxtNumber = 0f;
            print("输入错误!");
            //显示错误
            ban.SetActive(true);
            dui.gameObject.SetActive(false);
            cuo.gameObject.SetActive(true);


        }
        else
        {


            try
            {


               inputTxtNumber = float.Parse(inputTxt.text);
                print("输入数值" + inputTxtNumber);
                //字符串转化成float


            }
            catch
            {
                print("输入错误!");
                //显示错误
                ban.SetActive(true);
                dui.gameObject.SetActive(false);
                cuo.gameObject.SetActive(true);


            }
        }


        float  cha =Mathf .Abs (numberChange - inputTxtNumber);


        print("差值!" + cha);


        if (cha <= 0.005)
        {
            //显示正确
            ban.SetActive(true);
            dui.gameObject.SetActive(true);
            cuo.gameObject.SetActive(false);
        }
        else
        {
            //显示错误
            ban.SetActive(true);
            dui.gameObject.SetActive(false);
            cuo.gameObject.SetActive(true);
        }


    }


}
### 实现 Unity物体的自转和公转 为了实现物体Unity 中既能够自转又可以围绕另一个中心点进行公转的效果,可以通过编写脚本来控制物体的位置变化以及自身的旋转角度。 #### 自转功能 对于使物体绕自己的轴线转动的功能,可采用如下 C# 脚本: ```csharp using System.Collections; using System.Collections.Generic; using UnityEngine; public class RotateSelf : MonoBehaviour { public float rotationSpeed = 10.0f; // 设置自转的速度 void Update() { transform.Rotate(Vector3.up, rotationSpeed * Time.deltaTime); } } ``` 这段代码会使得应用此脚本的对象按照设定好的速率沿 Y 轴方向顺时针或逆时针旋转[^1]。 #### 公转功能 为了让一个对象环绕另一固定点运动,则需定义目标作为轨道圆心,并调整其相对于该点的角度来模拟行星绕太阳般的轨迹移动。下面给出了一种方法用于创建这样的效果: ```csharp using System.Collections; using System.Collections.Generic; using UnityEngine; public class OrbitAroundCenter : MonoBehaviour { public Transform centerOfOrbit; // 设定公转中心 public float orbitRadius = 5.0f; // 定义距离中心的距离 public float orbitSpeed = 2.0f; // 控制公转速度 private float currentAngle = 0.0f; void LateUpdate(){ MoveInCircle(); } void MoveInCircle(){ currentAngle += orbitSpeed * Time.deltaTime; Vector3 offsetFromCenter = new Vector3(Mathf.Sin(currentAngle)*orbitRadius, 0, Mathf.Cos(currentAngle)*orbitRadius); transform.position = centerOfOrbit.position + offsetFromCenter; } } ``` 上述 `MoveInCircle` 函数通过不断更新当前角度并计算新的坐标位置,从而实现了平滑连续的圆形路径上的位移变换。 当希望某个游戏物件既能保持自身旋转又能沿着特定半径做周期性的环形路线前进时,只需同时附加这两个类即可达成目的。注意要确保两个脚本中的参数配置合理匹配实际需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值