即使我对特定位置进行硬编码,角色也会移动到错误的位置

Posted

技术标签:

【中文标题】即使我对特定位置进行硬编码,角色也会移动到错误的位置【英文标题】:the character is moving to the wrong place even when I hard code a specific location 【发布时间】:2021-11-19 13:00:10 【问题描述】:

所以我正在尝试制作一个回合制策略游戏。目前我可以选择角色并且黄色方块出现在它可以移动的位置。但是当我点击一个黄色方块时,角色并没有走到方块上,而是去到了其他地方。如果我使用相同的功能将角色移动到特定位置,那么他会转到 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
         
     
 
 

【问题讨论】:

不看你的代码:你的对象是任何东西的孩子吗?您是否可能必须使用localPosition? 据我所知,它不是任何事物的孩子。我仍然可以尝试 localposition 看看是否有帮助。 【参考方案1】:

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

【讨论】:

以上是关于即使我对特定位置进行硬编码,角色也会移动到错误的位置的主要内容,如果未能解决你的问题,请参考以下文章

对ASP.NET MVC的角色(外部数据库)进行硬编码并添加Windows Authenticated Users

如何读取具有动态名称的文件,同时避免在 R 中进行硬编码?

即使设备没有移动,位置也会不断变化

即使我已显着移动,也会在后台更新相同的位置

即使 IAM 角色具有完整的 Redshift 权限,AWS Lambda 在调用 Redshift 的“CreateCluster”操作时也会出现“拒绝访问”错误

合并两个列表:我对两个列表进行了硬编码,并希望打印条件匹配的两个列表数据。请让我知道我该怎么做?