Unity实现物体左右移动效果的方法

小编给大家分享一下Unity实现物体左右移动效果的方法,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

创新互联建站于2013年开始,先为济源等服务建站,济源等地企业,进行企业商务咨询服务。为济源企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

具体内容如下

效果如下

Unity实现物体左右移动效果的方法

代码:

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实现物体左右移动效果的方法”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


本文题目:Unity实现物体左右移动效果的方法
文章路径:http://bzwzjz.com/article/pdisdi.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都企业网站建设 成都网站设计 营销型网站建设 商城网站建设 重庆网站建设 网站制作报价 成都网站制作 成都网站设计 成都定制网站建设 成都网站设计公司 重庆网站建设 成都网站设计 成都响应式网站建设公司 定制网站制作 网站建设公司 成都品牌网站设计 成都网站设计 企业网站设计 网站制作 高端品牌网站建设 网站制作公司 营销网站建设