Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Help on making GameObjects moves with Input

Discussion in '2D' started by richardthere, Jul 19, 2016.

  1. richardthere

    richardthere

    Joined:
    Mar 13, 2015
    Posts:
    19
    Hi everyone,
    sorry if this question is to specific but I am trying to figured it out for a long time and I can't come to a result.
    I'm learning C# so everytime that I have an idea for my game I have to find out a way to do it.
    So I'll tell you the idea and maybe you could help to find a way to do it.

    My Player collides with a tree and all the apples from this tree fall on the ground. (this is already working)
    Then the Player goes to a machine where he can controll some forces moving the apples up, down, left or right. When the Player touchs the machine I desactivate the Player Controls script and activate a new script to control this forces. This new script is now moving the apples in a random way just to show the Player that now he can control the apples. (this is also already working.)

    Now I am a bit lost on how can I make the player inputs affects the apples. Could you suggest me the best way to implement this? Is a Foreach loop the right way to do that?

    Here is the script that I wrote.

    Code (CSharp):
    1. public class SonhoController : MonoBehaviour {
    2.     public GameObject[] macas;
    3.    
    4.     void Start () {
    5.             macas = GameObject.FindGameObjectsWithTag("Maca");
    6.     }
    7.  
    8.     void FixedUpdate() {
    9.         foreach (GameObject Maca in macas)
    10.         {
    11.             Vector2 pos = Maca.transform.position;
    12.             Vector2 r = new Vector2(Random.Range(-.2f, .2f),Random.Range(-.2f,.2f));
    13.             Maca.transform.position = pos + r;
    14.         }
    15.     }
    16. }
     
  2. richardthere

    richardthere

    Joined:
    Mar 13, 2015
    Posts:
    19
    Sorry, I found out the way to do it. Now I have a new problem. :)
    The Player can control the apples now but they can go everywhere.
    How would be the best way to implement some limits for this?

    here is my new script:

    Code (CSharp):
    1. public class SonhoController : MonoBehaviour {
    2.     public GameObject[] macas;
    3.      
    4.     void Start () {
    5.             macas = GameObject.FindGameObjectsWithTag("Maca");
    6.     }
    7.  
    8.     void FixedUpdate() {
    9.         float moveX = Input.GetAxis("Horizontal");
    10.         float moveY = Input.GetAxis("Vertical");
    11.         Vector2 moveXY = new Vector2(moveX, moveY);
    12.  
    13.         foreach (GameObject Maca in macas)
    14.         {
    15.             Vector2 pos = Maca.transform.position;
    16.             Maca.transform.position = pos + moveXY;
    17.         }
    18.     }
    19. }
     
    Last edited: Jul 19, 2016
  3. capnjake

    capnjake

    Joined:
    Nov 12, 2013
    Posts:
    53
    Do you just want to avoid the apples from going to high or low? This can be achieved by adding a new variable and checking the objects location before doing anything.

    Code (csharp):
    1.  
    2. public class SonhoController : MonoBehaviour
    3. {
    4.     public GameObject[] macas;
    5.     private const float Y_LIMIT = 4; // This is the limiting height
    6.    
    7.     void Start()
    8.     {
    9.         macas = GameObject.FindGameObjectsWithTag("Maca");
    10.     }
    11.  
    12.     void FixedUpdate()
    13.     {
    14.         float moveX = Input.GetAxis("Horizontal");
    15.         float moveY = Input.GetAxis("Vertical");
    16.         Vector2 moveXY = new Vector2(moveX, moveY);
    17.    
    18.         foreach (GameObject Maca in macas)
    19.         {
    20.             Vector2 pos = Maca.transform.position;
    21.  
    22.             // This will decide if the apple is above or below the limit
    23.             if (pos.y >= Y_LIMIT || pos.y <= -Y_LIMIT)
    24.                 continue; // If it is, simply move along to the next apple
    25.  
    26.             Maca.transform.position = pos + moveXy;
    27.         }  
    28.     }
    29. }
    30.  
     
    richardthere likes this.
  4. richardthere

    richardthere

    Joined:
    Mar 13, 2015
    Posts:
    19
    Thank's for your reply. This is also working now. But I have a new problem. ;) it's endless...
    I am moving the apples using the transform properties and because of this the collisions are not very accurate.
    Now I am trying to move the apples using their rigidbodies2d and adding force to it. But I can't make an array of rigidbodies. Could you suggest me how could I get this array done? And then the best way to apply forces related to the input on each member of this array?
     
  5. richardthere

    richardthere

    Joined:
    Mar 13, 2015
    Posts:
    19
    And again I found the solution. This looks more like an newbie dev blog!
    and here the code if anyone needs:
    Code (CSharp):
    1. public class SonhoController : MonoBehaviour
    2. {
    3.     public GameObject[] macas;
    4.     public float maxspeed = 5;
    5.     public int nextArray = 0;
    6.  
    7.     void Start()
    8.     {
    9.         macas = GameObject.FindGameObjectsWithTag("Maca");
    10.  
    11.         Debug.Log(macas.Length);
    12.  
    13.     }
    14.  
    15.     void FixedUpdate()
    16.     {
    17.         float moveX = Input.GetAxis("Horizontal");
    18.         float moveY = Input.GetAxis("Vertical");
    19.         Vector2 moveXY = new Vector2(moveX, moveY);
    20.         Debug.Log(nextArray);
    21.  
    22.         if (nextArray < macas.Length)
    23.         {
    24.             Rigidbody2D r = macas[nextArray].GetComponent<Rigidbody2D>();
    25.             //apply a force
    26.             r.velocity = moveXY * maxspeed;
    27.  
    28.             //move onto the next object
    29.             nextArray++;
    30.         }
    31.  
    32.         if (nextArray == macas.Length)
    33.         {
    34.             nextArray = 0;
    35.         }
    36.     }
    37. }
     
  6. capnjake

    capnjake

    Joined:
    Nov 12, 2013
    Posts:
    53
    What do you mean you cannot get the `Rigidbody2D` as an array? You should be able to declare an array of Rigidbody2D like you do you `GameObject` array. Or do you mean populating the same way using something like `macas = GameObject.FindGameObjectsWithTag("Maca");`?

    In that case you are probably better off using a List instead.

    Code (csharp):
    1. using System.Collections.Generic;
    2.  
    3. private List<Rigidbody2D> macasRB = new List<Rigidbody2D>();
    4.  
    5. private void Start()
    6. {
    7.     GameObject[] macas = GameObject.FindGameObjectsWithTag("Maca");
    8.     foreach (GameObject Maca in macas)
    9.     {
    10.         macasRB.Add(Maca.GetComponent<Rigibody2D>());
    11.     }
    12. }
    13.  
    This method probably isn't the best if you have a lot of apples and aren't calling the it at the start of your game. You can convert the `List` easily to an `Array` if you would rather have an array as well.
     
  7. richardthere

    richardthere

    Joined:
    Mar 13, 2015
    Posts:
    19
    Thank's. This is almost what I did.
    Here is my new code:
    Code (CSharp):
    1. public class SonhoController : MonoBehaviour
    2. {
    3.     public GameObject[] macas;
    4.     public float maxspeed = 5;
    5.     public int nextArray = 0;
    6.  
    7.     void Start()
    8.     {
    9.        macas = GameObject.FindGameObjectsWithTag("Maca");
    10.     }
    11.  
    12.     void FixedUpdate()
    13.     {
    14.         float moveX = Input.GetAxis("Horizontal");
    15.         float moveY = Input.GetAxis("Vertical");
    16.         Vector2 moveXY = new Vector2(moveX, moveY);
    17.  
    18.         if (nextArray < macas.Length)
    19.         {
    20.             Rigidbody2D r = macas[nextArray].GetComponent<Rigidbody2D>();
    21.             //apply a force
    22.             r.velocity = moveXY * maxspeed;
    23.  
    24.             //move onto the next object
    25.             nextArray++;
    26.         }
    27.  
    28.         if (nextArray == macas.Length)
    29.         {
    30.             nextArray = 0;
    31.         }
    32.     }
    33. }