Question by
akshayjose07 · Feb 21, 2020 at 04:13 PM ·
c#script.speedspeed up
How to include speed trigger creation option in this script.
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)
using UnityEngine;
using PathCreation;
public class Follower : MonoBehaviour {
public PathCreator pathCreator;
public float speed = 5;
float distanceTravelled;
void Update() {
distanceTravelled += speed * Time.deltaTime;
transform.position = pathCreator.path.GetPointAtDistance(distanceTravelled);
transform.rotation = pathCreator.path.GetRotationAtDistance(distanceTravelled);
}
}
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BezierSolution;
public class SpeedController : MonoBehaviour
{
public float normalSpeed = 15.0f;
public float dropSpeed = 20.0f;
public float climbUpSpeed = 10.0f;
public float speedChangeAcceleration = 5.0f;
public AudioClip fastAudioClip;
public AudioClip normalAudioClip;
public AudioClip slowAudioClip;
private DoubleAudioSource doubleAudioSource;
private const int NORMAL_SPEED_MODE = 1;
private const int DROP_SPEED_MODE = 2;
private const int CLIMB_UP_SPEED_MODE = 3;
private int speedMode = NORMAL_SPEED_MODE;
private int previousSpeedMode = NORMAL_SPEED_MODE;
private bool changeSpeed = false;
private SplineFollower splineFollower;
// Use this for initialization
void Start()
{
splineFollower = GetComponent<SplineFollower>();
doubleAudioSource = GetComponent<DoubleAudioSource>();
if (doubleAudioSource != null && normalAudioClip != null)
{
doubleAudioSource.CrossFade(normalAudioClip, 1.0f, 2.0f);
}
splineFollower.speed = normalSpeed;
}
// Update is called once per frame
void Update()
{
if (changeSpeed)
{
if (previousSpeedMode == NORMAL_SPEED_MODE && speedMode == CLIMB_UP_SPEED_MODE)
{
splineFollower.speed -= (speedChangeAcceleration * Time.deltaTime);
if (splineFollower.speed <= climbUpSpeed)
{
print("Reached climb up speed");
changeSpeed = false;
}
}
else if (previousSpeedMode == CLIMB_UP_SPEED_MODE && speedMode == NORMAL_SPEED_MODE)
{
splineFollower.speed += (speedChangeAcceleration * Time.deltaTime);
if (splineFollower.speed >= normalSpeed)
{
print("Reached normal speed");
changeSpeed = false;
}
}
else if (previousSpeedMode == NORMAL_SPEED_MODE && speedMode == DROP_SPEED_MODE)
{
splineFollower.speed += (speedChangeAcceleration * Time.deltaTime);
if (splineFollower.speed >= dropSpeed)
{
print("Reached drop speed");
changeSpeed = false;
}
}
else if (previousSpeedMode == DROP_SPEED_MODE && speedMode == NORMAL_SPEED_MODE)
{
splineFollower.speed -= (speedChangeAcceleration * Time.deltaTime);
if (splineFollower.speed <= normalSpeed)
{
print("Reached normal speed");
changeSpeed = false;
}
}
}
}
private void OnTriggerEnter(Collider other)
{
print("OnTriggerEnter " + other.tag);
previousSpeedMode = speedMode;
if (other.CompareTag("Normal"))
{
speedMode = NORMAL_SPEED_MODE;
changeSpeed = true;
print("Change to normal speed");
if (doubleAudioSource != null && normalAudioClip != null)
{
doubleAudioSource.CrossFade(normalAudioClip, 1.0f, 2.0f);
}
}
else if (other.CompareTag("Drop"))
{
speedMode = DROP_SPEED_MODE;
changeSpeed = true;
print("Change to drop speed");
if (doubleAudioSource != null && fastAudioClip != null)
{
doubleAudioSource.CrossFade(fastAudioClip, 1.0f, 2.0f);
}
}
else if (other.CompareTag("Climb up"))
{
speedMode = CLIMB_UP_SPEED_MODE;
changeSpeed = true;
print("Change to climb up speed");
if (doubleAudioSource != null && slowAudioClip != null)
{
doubleAudioSource.CrossFade(slowAudioClip, 1.0f, 2.0f);
}
}
}
private void OnTriggerExit(Collider other)
{
print("OnTriggerExit");
}
}
Comment