当前位置:网站首页>在Unity的Update中通过物体自身位置判断运动方向
在Unity的Update中通过物体自身位置判断运动方向
2022-08-10 05:35:00 【三和尚】
在Unity的Update方法中通过物体自身位置判断运动方向
1、在Unity场景中创建一个Cube物体。

2、新建一个脚本,名为"JudgeDirection"。
打开该脚本,编写如下代码,然后保存:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JudgeDirection : MonoBehaviour
{
private float originValue;
void Start()
{
originValue = transform.position.x;
}
void Update()
{
if (originValue - transform.position.x > 0)
{
Debug.Log("物体往左移动");
}
else if (originValue - transform.position.x < 0)
{
Debug.Log("物体往右移动");
}
originValue = transform.position.x;
}
}
该脚本只能判断物体往左或者是往右运动。如果想判断物体往前后上下运动,只需要修改物体自身的轴向即可,即把transform.position.x修改成transform.position.z或者是transform.position.y。
3、运行场景
拖拽Cube物体的X轴,就可以在Console控制台输出相关信息。
边栏推荐
猜你喜欢

每日刷题(day03)——leetcode 899. 有序队列

Unity中Xml简介以及通过脚本读取Xml文本中的内容

每日刷题(day02)——leetcode 622. 设计循环队列

【从零设计 LaTex 模板】1. 一些基础知识

Explain the principle of MySql index in detail

pytorch-11. Convolutional Neural Network (Advanced)

51单片机ST188手持人体温度脉搏心率测量仪锂电池充电

51单片机智能蓝牙APP加油站火灾预警安防防控报警监控系统MQ2DHT11

LeetCode 100. The same tree (simple)

Machine Learning - Clustering - Shopping Mall Customer Clustering
随机推荐
Likou - Number of Provinces
一个基于.Net Core 开源的物联网基础平台
树结构——二叉查找树原理与实现
51单片机ST188手持人体温度脉搏心率测量仪锂电池充电
51单片机BH1750智能补光灯台灯光强光照恒流源LED控制系统
51单片机室内环境甲醛PM2.5光照温度湿度检测及窗帘加湿消毒控制系统
LeetCode 292.Nim 游戏(简单)
.Net Core imports tens of millions of data to Mysql
LeetCode 938.二叉搜索树的范围和(简单)
pytorch-10. Convolutional Neural Networks
基于MNIST数据集的简单FC复现
PyTorch的安装与基础知识
详解样条曲线(上)(包含贝塞尔曲线)
Convolutional Neural Network (CNN) for Clothing Image Classification
通过adb devices命令在控制台显示企业级PicoNeo3设备号
PyTorch之CV
二维卷积定理的验证(下,cv2.filter2D())
pytorch-05.用pytorch实现线性回归
STM32F407ZG 串口通信+固定帧头帧尾传输数据帧
pytorch-10. Convolutional Neural Networks (homework)