using UnityEngine;
using System.Collections;
//Add this script to the platform you want to move.
//左右移动的平台
public class MovingPlatform : MonoBehaviour {
//Platform movement speed.平台移动速度
public float speed;
//This is the position where the platform will move.平台移动的位置
public Transform MovePosition;//创建一个空物体作为移动的位置
private Vector3 StartPosition;
private Vector3 EndPosition;
private bool OnTheMove;
// Use this for initialization
void Start () {
//Store the start and the end position. Platform will move between these two points.储存左右两端点位置
StartPosition = this.transform.position;
EndPosition = MovePosition.position;
}
void FixedUpdate () {
float step = speed * Time.deltaTime;
if (OnTheMove == false) {
this.transform.position = Vector3.MoveTowards (this.transform.position, EndPosition, step);
}else{
this.transform.position = Vector3.MoveTowards (this.transform.position, StartPosition, step);
}
//When the platform reaches end. Start to go into other direction.
if (this.transform.position.x == EndPosition.x && this.transform.position.y == EndPosition.y && OnTheMove == false) {
OnTheMove = true;
}else if (this.transform.position.x == StartPosition.x && this.transform.position.y == StartPosition.y && OnTheMove == true) {
OnTheMove = false;
}
}
}
---------------------
Unity代码简单实现物体左右移动
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
上一篇:总结
下一篇:express显示数据库数据

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
java中的事件处理_让小球上下左右移动
java中的事件处理_让小球上下左右移动
java ide sed -
成长手删代码40-listbox js操作上下左右移动.txt
<script type ="text/javascript"> function imgRight() { var ddlRight=document.getElementById ("LBRi
JavaScript ViewUI i++ 数据 javascript -
Qt代码实现上下左右布局
Qt代码实现上下左右布局
qt 按钮 布局 -
jquery文字上下左右滚动代码 jquery实现div左右移动
jquery文字上下左右滚动代码 jquery实现div左右移动
ide css javascript -
利用键盘上的上下左右(回车)键模拟出tab 键的功能上下左右移动焦点
第一: 第一: 第一: 第一: 第一: 第一: 第一: 第一: 大陆 大陆 Untitled Document ...
html javascript d3 i++ 控件 -
visio移动形状 上下左右键 移动滚动条
今天在用visio写作业,想微移visio的形状,于是按上下左右方向键,结果移
.net 商业 javascript ide i++