Search Unity

Lerp based off of key input and bool from another script

Discussion in 'Scripting' started by DigitalOddity, Mar 18, 2019.

  1. DigitalOddity

    DigitalOddity

    Joined:
    Oct 8, 2018
    Posts:
    10
    A while ago I set up a platform in my game that lerps upon a keypress. However, I wanted it to also happen only if a bool was activated true by doing another action so if key press and bool true then lerp. I managed to set everything up correctly however when I tried finding the game object and its script and pulling the bool for the activation it is not working.

    Here is the bool code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class spikeRunInto : MonoBehaviour
    6. {
    7.     public bool bloodDrawn;
    8.     void onStart()
    9.     {
    10.         bloodDrawn = false;
    11.     }
    12.     void OnTriggerEnter(Collider col)
    13.     {
    14.         if (col.gameObject.tag == "spikes")
    15.         {
    16.             bool bloodDrawn = true;
    17.             Debug.Log(bloodDrawn);
    18.         }
    19.     }
    20. }
    21.  
    Lerp code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class platfromraiser2 : MonoBehaviour
    6. {
    7.     Vector3 start1 = new Vector3(243, -300, 139);
    8.     Vector3 end1 = new Vector3(243, -42, 139);
    9.     bool detect;
    10.  
    11.     // Movement speed in units/sec.
    12.     public float speed2 = 20.0f;
    13.  
    14.     // Time when the movement started.
    15.     private float startTime;
    16.  
    17.     // Total distance between the markers.
    18.     private float journeyLength;
    19.     // Start is called before the first frame update
    20.     void Start()
    21.     {
    22.         // Keep a note of the time the movement started.
    23.         startTime = Time.time * 200;
    24.  
    25.         // Calculate the journey length.
    26.         journeyLength = Vector3.Distance(start1, end1);
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     private void Update()
    31.     {
    32.  
    33.         if (GameObject.Find("FPSController/FirstPersonCharacter").GetComponent<spikeRunInto>().bloodDrawn == true &&  Input.GetKeyDown(KeyCode.Return))
    34.         {
    35.         // Distance moved = time * speed.
    36.         float distCovered = (Time.time - startTime) * speed2 * 10;
    37.  
    38.             // Fraction of journey completed = current distance divided by total distance.
    39.             float fracJourney = distCovered / journeyLength;
    40.  
    41.             // Set our position as a fraction of the distance between the markers.
    42.             transform.position = Vector3.Lerp(start1, end1, fracJourney);
    43.             Debug.Log("bye");
    44.         }
    45.     }
    46. }
    47.  
    48.  
     
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    There are a few issues with your code here.

    GetKeyDown will return true for one frame when the key specified is pressed so you lerp code only runs for one frame.

    GameObject.Find and GetComponent are slow methods and should really only be used when need be, not every frame. So cahce a reference to spikerun in your start method or add an even to spikerun and subscribe to it in you platform class.

    Your startTime should be set when the player starts to move the platform and you certainly don't want to be multipling Time.time by any number.

    You also want a way to detect if the player is on the platform, OnTriggerEnter and Exit could be used for that. If you don't know when the player is on the platform, it would just start anytime Enter is pressed and your bool is true.
     
  3. DigitalOddity

    DigitalOddity

    Joined:
    Oct 8, 2018
    Posts:
    10
    Could you provide an example of using the Start to cache a reference? I understand how to do it but only without having to search for another script and find a certain thing such as that bool in that script. For example, I know how to store a simple Game.Object.Find, not when I need to find the script and a component of it. Second, ignoring key down the bool isn't activating the platforms, key down by itself works but if I try using just the boolean to activate it refuses to do anything. Using the Debug.Log I can see that the bool is being set to true but for whatever reason, it is not making the lerp run. I am using a bool because you are supposed to make a sacrifice in the game before being allowed to raise the platforms, I do not need to be doing any on platform checking.