Unity3D中如何用代码实现物体的左右循环移动 最好是C# JS也可以 初学者 希望详细代码 谢谢啊
1个回答
展开全部
1、新建一个Cube,在Cube X轴的正方向放置竖孝一个空物体或者其他GameObject,Cube和氏晌空物体的Y值一致,确保2者在同一水平线上;
2、把下列余核稿代码保存为C#,赋给Cube,并在Inspector视图中,把空物体赋到脚本的PointB中;
using UnityEngine;
using System.Collections;
public class Moving : MonoBehaviour
{
public Transform PointB;
private int _direction = 1;
private float _pointA;
// Use this for initialization
IEnumerator Start ()
{
_pointA = transform.position.x;
while (true)
{
if (transform.position.x < _pointA)
{
_direction = 1;
}
if (transform.position.x > PointB.position.x)
{
_direction = -1;
}
transform.Translate(_direction * 2 * Time.deltaTime,0,0);
yield return 0;
}
}
}
2、把下列余核稿代码保存为C#,赋给Cube,并在Inspector视图中,把空物体赋到脚本的PointB中;
using UnityEngine;
using System.Collections;
public class Moving : MonoBehaviour
{
public Transform PointB;
private int _direction = 1;
private float _pointA;
// Use this for initialization
IEnumerator Start ()
{
_pointA = transform.position.x;
while (true)
{
if (transform.position.x < _pointA)
{
_direction = 1;
}
if (transform.position.x > PointB.position.x)
{
_direction = -1;
}
transform.Translate(_direction * 2 * Time.deltaTime,0,0);
yield return 0;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询