当前位置:网站首页>在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控制台输出相关信息。
边栏推荐
猜你喜欢
随机推荐
String common methods
[List Exercise] Traverse the collection and sort by price from low to high,
LeetCode 面试题17.14 最小k个数(中等)
【简易笔记】PyTorch官方教程简易笔记 EP2
中间件-Rocktmq
pytorch-11. Convolutional Neural Network (Advanced)
51单片机智能蓝牙APP加油站火灾预警安防防控报警监控系统MQ2DHT11
2022李宏毅机器学习hw1--COVID-19 Cases Prediction
Flutter的生命周期
LeetCode refers to offer 10-I. Fibonacci sequence (simple)
以STM32F103C6T6为例通过配置CubeMX实现EXIT外部中断
STM32F407ZG PWM
过大数组导致爆栈的解决方法记录(堆栈)
解决错误 Could not find method leftShift() for arguments
VTK 初步 (1) ----- 可视化管线
LeetCode 100. The same tree (simple)
STC12C5A60S2单片机WIFI信号扫描报警监视系统信号增强信号过低报警
详解 Hough 变换(下)圆形检测
STM32单片机手机APP蓝牙高亮RGB彩灯控制板任意颜色亮度调光
LeetCode 1720.解码异或后的数组(简单)