Search Unity

[SOLVED]My Parenting Clones Controlling Problem

Discussion in 'Scripting' started by LightningGravity, Aug 17, 2019.

  1. LightningGravity

    LightningGravity

    Joined:
    May 29, 2019
    Posts:
    26
    Here I am trying to make the AI parent and child cloning the blocking rock walls in my own video game. So far there is the parent PerRockClone and the cloning child Rock area in the Instantiate cloning method seems to have a parenting clones issue at the same timing when I have pressed the holding buttons. The parenting cloning issues seems like I cannot change the groups of clones properly at the same timing to move it all properly manners at once meanwhile I am cloning it more at the same time. Also each times I have clone more the PerRockClone parent seems to always like double the Rock child traveling speed movement when I am keep on cloning the child it more.
    In the end I think I just need to figure out how to refresh and restart and then gather the parent PerRockClone in the new group of parent instead to clone and move the child Rock more properly.
    But just keep in your mind that this video game has a phone app button video game instead of a desktop keyboard video game.
    Please, can anyone help me about the parenting clones controlling it better. Thanks.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Script_SpawnRock : MonoBehaviour
    6. {
    7.  
    8.     public GameObject Rock;
    9.     public GameObject PerRockClone;
    10.  
    11.     public GameObject ShipObject;
    12.     Script_ShipTurn ShipTravel;
    13.     public GameObject RightButton;
    14.     Script_ShipRight pressedRight;
    15.     public GameObject LeftButton;
    16.     Script_ShipLeft pressedLeft;
    17.  
    18.     // Start is called before the first frame update
    19.     void Start()
    20.     {
    21.         ShipTravel = ShipObject.GetComponent<Script_ShipTurn>();
    22.         pressedRight = RightButton.GetComponent<Script_ShipRight>();
    23.         pressedLeft = LeftButton.GetComponent<Script_ShipLeft>();
    24.  
    25.         float spawnTime = Random.Range(0.001f, 0.01f);
    26.         InvokeRepeating("RockSpawnPointer", spawnTime, 0.1f);
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.  
    33.     }
    34.  
    35.     void RockSpawnPointer()
    36.     {
    37.         if (ShipTravel.GroundSpeed == 0 && pressedRight.pointerDown && !pressedLeft.pointerDown)
    38.         {
    39.             float xPos = Random.Range(-37.5f, 37.5f);
    40.             PerRockClone = Instantiate(Rock, new Vector3(xPos, -0.5f, 37.5f), Quaternion.identity);
    41.         }
    42.         else if (ShipTravel.GroundSpeed == 0 && pressedLeft.pointerDown && !pressedRight.pointerDown)
    43.         {
    44.             float xPos = Random.Range(-37.5f, 37.5f);
    45.             PerRockClone = Instantiate(Rock, new Vector3(xPos, -0.5f, 37.5f), Quaternion.identity);
    46.         }
    47.         else
    48.         {
    49.             // do nothing
    50.         }
    51.     }
    52.  
    53.     public void RockRight()
    54.     {
    55.         PerRockClone.transform.Translate(-0.25f * Time.time, 0f, 0f, Space.World);
    56.     }
    57.     public void RockLeft()
    58.     {
    59.         PerRockClone.transform.Translate(0.25f * Time.time, 0f, 0f, Space.World);
    60.     }
    61.  
    62. }
     
    Last edited: Aug 17, 2019
  2. LightningGravity

    LightningGravity

    Joined:
    May 29, 2019
    Posts:
    26
    I have tried this YouTube video one that are trying to create more like a creating game object instantly without any parenting Instantiate cloning the children too much at the same time. But it seems like almost the same problem as well but it seems a bit better at least. I just only create an empty game object then add in every single components including the 3D shape mesh, but it seems to keep on adding up speed only instead which are still much better than the last time.




    Thank God. I am finish with that hardest AI. I just need to use the Time.deltaTime instead of Time.time.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Script_Rock : MonoBehaviour
    6. {
    7.  
    8.     float MoveRightX = 0f;
    9.     float MoveLeftX = 0f;
    10.     float MoveBackZ = 0f;
    11.     float Speed = 6f;
    12.  
    13.     GameObject ShipGround;
    14.     Script_ShipTurn ShipSpeed;
    15.     GameObject RightButton;
    16.     Script_ShipRight pressedRight;
    17.     GameObject LeftButton;
    18.     Script_ShipLeft pressedLeft;
    19.  
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.         ShipGround = GameObject.Find("GameObject_Ship");
    24.         RightButton = GameObject.Find("Button_Right");
    25.         LeftButton = GameObject.Find("Button_Left");
    26.  
    27.         ShipSpeed = ShipGround.GetComponent<Script_ShipTurn>();
    28.         pressedRight = RightButton.GetComponent<Script_ShipRight>();
    29.         pressedLeft = LeftButton.GetComponent<Script_ShipLeft>();
    30.  
    31.         float rotateZ = Random.Range(0f, 360f);
    32.         this.transform.Rotate(new Vector3(-90f, 0f, rotateZ));
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.         MoveRock();
    39.         if (this.transform.position.z <= -10)
    40.         {
    41.             Destroy(this.gameObject);
    42.         }
    43.     }
    44.  
    45.     public void MoveRock()
    46.     {
    47.         if (ShipSpeed.GroundSpeed == 0 && pressedRight.pointerDown && !pressedLeft.pointerDown)
    48.         {
    49.             MoveBackZ = 0f;
    50.             MoveRightX = Time.deltaTime * 7.6f;
    51.             this.transform.Translate(new Vector3(MoveLeftX - MoveRightX, 0f, -MoveBackZ), Space.World);
    52.         }
    53.         else if (ShipSpeed.GroundSpeed == 0 && pressedLeft.pointerDown && !pressedRight.pointerDown)
    54.         {
    55.             MoveBackZ = 0f;
    56.             MoveLeftX = Time.deltaTime * 7.6f;
    57.             this.transform.Translate(new Vector3(MoveLeftX - MoveRightX, 0f, -MoveBackZ), Space.World);
    58.         }
    59.         else if (ShipSpeed.GroundSpeed > 0 && pressedRight.pointerDown && !pressedLeft.pointerDown)
    60.         {
    61.             MoveRightX = Time.deltaTime * 7.6f;
    62.             MoveBackZ = Time.deltaTime * 7.6f;
    63.             this.transform.Translate(new Vector3(MoveLeftX - MoveRightX, 0f, -MoveBackZ), Space.World);
    64.         }
    65.         else if (ShipSpeed.GroundSpeed > 0 && pressedLeft.pointerDown && !pressedRight.pointerDown)
    66.         {
    67.             MoveLeftX = Time.deltaTime * 7.6f;
    68.             MoveBackZ = Time.deltaTime * 7.6f;
    69.             this.transform.Translate(new Vector3(MoveLeftX - MoveRightX, 0f, -MoveBackZ), Space.World);
    70.         }
    71.         else if (ShipSpeed.GroundSpeed > 0 && !pressedRight.pointerDown && !pressedLeft.pointerDown)
    72.         {
    73.             MoveRightX = 0;
    74.             MoveLeftX = 0;
    75.             MoveBackZ = Time.deltaTime * 7.6f;
    76.             this.transform.Translate(new Vector3(MoveLeftX - MoveRightX, 0f, -MoveBackZ), Space.World);
    77.         }
    78.         else if (ShipSpeed.GroundSpeed > 0 && pressedRight.pointerDown && pressedLeft.pointerDown)
    79.         {
    80.             MoveRightX = 0;
    81.             MoveLeftX = 0;
    82.             MoveBackZ = Time.deltaTime * 7.6f;
    83.             this.transform.Translate(new Vector3(MoveLeftX - MoveRightX, 0f, -MoveBackZ), Space.World);
    84.         }
    85.         else
    86.         {
    87.             // do nothing
    88.         }
    89.     }
    90.  
    91. }
    Here done one of the part only.
     
    Last edited: Aug 20, 2019