Unity实现物体左右移动效果

作者:_April_ 时间:2021-12-17 15:52:31 

本文实例为大家分享了Unity实现物体左右移动效果的具体代码,供大家参考,具体内容如下

效果如下

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;
}
}

}

来源:https://blog.csdn.net/wang568270833/article/details/97936537

标签:Unity,移动
0
投稿

猜你喜欢

  • android实现蓝牙app代码

    2021-07-08 07:52:15
  • 如何基于java实现Gauss消元法过程解析

    2023-12-15 21:51:08
  • Android 实现定时器的四种方式总结及实现实例

    2023-04-20 15:22:04
  • Java位掩码控制权限与(&)或(|)非(~)、>的介绍

    2023-05-27 19:10:15
  • 解决springboot生成bean名称冲突(AnnotationBeanNameGenerator)

    2023-01-09 22:27:11
  • C# menuStrip控件实现鼠标滑过自动弹出功能

    2022-09-01 05:18:37
  • 详解Android 中的文件存储

    2023-12-21 17:44:44
  • Spring bean的实例化和IOC依赖注入详解

    2023-11-23 23:57:15
  • Android app第三方支付宝支付接入教程

    2022-06-05 20:02:19
  • SpringBoot中如何统一接口返回与全局异常处理详解

    2021-08-11 02:22:35
  • java实现简单斗地主(看牌排序)

    2023-09-12 14:42:40
  • 一天时间用Java写了个飞机大战游戏,朋友直呼高手

    2023-12-11 10:51:30
  • Android 7.0新特性详解

    2022-10-10 07:11:56
  • Android仿QQ空间动态界面分享功能

    2023-11-13 11:03:58
  • Java日常练习题,每天进步一点点(53)

    2023-08-12 00:55:00
  • FeignClient实现接口调用方式(不同参数形式)

    2023-03-06 08:41:39
  • C#反射(Reflection)详解

    2022-09-16 22:58:11
  • 如何将C语言代码转换为应用程序(也就是编译)

    2022-09-02 06:30:49
  • VsCode配置java环境的详细图文教程

    2022-03-29 00:56:32
  • C#控制台实现飞行棋游戏

    2022-03-12 04:18:40
  • asp之家 软件编程 m.aspxhome.com