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

(Solved) Lerp not working, object not moving on contact of trigger.

Discussion in 'Scripting' started by RottenH20, Mar 26, 2022.

  1. RottenH20

    RottenH20

    Joined:
    Sep 6, 2018
    Posts:
    6
    This is the first part of the code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. /*
    6. *  This code is for objects to be pushed by player object.
    7. *  for falling we use FallCheck script for these objects
    8. *  This script just moves the object on X and Z axis if player "pushes" it
    9. *
    10. *  Each child has its on script that sends a "signal" to this script if player is "pushing" it
    11. */
    12.  
    13. public class PushableObject : MonoBehaviour
    14. {
    15.     bool moving = false;
    16.  
    17.     public int moveSpeed = 5;
    18.  
    19.     public void PushObject(GameObject collider)
    20.     {
    21.         if (!moving) // Make sure game object is not already moving
    22.         {
    23.             moving = true;
    24.  
    25.             switch (collider.name)
    26.             {
    27.                 case "xTop":
    28.                     initiateMovablePush(1, 0);
    29.                     break;
    30.                 case "xBottom":
    31.                     initiateMovablePush(-1, 0);
    32.                     break;
    33.                 case "zTop":
    34.                     initiateMovablePush(0, 1);
    35.                     break;
    36.                 case "zBottom":
    37.                     initiateMovablePush(0, -1);
    38.                     break;
    39.                 default:
    40.                     Debug.Log("collider.name");
    41.                     break;
    42.             }
    43.         }
    44.     }
    45.  
    46.     IEnumerator initiateMovablePush(int x, int z) // Same script as fallCheck just going different direction
    47.     {
    48.         Debug.Log("PushObject");
    49.         Vector3 currentPos = gameObject.transform.position;
    50.         Vector3 newPosition = new Vector3(gameObject.transform.position.x + x, gameObject.transform.position.y, gameObject.transform.position.z + z);
    51.         var t = 0f;
    52.         while (t < 1)
    53.         {
    54.             t += Time.deltaTime / moveSpeed;
    55.             gameObject.transform.position = Vector3.Lerp(currentPos, newPosition, t);
    56.             yield return null;
    57.         }
    58.         moving = false;
    59.     }
    60. }
    61.  
    This is the part that calls is


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ColliderPushable : MonoBehaviour
    6. {
    7.     public PushableObject PushableObject;
    8.  
    9.     void OnTriggerEnter(Collider other)
    10.     {
    11.         PushableObject.PushObject(gameObject);
    12.     }
    13. }
    14.  
    The game object is not being moved and no Debug.Logs are being shown in the console. I dont know what I am doing wrong, here are pictures of the scene

    upload_2022-3-26_14-44-29.png
    Each of the "triggers" are the rectangular parts on the box

    upload_2022-3-26_14-45-17.png

    Thank you in advance. I am most likely making a really small/dumb mistake but I have tried almost everything to figure out the reason.
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    Since you are using an "IEnumerator" you have to call it as following:

    Code (CSharp):
    1. StartCoroutine(initiateMovablePush(1, 0));
    Since you need to start the Enumerator somehow :)
     
  3. RottenH20

    RottenH20

    Joined:
    Sep 6, 2018
    Posts:
    6
    I knew I was making some dumb mistake, thank you!
     
    Terraya likes this.