首页 > 解决方案 > 即使我对特定位置进行硬编码,角色也会移动到错误的位置

问题描述

所以我正在尝试制作一个回合制策略游戏。目前我可以选择角色,黄色方块出现在它可以移动的位置。但是当我点击一个黄色方块时,角色并没有走到方块上,而是去到了其他地方。如果我使用相同的功能将角色移动到特定位置,那么他会转到 15x0y。如果我再次选择他并移动到某个点,则 x 值会增加 5 个单位。我不知道为什么会这样。我尝试了一种不同的方法,进一步打破了它。基本上这个角色没有去它应该去的地方。

附在黄色方块上:

`     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class MovePlate : MonoBehaviour
     {
         GameObject controller;
         public GameObject reference = null;
         public float x;
         public float y;
         // Start is called before the first frame update
         void Start()
         {
             x = this.transform.position.x;
             y = this.transform.position.y;
             
         }
     
         // Update is called once per frame
         void Update()
         {
             
         }
     
         public void SetReference(GameObject obj)
         {
             reference = obj;
         }
         public GameObject GetReference()
         {
             return reference;
         }
         public void OnMouseOver()
         {
             if(Input.GetKey(KeyCode.Mouse0))
             {
                 reference.transform.Translate(x,y,-1);
                 reference.GetComponent<units>().selected = false;
             }
     
         }
     }

附在人物身上

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class units : MonoBehaviour
 {
     public GameObject Select;
     public GameObject movePlate;
     public GameObject tempselect;
     GameObject mp1;
     GameObject mp2;
     GameObject mp3;
     GameObject mp4;
     GameObject mp5;
     GameObject mp6;
     GameObject mp7;
     GameObject mp8;
 
     public int movement;
     public bool selected;
     private bool mouseover;
     // Start is called before the first frame update
     void Start()
     {
         
         selected = false;
         mouseover = false;
     }
 
     // Update is called once per frame
     void Update()
     {
         if (selected)
         {
             for (int i = 1; i < movement + 1; i++)
             {
                 Debug.Log("Bacon");
                 if (this.gameObject.transform.position.x + i < 20)
                 {
                     mp1 = Instantiate(movePlate, new Vector3(this.gameObject.transform.position.x + i, this.gameObject.transform.position.y, -2), Quaternion.identity);
                     mp1.GetComponent<MovePlate>().SetReference(this.gameObject);
                     mp1.GetComponent<MovePlate>().x = this.gameObject.transform.position.x + i;
                     mp1.GetComponent<MovePlate>().y = this.gameObject.transform.position.y;
                 }
                 if (this.gameObject.transform.position.x + i < 20 && this.gameObject.transform.position.y + i < 20)
                 {
                     mp2 = Instantiate(movePlate, new Vector3(this.gameObject.transform.position.x + i, this.gameObject.transform.position.y + i, -2), Quaternion.identity);
                     mp2.GetComponent<MovePlate>().SetReference(this.gameObject);
                     mp2.GetComponent<MovePlate>().x = this.gameObject.transform.position.x + i;
                     mp2.GetComponent<MovePlate>().y = this.gameObject.transform.position.y + i;
                 }
                 if (this.gameObject.transform.position.y + i < 20)
                 {
                     mp3 = Instantiate(movePlate, new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y + i, -2), Quaternion.identity);
                     mp3.GetComponent<MovePlate>().SetReference(this.gameObject);
                     mp3.GetComponent<MovePlate>().x = this.gameObject.transform.position.x;
                     mp3.GetComponent<MovePlate>().y = this.gameObject.transform.position.y + i;
                 }
                 if (this.gameObject.transform.position.x - i >= 0 && this.gameObject.transform.position.y + i < 20)
                 {
                     mp4 = Instantiate(movePlate, new Vector3(this.gameObject.transform.position.x - i, this.gameObject.transform.position.y + i, -2), Quaternion.identity);
                     mp4.GetComponent<MovePlate>().SetReference(this.gameObject);
                     mp4.GetComponent<MovePlate>().x = this.gameObject.transform.position.x - i;
                     mp4.GetComponent<MovePlate>().y = this.gameObject.transform.position.y + i;
                 }
                 if (this.gameObject.transform.position.x - i >= 0)
                 {
                     mp5 = Instantiate(movePlate, new Vector3(this.gameObject.transform.position.x - i, this.gameObject.transform.position.y, -2), Quaternion.identity);
                     mp5.GetComponent<MovePlate>().SetReference(this.gameObject);
                     mp5.GetComponent<MovePlate>().x = this.gameObject.transform.position.x - i;
                     mp5.GetComponent<MovePlate>().y = this.gameObject.transform.position.y;
                 }
                 if (this.gameObject.transform.position.x + 1 < 20 && this.gameObject.transform.position.y - i >= 0)
                 {
                     mp6 = Instantiate(movePlate, new Vector3(this.gameObject.transform.position.x + i, this.gameObject.transform.position.y - i, -2), Quaternion.identity);
                     mp6.GetComponent<MovePlate>().SetReference(this.gameObject);
                     mp6.GetComponent<MovePlate>().x = this.gameObject.transform.position.x + i;
                     mp6.GetComponent<MovePlate>().y = this.gameObject.transform.position.y - i;
                 }
                 if (this.gameObject.transform.position.y - i >= 0)
                 {
                     mp7 = Instantiate(movePlate, new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y - i, -2), Quaternion.identity);
                     mp7.GetComponent<MovePlate>().SetReference(this.gameObject);
                     mp7.GetComponent<MovePlate>().x = this.gameObject.transform.position.x;
                     mp7.GetComponent<MovePlate>().y = this.gameObject.transform.position.y - i;
                 }
                 if (this.gameObject.transform.position.x - i >= 0 && this.gameObject.transform.position.y - i >= 0)
                 {
                     mp8 = Instantiate(movePlate, new Vector3(this.gameObject.transform.position.x - i, this.gameObject.transform.position.y - i, -2), Quaternion.identity);
                     mp8.GetComponent<MovePlate>().SetReference(this.gameObject);
                     mp8.GetComponent<MovePlate>().x = this.gameObject.transform.position.x - i;
                     mp8.GetComponent<MovePlate>().y = this.gameObject.transform.position.y - i;
                 }
 
             }
             
         }
         
         if(selected && Input.GetKey(KeyCode.Mouse1))
         {
             
             selected = false;
         }
         if(!selected)
         {
             DestroyMovePlates();
         }
     }
 
     void OnMouseOver()
     {
         if (Input.GetKey(KeyCode.Mouse0)&&!selected)
         {
             tempselect = Instantiate(Select, new Vector3(this.gameObject.transform.position.x, this.gameObject.transform.position.y, -3), Quaternion.identity);
             selected = true;
         }
     }
     public void DestroyMovePlates()
     {
         //Destroy old MovePlates
         GameObject[] movePlates = GameObject.FindGameObjectsWithTag("MovePlate");
         for (int i = 0; i < movePlates.Length; i++)
         {
             Destroy(movePlates[i]); //Be careful with this function "Destroy" it is asynchronous
         }
     }
 }
 

标签: c#unity3dpositiontransformgameobject

解决方案


我修好了它!!!!在移动板代码中,我更改了“reference.transform.Translate(x,y,-1);” 到“reference.transform.SetPositionAndRotation(new Vector3(x, y, -1.0f), Quaternion.identity);”


推荐阅读