Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by unity_pFb9mvTB5gzvDA · Sep 27, 2021 at 02:32 PM · transformtranslateobjects-movement

object translate issue

so I am trying to make a turn based strategy game. currently I can select the character and yellow squares appear to where it can move. but when I click on a yellow square the character does not go to the square it goes somewhere else. if I use the same function to move the character to a specific spot then he goes to 15x0y. if I select him again and move to a spot 5 units are added to the x value. I don't know why this is happening. I tried a different approach that broke it even further. basically the character is not going where it is supposed to.

Attached to the yellow squares:

 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;
         }
 
     }
 }

Attached to the character

 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
         }
     }
 }
 


Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Captain_Pineapple · Sep 28, 2021 at 09:18 AM 0
Share

On first glance i cannot see any issue with your movement code but to make this easier please please simplify your code.

this:

 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;
                  }

can be written as this:

 if (transform.position.x + i < 20)
 {
        Instantiate(movePlate, new Vector3(transform.position.x + i, transform.position.y, -2) , Quaternion.identity).GetComponent<MovePlate>().SetReference(this.gameObject);
 }

If you should ever do more with the move plates on instantiation you can ofc always cache that locally again but there is no need for all those member variables mp1-8.


Most imprtantly though: From what i can see you instantiate the movementplates every frame since you never reset selected. Those plates must be piling up pretty high in your scene... Make sure that you get rid of this and only instantiate them once. Best to do that when you set selected = true. This way you know that this only happens once on left-click.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by unity_pFb9mvTB5gzvDA · Sep 29, 2021 at 09:52 AM

I fixed it!!!! In the moveplate code I changed "reference.transform.Translate(x,y,-1);" to "reference.transform.SetPositionAndRotation(new Vector3(x, y, -1.0f), Quaternion.identity);"

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

164 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Moving a GameObject to a certain point in world space via script 1 Answer

Rigidbody Sink into BoxColider 0 Answers

Moving character on x axis 1 Answer

Gameobject.transform.Translate not working 1 Answer

Vector3 Returning Infinity Error 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges