Search Unity

Trigger not sending, or gameobjects already populated?

Discussion in 'Scripting' started by spelafort, Dec 20, 2017.

  1. spelafort

    spelafort

    Joined:
    May 8, 2017
    Posts:
    37
    Hi,

    I'm trying to make a small game where a pig 'eats' coins. I have a prefab with an empty parent (myCoinParent for location), a coin (coin), and another empty (myCoinTarget) that the pig uses to line itself up to the right location. I'm trying to get it so that the trigger will send the object information to the pig's script, which will then be used to zero in on the proper location, trigger the respective animators, and then delete the coin when the animation finishes. As it stands the scripts I have work fine for a single coin, but the script does nothing (or bugs out) after the first one.

    Having made the gameobjects public on the pig (tankcontroller.cs), I can see that the GameObjects are already populated before the trigger. They definitely shouldn't be. I'm really at a loss for what I'm doing wrong, so any help is appreciated.

    (apologies in advance for newbie spaghetti code)

    Code (csharp):
    1.  
    2. public class Collectables : MonoBehaviour {
    3.  
    4.     public int totalCollected;
    5.     public float rotateSpeed = 20000f;
    6.     public TankController myTankController;
    7.  
    8.  
    9.     void Start () {
    10.        
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.         gameObject.transform.Rotate(Vector3.up * Time.deltaTime * rotateSpeed);
    16.        
    17.     }
    18.  
    19.     private void OnTriggerEnter(Collider other)
    20.     {
    21.         SendStuff(gameObject);
    22.     }
    23.    
    24.     public void SendStuff(GameObject coin)
    25.     {
    26.  
    27.             coin = gameObject;
    28.             myTankController.coin = coin;
    29.  
    30.             GameObject myCoinParent = gameObject.transform.parent.gameObject;
    31.             myTankController.myCoinParent = myCoinParent;
    32.  
    33.             GameObject myCoinTarget = gameObject.transform.Find("myCoinTarget").gameObject;
    34.             myTankController.myCoinTarget = myCoinTarget;
    35.        
    36.  
    37.     }
    38.  
    39.  
    40. }
    here's the (much longer and poorly coded) character/animator controller I'm working on:
    Code (csharp):
    1.  
    2. Vector3 targetPosition;
    3.     Vector3 lookAtTarget;
    4.     Vector3 velocityVector;
    5.     public Vector3 transformPosition;
    6.     public Vector3 myCoinTarget_Vector;
    7.     public GameObject myCoinTarget;
    8.     public GameObject coin;
    9.     public GameObject myCoinParent;
    10.     public int totalCollected;
    11.     Quaternion playerRot;
    12.     float rotSpeed = 3f;
    13.     float speed = 0f;
    14.     bool moving = false;
    15.     bool movingConnectEmpties = false;
    16.     public bool connectedEmpties = false;
    17.     bool shootingCoins = false;
    18.     public float offsetMagnitude = 0;
    19.     float runDistance = 5f;
    20.     public Animator myCoinAnimator;
    21.     public Animator myPigAnimator;
    22.     public string animClipName;
    23.     float animCurrentClipLength;
    24.     AnimatorClipInfo[] animCurrentClipInfo;
    25.     // Use this for initialization
    26.     void Start () {
    27.  
    28.    
    29.     }
    30.     // Update is called once per frame
    31.     void Update()
    32.     {
    33.         if(coin != null)
    34.         {
    35.             myCoinTarget_Vector = myCoinTarget.transform.position;
    36.         }
    37.         myPigAnimator.SetFloat("offsetMagnitude", offsetMagnitude);
    38.         transformPosition = transform.position;
    39.         AnimatorInfo();
    40.         if (Input.GetMouseButton(0))
    41.         {
    42.             myPigAnimator.SetTrigger("walk");
    43.             SetTargetPosition();
    44.         }
    45.         else if (moving == true)
    46.         {
    47.             Move();
    48.         }
    49.         else if (moving == false && movingConnectEmpties == true && connectedEmpties == false)
    50.         {
    51.             SetCoinPosition();
    52.             ConnectEmpties();
    53.         }
    54.         else if (totalCollected >= 5)
    55.         {
    56.             moving = false;
    57.             movingConnectEmpties = false;
    58.             connectedEmpties = false;
    59.             myPigAnimator.SetTrigger("explode");
    60.         }
    61.     }
    62.     void SetTargetPosition()
    63.     {
    64.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    65.         RaycastHit hit;
    66.         if (Physics.Raycast(ray, out hit, 1000))
    67.         {
    68.             targetPosition = hit.point;
    69.             //this.transform.LookAt(targetPosition);
    70.             lookAtTarget = new Vector3(targetPosition.x - transform.position.x,
    71.                 transform.position.y,
    72.                 targetPosition.z - transform.position.z);
    73.             playerRot = Quaternion.LookRotation(lookAtTarget);
    74.             myPigAnimator.SetTrigger("walk");
    75.             moving = true;
    76.             offsetMagnitude = new Vector3(targetPosition.x - transform.position.x, targetPosition.x - transform.position.y, targetPosition.z - transform.position.z).magnitude;
    77.         }
    78.     }
    79.     void Move()
    80.     {
    81.         if(offsetMagnitude >= 10f)
    82.         {
    83.             speed = 4f;
    84.             print("Speed: " + speed);
    85.         }
    86.         else if(offsetMagnitude < 10f)
    87.         {
    88.             speed = 2f;
    89.             print("Speed: " + speed);
    90.         }
    91.         transform.rotation = Quaternion.Slerp(transform.rotation, playerRot, rotSpeed * Time.deltaTime);
    92.         velocityVector = Vector3.MoveTowards(transform.position, targetPosition, speed * Time.deltaTime);
    93.         transform.position = velocityVector;
    94.         if(transformPosition == targetPosition)
    95.         {
    96.             moving = false;
    97.             offsetMagnitude = 0;
    98.         }
    99.     }
    100.     public void AnimatorInfo()
    101.     {
    102.         //Fetch the current Animation clip information for the base layer
    103.         animCurrentClipInfo = this.myPigAnimator.GetCurrentAnimatorClipInfo(0);
    104.         //Access the current length of the clip;
    105.         animCurrentClipLength = animCurrentClipInfo[0].clip.length;
    106.         //Access the Animation clip name
    107.         animClipName = animCurrentClipInfo[0].clip.name;
    108.     }
    109.    void OnTriggerEnter(Collider col)
    110.     {
    111.         //col.gameObject.GetComponent<Collectables>().SendStuff(coin);
    112.         movingConnectEmpties = true;
    113.         moving = false;
    114.     }
    115.     void SetCoinPosition()
    116.     {
    117.         lookAtTarget = new Vector3(myCoinTarget_Vector.x - transform.position.x,
    118.             transform.position.y,
    119.             myCoinTarget_Vector.z - transform.position.z);
    120.         playerRot = Quaternion.LookRotation(lookAtTarget);
    121.        
    122.         myPigAnimator.SetTrigger("walk");
    123.         offsetMagnitude = new Vector3(myCoinTarget_Vector.x - transform.position.x, myCoinTarget_Vector.x - transform.position.y, myCoinTarget_Vector.z - transform.position.z).magnitude;
    124.         print("Offset magnitude is :" + offsetMagnitude);
    125.      
    126.     }
    127.     void ConnectEmpties()
    128.     {
    129.         if (offsetMagnitude >= 10f)
    130.         {
    131.             speed = 4f;
    132.             print("Speed: " + speed);
    133.         }
    134.         else if (offsetMagnitude < 10f)
    135.         {
    136.             speed = 2f;
    137.             print("Speed: " + speed);
    138.         }
    139.         transform.rotation = Quaternion.Slerp(transform.rotation, playerRot, rotSpeed * Time.deltaTime);
    140.         velocityVector = Vector3.MoveTowards(transform.position, myCoinTarget_Vector, speed * Time.deltaTime);
    141.         transform.position = velocityVector;
    142.         if (transform.position == myCoinTarget_Vector)
    143.         {
    144.             myCoinAnimator = coin.GetComponent<Animator>();
    145.             moving = false;
    146.             movingConnectEmpties = false;
    147.             connectedEmpties = true;
    148.             speed = 0;
    149.             //myPigAnimator.SetTrigger("idle");
    150.             myPigAnimator.SetTrigger("snort");
    151.            
    152.             myCoinAnimator.SetBool("suck", true);
    153.             Destroy(myCoinParent, 1.3f);
    154.             connectedEmpties = false;
    155.         }
    156.     }
    157. }
    158.  
    any help would be appreciated!
     
  2. spelafort

    spelafort

    Joined:
    May 8, 2017
    Posts:
    37
    update: I have three coins in my scene right now, and I've realized that if I approach either of the latter two, the pig will still run back to the location of the first one. This occurs even after the first coin is destroyed. But the first script above (Collectables) is on the prefab itself, so I have no idea what's causing this :(