Created
August 24, 2016 20:39
-
-
Save Souliloquy/7c912c4ff1d931bd48ba7f8e277000ea to your computer and use it in GitHub Desktop.
Car Teleportation Trigger script for Edy's Vehicle Physics (Unity Store Asset)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using EVP; | |
public class TriggerCarTeleport : MonoBehaviour { | |
public Transform target; | |
public string[] tagList; | |
public bool onlyOnce = false; | |
[Header("Debug")] | |
public bool used = false; | |
string audioSourcesParentName = "Audio"; | |
public void OnEnable() { | |
used = false; | |
} | |
public void OnTriggerEnter(Collider other) { | |
Debug.Log ("Target Enter " + other.name); | |
if (!used || !onlyOnce) { | |
if (TagListContainsTag(other.tag)) { | |
used = true; | |
VehicleController car = other.GetComponentInParent<VehicleController> (); | |
Debug.Log (car.name + " TriggerTeleport to " + target); | |
// Freeze the rigidbody rotation, deactivate audio sources and then set the target position | |
car.transform.FindChild (audioSourcesParentName).gameObject.SetActive (false); | |
car.cachedRigidbody.freezeRotation = true; | |
car.transform.position = target.position; | |
// Now do some stuff with waiting frames | |
StartCoroutine (UnfreezeAndRotate (car)); | |
} | |
} | |
} | |
IEnumerator UnfreezeAndRotate(VehicleController car) { | |
// Wait one frame | |
yield return 0; | |
Rigidbody carBody = car.cachedRigidbody; | |
// Unfreeze rotation, remember current velocity and set target rotation | |
carBody.freezeRotation = false; | |
float vel = carBody.velocity.magnitude; | |
carBody.velocity = Vector3.zero; | |
carBody.rotation = target.rotation; | |
// Wait one frame | |
yield return 0; | |
// Set the velocity back to what it was | |
carBody.velocity = target.forward * vel; | |
// No angular velocity or inertia | |
carBody.angularVelocity = Vector3.zero; | |
carBody.ResetInertiaTensor (); | |
// Activate audio sources | |
car.transform.FindChild (audioSourcesParentName).gameObject.SetActive (true); | |
} | |
bool TagListContainsTag(string tag) { | |
for (int i = 0; i < tagList.Length; i++) { | |
if (tagList [i] == tag) | |
return true; | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment