C# 添加将游戏对象左右移动的脚本

C# 添加将游戏对象左右移动的脚本,c#,unity3d,C#,Unity3d,我正在努力添加一个脚本来左右移动游戏对象 我想创建一个函数,将游戏对象从左向右移动,避免其他对象碰撞 public AudioManager2 audiomanager; public GameManager gamemanager; private float xposition; public GameObject character; public Sprite[] characters; public bool teleporting=false; private int n; v

我正在努力添加一个脚本来左右移动游戏对象

我想创建一个函数,将游戏对象从左向右移动,避免其他对象碰撞

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));
公共音频管理器2音频管理器;
公共游戏经理游戏经理;
私人浮动位置;
公共游戏对象角色;
公共精灵[]字符;
公共广播传送=假;
私人int n;
void Start(){
ZPlayerPrefs.Initialize(“group123”、“happyapplications2016”);
传送=假;
n=ZPlayerPrefs.GetInt(“字符”);
Log(n.ToString());
character.GetComponent().sprite=字符[n];
}
无效OnTiggerEnter2D(碰撞的R2D其他){
//在圆圈触摸汽车时,它将播放scoreUp音频并向gamecontroller发送消息以添加分数。
if(other.gameObject.tag==“点”){
销毁(其他游戏对象);
audiomanager.PlayPoint();
ScoreManager.Score++;
}
//当立方体触碰汽车时,它将向gamecontroller发送消息以结束游戏。
如果(other.gameObject.tag==“障碍物”){
销毁(其他游戏对象);
audiomanager.PlayGameOver();
ScoreManager.Lifes--;
this.gameObject.SetActive(false);
}
如果(other.gameObject.tag==“齿轮”){
销毁(其他游戏对象);
audiomanager.PlayPoint();
ScoreManager.gearsno++;
}
//如果接触到地板,请将其重新定位到顶部
如果(other.gameObject.tag==“Floor”){
this.gameObject.transform.position=新矢量3(this.transform.position.x,5.3f,0);
}
//向左隐形传态
if(other.gameObject.tag==“BridgeLeft”){
传送=真实;
xposition=this.GetComponent().position.x;
浮动新位置=X位置-2.1f;
this.GetComponent().transform.DOMoveX(newpos,1.6f);
如果(this.gameObject.tag==“Car1”)
GameController.Car1CurrentPosition=GameController.Car1CurrentPosition-3;
else if(this.gameObject.tag==“Car2”)
GameController.Car2CurrentPosition=GameController.Car2CurrentPosition-3;
start例程(改变隐形传态(1.6f));

创建一个if语句,允许您也变换对象的位置
如果(other.gameObject.tag==“Floor”){

你能更详细地解释一下你在以什么方式挣扎吗?比如,你的脚本现在正在做什么,以及它如何无法实现你想要实现的目标?这样的信息对于追踪脚本中的问题非常有帮助。你如何将远程传送状态恢复为真。感谢前面的反馈。创建一个public函数将其命名为任何您想要的名称,并返回如下函数(return=)