Skip to content

Instantly share code, notes, and snippets.

@Souliloquy
Created August 24, 2016 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Souliloquy/7c912c4ff1d931bd48ba7f8e277000ea to your computer and use it in GitHub Desktop.
Save Souliloquy/7c912c4ff1d931bd48ba7f8e277000ea to your computer and use it in GitHub Desktop.
Car Teleportation Trigger script for Edy's Vehicle Physics (Unity Store Asset)
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