Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 babji3 · Jun 11, 2015 at 10:46 AM · scripting problemgameobjectmousepositiontranslate

Need help for Translating a peg in the game...

Hi ,I am creating a peg solitaire game ,i almost completed,but i need some help how i Translate the peg ,where i clicked.. Here is my code..

     public int Width;
     public int Height;
     public GameObject Peg;
     public GameObject[,] grid ;
     public bool isSelected;
     private GameObject selectedPeg;
     public float duration = 50.0f;
 
     //List<GameObject> myList = new List<GameObject>();
     //List<GameObject> tList = new List<GameObject>();
 
     void Start ()
     {
 
             
         grid = new GameObject[Width, Height];
         for (int x = 0; x < Width; x++)
         {
             for (int y = 0; y < Height; y++)
             {
                 GameObject go =GameObject.Instantiate(Peg) as GameObject;
                 Vector3 position = new Vector3(x, y, 0);
                 go.transform.position = position;
                 grid[x, y] = go;
                 GameObject GridGB = GameObject.Find ("Marbles_GRP");
                 go.transform.parent = GridGB.transform;
 
                 //myList.Add(go);
                 //tList.Add(go);
                 //print(myList.Contains(go));
 
             }
         }
 
         //Down side
         Destroy (grid [0, 0]);//myList.Remove (grid [0, 0]);
         Destroy (grid [0, 1]);//myList.Remove (grid [0, 1]);
         Destroy (grid [0, 5]);//myList.Remove (grid [0, 5]);
         Destroy (grid [0, 6]);//myList.Remove (grid [0, 6]);
         Destroy (grid [1, 0]);//myList.Remove (grid [1, 0]);
         Destroy (grid [1, 1]);//myList.Remove (grid [1, 1]);
         Destroy (grid [1, 5]);//myList.Remove (grid [1, 5]);
         Destroy (grid [1, 6]);//myList.Remove (grid [1, 6]);
         //Up side
         Destroy (grid [6, 0]);//myList.Remove (grid [6, 0]);
         Destroy (grid [6, 1]);//myList.Remove (grid [6, 1]);
         Destroy (grid [6, 5]);//myList.Remove (grid [6, 5]);
         Destroy (grid [6, 6]);//myList.Remove (grid [6, 6]);
         Destroy (grid [5, 0]);//myList.Remove (grid [5, 0]);
         Destroy (grid [5, 1]);//myList.Remove (grid [5, 1]);
         Destroy (grid [5, 5]);//myList.Remove (grid [5, 5]);
         Destroy (grid [5, 6]);//myList.Remove (grid [5, 6]);
         //middle
         Destroy (grid [3, 3]);//myList.Remove (grid [3, 3]);
         //print(myList.Count);
 
     }
 
     void Update ()
     {
         if(Input.GetMouseButtonDown(0))
         {
             SelectPegs();
         }
         if(Input.GetMouseButtonDown(1))
         {
             MovePegs();
         }
 
     }
     void SelectPegs()
     {
         Vector3 mPosition =Camera.main.ScreenToWorldPoint(Input.mousePosition);
         int x = (int)(mPosition.x +0.5f);
         int y = (int)(mPosition.y +0.5f);
 
         for(int _x = 0; _x < Width; _x++)
         {
             for(int _y = 0; _y < Height; _y++)
             {
                 GameObject go = grid[_x, _y];
                 if(go!=null){
                     go.transform.localScale = new Vector2(2f,2f);
                     isSelected=false;
                 }
             }
         }
         
         if(x >=0 && y >=0 && x < Width && y < Height)
         {
              GameObject go = grid[x,y];
             if(go!=null){
                 go.transform.localScale = new Vector2(2.3f,2.3f);
                 isSelected=true;
                 selectedPeg= go;
                 selectedPeg.tag = "SelectedTag";
             }
 
         }
 
     }
     void MovePegs()
     {
         if(isSelected == true)
         {
             //foreach (GameObject obj in myList)
             {
             //if(obj!=null )
                 {
                     Vector2 targetPosition =Camera.main.ScreenToWorldPoint(Input.mousePosition);
                     int _x = (int)(targetPosition.x +0.5f);
                     int _y = (int)(targetPosition.y +0.5f);
                     targetPosition.x=(float)(Mathf.Round(targetPosition.x));
                     targetPosition.y=(float)(Mathf.Round(targetPosition.y));
 
 
                     if(Vector2.Distance(targetPosition,selectedPeg.transform.position)==2.0f)
                     {
                         //selectedPeg.transform.position=targetPosition;
                         //This Line Is not working //
                         selectedPeg.transform.position = Vector2.Lerp(selectedPeg.transform.position, targetPosition, 20/(duration*(Vector2.Distance(selectedPeg.transform.position, targetPosition))));
                         selectedPeg.transform.localScale = new Vector2(2f,2f);
                         isSelected=false;
                         grid[_x, _y] = selectedPeg;
 
                     }
             }
             }
         }
     }
 }

When am Right clicking the peg moves a little instead of translate.Thanks

Comment
Add comment · Show 4
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 babji3 · Jun 12, 2015 at 04:57 AM 0
Share

Here what i generated my pegs on the Board..and i want to Translate the selected peg..Am struggling this one any one help.Thanks. alt text

untitled.png (303.9 kB)
avatar image Lachee1 · Jun 12, 2015 at 06:09 AM 1
Share

You are only lerping once. You need to lerp on update as it will only move you piece a fraction to the target.

avatar image babji3 · Jun 12, 2015 at 11:50 AM 0
Share

where i have to put that line,it is in update only ,i call the function in update..

avatar image babji3 · Jun 13, 2015 at 01:55 PM 0
Share

Any one please.need suggation..

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Addyarb · Jun 13, 2015 at 03:16 PM

At line 25, add something like:

 else{
 MovePegs();
 }

You are only causing the peg to "Lerp" for one frame, which is moving it just a minute distance. You need to constantly check the distance, and keep moving it until it has reached it's destination. Calling the function MovePegs(); while it isn't at it's destination will do that.

Comment
Add comment · Show 4 · 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
avatar image babji3 · Jun 15, 2015 at 06:23 AM 0
Share

thanks for replay, ill check this is out..

avatar image babji3 · Jun 15, 2015 at 06:46 AM 0
Share

No, that one also not working ,it also snapping the peg along with the mouse position while i didn't click the button.any other suggestion...

avatar image Addyarb · Jun 15, 2015 at 09:31 AM 0
Share

Try putting in a new method, like such:

 void $$anonymous$$oveSelectedPeg(GameObject pegTo$$anonymous$$ove, Vector3 targetPosition){
 if(!pegTo$$anonymous$$ove) return; //don't run this method if we don't have a GameObject.
      if(pegTo$$anonymous$$ove.transform.position != targetposition){
      pegTo$$anonymous$$ove.transform.position = Vector3.Slerp(pegTo$$anonymous$$ove.transform.position,targetPosition,Time.deltaTime * 10);
      }
       else{
          return null;
             }
 }

Then call that method in your $$anonymous$$ovePegs method at line 19.

 $$anonymous$$oveSelectedPeg(selectedPeg,targetPosition);


Be sure and put pegTo$$anonymous$$ove and targetPosition as public variables at the top of your script!

Good luck :)

avatar image babji3 · Jun 15, 2015 at 10:30 AM 0
Share

No sir this one also same problem ,it moves only some point distance.. Can i give you this project ,can you check what am doing wrong,this is my first grid system project i taken and am struggling for a while..thanks

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to Instantiate to Mouse Position 0 Answers

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

How to access the properties of any created Game Object from another script Unity C # 2D 1 Answer

Camera switch between child back to main camera issue 2 Answers

Duplicate object and shooting problem (SOLVED) 1 Answer


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