2017-02-24 69 views
-2

我正在努力添加一個將遊戲對象左右移動的腳本。添加將遊戲對象左右移動的腳本

我想創建一個函數來將遊戲的對象從左向右移動並避免其他對象的碰撞。

public AudioManager2 audiomanager; 
public GameManager gamemanager; 



private float xposition; 
public GameObject character; 
public Sprite[] characters; 
public bool teleporting=false; 
private int n; 
void Start(){ 
    ZPlayerPrefs.Initialize("group123", "happyapplications2016"); 
    teleporting = false; 
    n = ZPlayerPrefs.GetInt ("CHARACTER"); 
    Debug.Log (n.ToString()); 
    character.GetComponent<SpriteRenderer>().sprite = characters [n]; 
} 

void OnTriggerEnter2D(Collider2D other) { 
    //On circle touching the car, it will play scoreUp audio & send message to gamecontroller to add score . 
if(other.gameObject.tag == "Point"){ 
    Destroy(other.gameObject); 
     audiomanager.PlayPoint(); 
     ScoreManager.Score++; 
} 
    //On cube touching the car, it will send message to gamecontroller to end the game. 
if(other.gameObject.tag == "Obstacle"){ 
     Destroy(other.gameObject); 
     audiomanager.PlayGameOver(); 

     ScoreManager.lives--; 
     this.gameObject.SetActive (false); 
} 
    if(other.gameObject.tag == "Gear"){ 
     Destroy(other.gameObject); 
     audiomanager.PlayPoint(); 
     ScoreManager.gearsno++; 

    } 
    //If touches the floor, reposition it to top 
    if(other.gameObject.tag == "Floor"){ 

     this.gameObject.transform.position=new Vector3(this.transform.position.x,5.3f,0); 
    } 
    //Teleportation to left 
    if (other.gameObject.tag == "BridgeLeft") { 
     teleporting = true; 
     xposition = this.GetComponent<Transform>().position.x; 
     float newpos = xposition - 2.1f; 
     this.GetComponent<Transform>().transform.DOMoveX (newpos, 1.6f); 
     if (this.gameObject.tag == "Car1") 
      GameController.Car1CurrentPosition = GameController.Car1CurrentPosition - 3; 
     else if(this.gameObject.tag=="Car2") 
      GameController.Car2CurrentPosition = GameController.Car2CurrentPosition - 3; 
     StartCoroutine (ChangeTeleportation (1.6f)); 
+0

能否請你在什麼樣的方式更詳細的解釋你'正在掙扎?如同,你的腳本現在正在做什麼,以及它如何不能完成你想要實現的目標?像這樣的信息有助於在腳本中追蹤問題。 – Serlite

回答

0

創建if語句,讓你也變換對象 的位置。如果(other.gameObject.tag ==「地板」){

+0

如何將teleportation狀態返回爲true。感謝previuos反饋。 – Abdilgani

+0

創建一個你想要的任何公用函數名稱並返回函數如(return =) – SAYED