Advertisement

通过C#实现Unity中物体在左右之间循环移动

阅读量:

若在x轴的区间[-5,5]内进行位移,无需过多解释,直接展示代码如下:

复制代码
 using System.Collections;

    
 using System.Collections.Generic;
    
 using UnityEngine;
    
  
    
 public class NewBehaviourScript : MonoBehaviour {
    
     // Use this for initialization
    
     public float speed = 3f;
    
     private bool movingRight = true;
    
     void Start () {
    
  
    
     }
    
 	
    
 	// Update is called once per frame
    
 	void FixedUpdate ()
    
     {
    
     //假设我在x轴-5到5之间左右循环移动
    
     if(movingRight)
    
     {
    
         //我正在右移
    
         transform.Translate(Vector3.right * speed *

全部评论 (0)

还没有任何评论哟~