- Home /
how do you make a map which can be circumnavigated?
im trying to make a map which if you go to one edge you appear at the otherside.
This might be jumping the gun, but if you're thinking of making a world map kind of like the old play station final fantasies, I would say a good solution is to create the map as a sphere which rotates under the character as he plays his run animation. I previously tried creating a flat world map, but I ran into a problem where even though I was able to position my character to the other side once I reached the end, the change of textures was jarring, no matter how hard I tried to make it similar. Was I at least correct in my assumption that that is what you want to do?
Answer by ThunderAwesome · Sep 28, 2012 at 08:31 PM
I have an example project showing how it works at this link: http://dl.dropbox.com/u/105851962/Wall_Test.zip
EDIT: Package with the solution - http://dl.dropbox.com/u/105851962/Circumvent_Package.zip
EDIT AGAIN: Links have been updated! (Should Work)
You can place Collider boxes at the edges of the map (assuming it is square) and simply position your character at the Collider box on the other side of the map with a bit of an offset so they don't infinitely collide with a collider box and change their position.
So, if you character collides with the right side of the map, they would simply be transitioned to the left side of the map. Apply this script to your character:
using UnityEngine;
using System.Collections;
public class Circumvent2 : MonoBehaviour
{
//DISCLAIMER: Feel free to use this script and Projectfor whatever you need it for. - ThunderAwesome
///
/// Instructions
/// Assign all the wall GameObjects to their respective object.
/// Set the tags to each wall whatever it should be (I.E. - rightWall's tag is "Right_Wall")
/// Set the offset to whatever you want it to be...just make sure the character doesn't fly off the edge.
/// Offset should be positive for the Right and Top wall and negative for Left and Bottom...
/// Example: transform.position = leftWall.transform.position + new Vector3(offset, 0, transform.position.z);
/// Works with Right wall. AND transform.position = leftWall.transform.position + new Vector3(transform.position.x, 0, offset);
/// Works with Top wall. /// Example: transform.position = leftWall.transform.position + new Vector3(-offset, 0, transform.position.z); Works with Left wall.
/// Notice offset has a negative sign before it now.
/// Try implementing the other walls and taggin them correctly!
///
public GameObject leftWall;
public GameObject rightWall;
public GameObject topWall;
public GameObject bottomWall;
private float offset = 1.0f;
//used to set the Gameobjects to their respective
//object, so you don't have to assign them. Just make
//sure you name them correctly.
void Start()
{
leftWall = GameObject.Find("Left_Wall");
rightWall = GameObject.Find("Right_Wall");
topWall = GameObject.Find("Top_Wall");
bottomWall = GameObject.Find("Bottom_Wall");
}
//You'll need to have your Box Collider's isTrigger
//set to true for this function to work.
void OnTriggerEnter(Collider other)
{
//if the player collides with the rightWall
if(other.gameObject.name == "Right_Wall") //i usually check tags but this might work (can't test since I'm at work)
{
//set character position to left_wall + offset
transform.position = leftWall.transform.position + new Vector3(offset, 0, transform.position.z);
}
if(other.gameObject.name == "Left_Wall")
{
transform.position = rightWall.transform.position + new Vector3(-offset, 0, transform.position.z);
}
if(other.gameObject.name == "Top_Wall")
{
transform.position = bottomWall.transform.position + new Vector3(transform.position.x, 0, offset);
}
if(other.gameObject.name == "Bottom_Wall")
{
transform.position = topWall.transform.position + new Vector3(transform.position.x, 0, -offset);
}
}
//Inserted this to make debugging easier.
void Update()
{
//Press "R" to reset the scene
if(Input.GetKey(KeyCode.R))
Application.LoadLevel(0);
}
}
Adjust the values as needed. Make sure to set all the walls to their respective walls. I hope that helped.
i have been having trouble with this i have tried tinkering around with it but i cant get any of the other wall exccept right to work here is the code main code i mess about with
using UnityEngine; using System.Collections;
public class Circumvent : $$anonymous$$onoBehaviour { //DISCLAI$$anonymous$$ER: Feel free to use this script and Projectfor whatever you need it for. - ThunderAwesome /// /// Instructions /// Assign all the wall GameObjects to their respective object. /// Set the tags to each wall whatever it should be (I.$$anonymous$$ - rightWall's tag is "Right_Wall") /// Set the offset to whatever you want it to be...just make sure the character doesn't fly off the edge. /// Offset should be positive for the Right and Top wall and negative for Left and Bottom... /// Example: transform.position = leftWall.transform.position + new Vector3(offset, 0, transform.position.z); /// Works with Right wall. AND transform.position = leftWall.transform.position + new Vector3(transform.position.x, 0, offset); /// Works with Top wall. /// Example: transform.position = leftWall.transform.position + new Vector3(-offset, 0, transform.position.z); Works with Left wall. /// Notice offset has a negative sign before it now. /// Try implementing the other walls and taggin them correctly! /// public GameObject leftWall; public GameObject rightWall; public GameObject topWall; public GameObject bottomWall; private float offset = 1.0f; //You'll need to have your Box Collider's isTrigger //set to true for this function to work. void OnTriggerEnter(Collider other) { //if the player collides with the rightWall if(other.gameObject.tag == "Right_Wall") //i usually check tags but this might work (can't test since I'm at work) { //set character position to left_wall + offset transform.position = leftWall.transform.position + new Vector3(offset, 0, transform.position.z); } if(other.gameObject.tag == "Left Wall") { transform.position = leftWall.transform.position + new Vector3(-offset, 0, transform.position.z); } if(other.gameObject.tag == "Top Wall") { transform.position = bottomWall.transform.position + new Vector3(offset, 0, transform.position.x); } if(other.gameObject.tag == "Bottom Wall") { transform.position = topWall.transform.position + new Vector3(-offset, 0, transform.position.x); } //do the same for the other walls...but make sure //they are switched to the correct positions and such }
$$anonymous$$ake sure you tag the objects correctly. If you click on the right wall in the scene and look at the inspector you'll see the right wall is tagged "Right_Wall". Create your own tags for each of the other walls. Or to make it easier you could do this:
//Look for the object by name not tag //So whatever you named the object you //compare to see if it is that object here. if(other.gameObject.name == "Right_Wall") { //offset code stuff }
Also, for offsetting the player, I noticed that you just changed the: new Vector3(offset, 0, transform.position.z); to to something like (-offset, 0, tranfsorm.position.x); What you have to do is actually set it to: (transform.position.x, 0, offset) and (transform.position.x, 0, -offset) to get it to work for the top and bottom.
I changed my code around to show the solution.
this is just an extension of the main question but is it possible to have like a camera on the opposite sides of the side you entering so you can see throught wall in a sense towards the other side .So it gives the appearance of never ending.Of course i only make it so you can see only so far through a wall before teleporting to the other side
Like Portal? When you look through the Portals it looks never ending. Is that what you mean?
Answer by Kiloblargh · Sep 29, 2012 at 06:43 PM
It depends on how your map is and how you are moving your character, but colliders probably aren't the most efficient way to do it- you can also just check the character's transform x and z positions and move it if they go over or under a certain value. I f you had a circular game map, you could check the distance from the center point and move the character twice that distance in the direction back to the center. If you have a lot of other items/ enemies / whatever that could be getting warped from one side to the other in addition to the player, go with the colliders.
Your answer
Follow this Question
Related Questions
GUI Menu After every round how do I display a random image? 1 Answer
Zombie Round Script Help 1 Answer
Rounds increasing exteremely fast 1 Answer
Round x, y and z values of object? 1 Answer
Rounding up Damage to an Int 2 Answers