Question

I am making a pokemon clone. It is a top down tile based rpg.

So I added 4 gameobjects on the player itself. Called up, down, left, right. I placed them around the player. I use this set up to check if the gameObject up. Is standing on water. If that is true then the player cannot go up. It is my work around for collision. However my children don't want to follow my parent. All I did was I dragged the 4 for gameobjects onto the player, and added this c# script:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{

    public LayerMask GroundLayer;
    Vector2 startPoint;
    Vector2 endPoint;
    private float increment;
    bool isMoving;
    public float speed;

    public tk2dTileMap tilemap;
    public GameObject up;
    public GameObject down;
    public GameObject left;
    public GameObject right;


    void Start()
    {
        startPoint = transform.position;
        endPoint = transform.position;


    }

    void Update()
    {


        if (increment <= 1 && isMoving == true)
        {
            increment += speed / 100;
        }
        else
        {
            isMoving = false;
        }

        if (isMoving)
        {
            transform.position = Vector2.Lerp(startPoint, endPoint, increment);
        }

        if (Input.GetKey(KeyCode.UpArrow) && isMoving == false)
        {
            if (MovementEvents(up) == 0)
            {
                move(up);
            }
        }
        else if (Input.GetKey(KeyCode.DownArrow) && isMoving == false)
        {
            if (MovementEvents(down) == 0)
            {
                move(down);
            }
        }
        else if (Input.GetKey(KeyCode.LeftArrow) && isMoving == false)
        {       
            if (MovementEvents(left) == 0)
            {
                move(left);
            }
        }
        else if (Input.GetKey(KeyCode.RightArrow) && isMoving == false)
        {
            if (MovementEvents(right) == 0)
            {
                move(right);
            }
        }
    }

    int MovementEvents(GameObject direction)
    {
        switch (tilemap.GetTileIdAtPosition(direction.transform.position, 0)) //gettileIdAtPosition returns the id of a tile in this case 0 is grass and 1 is water
        {
        case 0:
            return 0;
        default:
            return 1;   
        }
    }
    void move(GameObject direction)
    {
        increment = 0;
        isMoving = true;
        startPoint = transform.position;
        endPoint = direction.transform.position;
    }
}

I am using 2d toolkit.

I know it is probaly because i am setting them in the wrong way but what am i doing wrong?

Was it helpful?

Solution

You are creating new GameObjects in your script (up, down, etc.) that have no relation to the objects you created in the editor. There are two solutions to this:

1) You need to assign up, down, etc. to the GameObjects you created in the editor. You can do this by editing your Start method to include:

up = GameObject.Find( "up" );
down = GameObject.Find( "down" );
// etc.

Replacing "up" with whatever you named the up GameObject in the editor.


2) Assign up, down, etc. as children to your parent GameObject using

up.transform.parent   = parentGameObject.transform;
down.transform.parent = parentGameObject.transform;
// etc.

Note that if you go this route, then the up, down, etc. objects you made in the editor will not be used. You also will need to translate the new objects (after setting their parent) or else they will just sit at (0,0,0) relative to the parent.

Edit in response to the comments

If I am understanding the problem correctly, then it seems you are just making a mistake in how you add the up, down, etc. objects as children in the editor. Simply drag them all onto the parent at the same level:

enter image description here

Then whenever the TestParent object moves, the other objects stay at the same relative position to the parent, and thus move as well. Then if you add the GameObjects to a script like so:

enter image description here

You can move the objects individually (while still being relative to the parent). With the following example script, the parent object moves up (Y+) at one unit a second. The Down object also moves up at an additional one unit per second. This results in the ParentTest, Up, Left, Right maintaining formation, moving one unit per second up, while Down moves at two units per second and slowly moves past them.

using UnityEngine;
using System.Collections;

public class DeleteMe : MonoBehaviour 
{
    public GameObject Up;
    public GameObject Down;
    public GameObject Left;
    public GameObject Right;

    void Start( ) 
    {

    }

    // Update is called once per frame
    void Update( ) 
    {
        Move( gameObject );
        Move( Down );
    }

    private void Move( GameObject obj )
    {
        Vector3 position = obj.transform.position;
        position.y += UnityEngine.Time.deltaTime;
        obj.transform.position = position;
    }
}

A screenshot of them at the start:

enter image description here

Another after some time has elapsed:

enter image description here

Finally note that you do not need to have public GameObjects in the script and then drag-and-drop them onto the script to access children. The same result can be achieved by modifying the script to:

    private GameObject Up;
    private GameObject Down;
    private GameObject Left;
    private GameObject Right;

    void Start( ) 
    {
        Up    = transform.Find( "Up" ).gameObject;
        Down  = transform.Find( "Down" ).gameObject;
        Left  = transform.Find( "Left" ).gameObject;
        Right = transform.Find( "Right" ).gameObject;
    }

This prevents the tedious nature of having to drag-and-drop onto the script itself, and also allows you to access children that may be added to the parent dynamically, and thus unable to drag-and-drop.

Hope this has helped!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top