Search Unity

How to include speed trigger creation option in this script.

Discussion in 'Scripting' started by akshayjose07, Feb 21, 2020.

  1. akshayjose07

    akshayjose07

    Joined:
    Mar 9, 2018
    Posts:
    2
    I am working on a VR Roller Coaster project.
    Currently, roller coaster object has a constant speed that specified in the script.
    I want to make 3-speed triggers such as
    • normalSpeed
    • climbUpSpeed (roller coaster going up)
    • dropSpeed (roller coaster going down)
    So I'm planning to create 3 cubes and add these 3 speeds as a tag for each of them in the inspector.

    This is the code I want to edit (constant speed)

    Code (CSharp):
    1. using UnityEngine;
    2. using PathCreation;
    3.  
    4. public class Follower : MonoBehaviour {
    5.     public PathCreator pathCreator;
    6.     public float speed = 5;
    7.     float distanceTravelled;
    8.  
    9.     void Update() {
    10.         distanceTravelled += speed * Time.deltaTime;
    11.         transform.position = pathCreator.path.GetPointAtDistance(distanceTravelled);
    12.         transform.rotation = pathCreator.path.GetRotationAtDistance(distanceTravelled);
    13.     }
    14. }
    I am not so good at programming. Can someone help me out?

    This is another script from a different project which has a similar function

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using BezierSolution;
    5.  
    6. public class SpeedController : MonoBehaviour
    7. {
    8.  
    9.     public float normalSpeed = 15.0f;
    10.     public float dropSpeed = 20.0f;
    11.     public float climbUpSpeed = 10.0f;
    12.  
    13.     public float speedChangeAcceleration = 5.0f;
    14.  
    15.     public AudioClip fastAudioClip;
    16.     public AudioClip normalAudioClip;
    17.     public AudioClip slowAudioClip;
    18.  
    19.     private DoubleAudioSource doubleAudioSource;
    20.     private const int NORMAL_SPEED_MODE = 1;
    21.     private const int DROP_SPEED_MODE = 2;
    22.     private const int CLIMB_UP_SPEED_MODE = 3;
    23.  
    24.     private int speedMode = NORMAL_SPEED_MODE;
    25.     private int previousSpeedMode = NORMAL_SPEED_MODE;
    26.  
    27.     private bool changeSpeed = false;
    28.  
    29.     private SplineFollower splineFollower;
    30.  
    31.     // Use this for initialization
    32.     void Start()
    33.     {
    34.         splineFollower = GetComponent<SplineFollower>();
    35.         doubleAudioSource = GetComponent<DoubleAudioSource>();
    36.         if (doubleAudioSource != null && normalAudioClip != null)
    37.         {
    38.             doubleAudioSource.CrossFade(normalAudioClip, 1.0f, 2.0f);
    39.  
    40.         }
    41.  
    42.      
    43.  
    44.         splineFollower.speed = normalSpeed;
    45.     }
    46.  
    47.     // Update is called once per frame
    48.     void Update()
    49.     {
    50.         if (changeSpeed)
    51.         {
    52.             if (previousSpeedMode == NORMAL_SPEED_MODE && speedMode == CLIMB_UP_SPEED_MODE)
    53.             {
    54.                 splineFollower.speed -= (speedChangeAcceleration * Time.deltaTime);
    55.  
    56.                 if (splineFollower.speed <= climbUpSpeed)
    57.                 {
    58.                     print("Reached climb up speed");
    59.  
    60.                     changeSpeed = false;
    61.                 }
    62.             }
    63.             else if (previousSpeedMode == CLIMB_UP_SPEED_MODE && speedMode == NORMAL_SPEED_MODE)
    64.             {
    65.                 splineFollower.speed += (speedChangeAcceleration * Time.deltaTime);
    66.  
    67.                 if (splineFollower.speed >= normalSpeed)
    68.                 {
    69.                     print("Reached normal speed");
    70.                     changeSpeed = false;
    71.                 }
    72.             }
    73.             else if (previousSpeedMode == NORMAL_SPEED_MODE && speedMode == DROP_SPEED_MODE)
    74.             {
    75.                 splineFollower.speed += (speedChangeAcceleration * Time.deltaTime);
    76.  
    77.                 if (splineFollower.speed >= dropSpeed)
    78.                 {
    79.                     print("Reached drop speed");
    80.                     changeSpeed = false;
    81.                 }
    82.             }
    83.             else if (previousSpeedMode == DROP_SPEED_MODE && speedMode == NORMAL_SPEED_MODE)
    84.             {
    85.                 splineFollower.speed -= (speedChangeAcceleration * Time.deltaTime);
    86.  
    87.                 if (splineFollower.speed <= normalSpeed)
    88.                 {
    89.                     print("Reached normal speed");
    90.                     changeSpeed = false;
    91.                 }
    92.             }
    93.         }
    94.     }
    95.  
    96.     private void OnTriggerEnter(Collider other)
    97.     {
    98.         print("OnTriggerEnter " + other.tag);
    99.         previousSpeedMode = speedMode;
    100.  
    101.         if (other.CompareTag("Normal"))
    102.         {
    103.             speedMode = NORMAL_SPEED_MODE;
    104.             changeSpeed = true;
    105.             print("Change to normal speed");
    106.  
    107.             if (doubleAudioSource != null && normalAudioClip != null)
    108.             {
    109.                 doubleAudioSource.CrossFade(normalAudioClip, 1.0f, 2.0f);
    110.             }
    111.  
    112.         }
    113.         else if (other.CompareTag("Drop"))
    114.         {
    115.             speedMode = DROP_SPEED_MODE;
    116.             changeSpeed = true;
    117.             print("Change to drop speed");
    118.  
    119.             if (doubleAudioSource != null && fastAudioClip != null)
    120.             {
    121.                 doubleAudioSource.CrossFade(fastAudioClip, 1.0f, 2.0f);
    122.             }
    123.  
    124.         }
    125.         else if (other.CompareTag("Climb up"))
    126.         {
    127.             speedMode = CLIMB_UP_SPEED_MODE;
    128.             changeSpeed = true;
    129.             print("Change to climb up speed");
    130.             if (doubleAudioSource != null && slowAudioClip != null)
    131.             {
    132.                 doubleAudioSource.CrossFade(slowAudioClip, 1.0f, 2.0f);
    133.  
    134.             }
    135.  
    136.         }
    137.  
    138.     }
    139.     private void OnTriggerExit(Collider other)
    140.     {
    141.         print("OnTriggerExit");
    142.     }
    143. }
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    The second script looks to be doing what you describe. What is your issue?
     
  3. akshayjose07

    akshayjose07

    Joined:
    Mar 9, 2018
    Posts:
    2
    Hi
    Thanks for replying
    Yes second script is doing that perfectly.
    I got these two scripts from two different projects. I got the first script from Bézier Path Creator tool (available in asset store). This tool is perfect for my use but it doesn't have speed changing feature.
    Second project have speed changing but it lacks so many other major features.
    So how can I get speed changing from the second script to the first ?
     
    Last edited: Mar 5, 2020