Hey guys,
I have been working on a space exploration flight sim game. I am having trouble writing a script. When I reach the edge of my terrain I would like to hit a collider and be transported to the exact opposite side of the terrain. I am not sure how I would set that up exactly. I already have the walls. The top wall has a trigger that when I hit it I “fly into space” by loading a new scene.
script would read somthing like
public var Player: GameObject;
function ontrigger enter(other: Collider)
{
if (other.gameobject.compairtag(“Player”)
player.transform.position = the oposite x and z but same Y
Also rotate the player x 180 degrees
You may need to give more detail about how the wall and the player are setup but here’s a few ideas to try:
For the trigger to work, the wall needs a collider with the Is Trigger toggle ticked. You probably have that
Then, for the trigger/collider to react, it needs a gameobject with a RigidBody component to pass into it. Does your player have a RigidBody?
If I were you I would define transporter points with empty game objects so you can easily define where the ‘wall’ sends your player to - let’s call it the TransportToLocation. This could be an Empty gameobject that you put on the other side. In your wall script, have a public variable for the TransportToLocation gameobject and in the inspector, for the wall script just drag in the Empty at the distant wall into the TransportToLocation slot in the Inspector.
NOTE: it would be smart to move the Empty transport-to objects just enough away from the trigger (on the distant wall) so your player doesn’t keep zipping back and forth between them forever.
I have this script that I put on my Jet. I works but only on one wall. When you hit the"left bumper" it does transport you to the “right bumper” but not the other two or back the way you came.
using UnityEngine;
using System.Collections;
public class Move : MonoBehaviour
{
public float speed = 5.0f;
private GameObject topBumper;
private GameObject bottomBumper;
private GameObject leftBumper;
private GameObject rightBumper;
private GameObject toBumper;
// Use this for initialization
void Start()
{
bottomBumper = GameObject.Find("BottomBumper");
topBumper = GameObject.Find("TopBumper");
leftBumper = GameObject.Find("LeftBumper");
rightBumper = GameObject.Find("RightBumper");
}
// Update is called once per frame
void Update()
{
float vDir = Input.GetAxis("Vertical");
float hDir = Input.GetAxis("Horizontal");
float rot = transform.rotation.z;
//Debug.Log (vDir);
//Debug.Log (hDir);
if (vDir > 0) // going up
{
if (rot > 180)
rot = 360;
else
rot = 0;
}
else if (vDir < 0) // going down
rot = 180;
if (hDir > 0) // to the right
rot -= 270;
else if (hDir < 0) // to the left
rot -= 90;
//Debug.Log(Mathf.Abs(rot));
if (vDir != 0 || hDir != 0)
transform.eulerAngles = new Vector3(0, 0, Mathf.Abs(rot) / 2);
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
transform.position += move * speed * Time.deltaTime;
}
void OnTriggerEnter(Collider hitObj)
{
float vDir = Input.GetAxis("Vertical");
float hDir = Input.GetAxis("Horizontal");
float movePastAmt = 1.0f;
float newH = transform.position.x;
float newV = transform.position.y;
if (hitObj.gameObject.tag == "VBumper")
//if (hitObj.gameObject == topBumper || hitObj.gameObject == bottomBumper)
if (vDir > 0)
{
newV = bottomBumper.transform.position.y + movePastAmt;
}
else if (vDir < 0)
{
newV = topBumper.transform.position.y + -movePastAmt;
}
if (hitObj.gameObject.tag == "HBumper")
if (hDir > 0)
{
newH = leftBumper.transform.position.x + movePastAmt;
}
else if (hDir < 0)
{
newH = rightBumper.transform.position.x + -movePastAmt;
}
Vector3 newPos = new Vector3(newH, newV, 0);
transform.position = newPos;
//transform.Rotate(0, 0, rot);
}