Ok, I'm fooling around with unity and I want to know how to make two objects that will cause an object to move to the other object when they collide. how would I script those two objects?
I assume you are looking to create teleport pads, or something similar.
- You'd create two objects with colliders that are set to being triggers. These are your teleport pad triggers. Typically for this implementation you'd want them to have their center a bit up in the air.
- You'd create a simple script that checks if the trigger is set, and then teleporting your object to your other exit. Add this script to the triggers and make their exit reference each other.
- Any object with a collider per collision action matrix (see end of document) will get teleported to the exit.
- Should you want to make one way teleporters, make exit be a simple empty game object and position it somewhere of your liking.
I present the code in C#
using UnityEngine;
public class Teleporter : MonoBehaviour
{
public Transform exit;
static Transform last;
void OnTriggerEnter ( Collider other )
{
if ( exit == last )
return;
TeleportToExit( other );
}
void OnTriggerExit ( )
{
if ( exit == last )
last = null;
}
void TeleportToExit ( Collider other )
{
last = transform;
other.transform.position = exit.transform.position;
}
}
The `static Transform last;` is used to keep track of which teleporter you're coming from so you don't keep bouncing back and forth all the time between teleporters (as you potentially enter another teleporter as you exit the first).
try do something like this
var value = 1.0;
function OnCollisionEnter(other: Collision){
other.gameObject.SendMessage("Action",value);
}
and in the another object put this:
function Action(value : float){
//put what you need to do
}
or another way to do it is:
static var value = 1.0;
function OnCollisionEnter(other: Collision){
value = 2.0;
}
and(lets say the name of the last script the name is FindCollision) in the another object:
if(FindCollision.value == 2.0){
//do something
}
Your question and your title confuse me somewhat.
Teleportation and collision based movement ( e.a. transferring kinetic energy between bodies ) are quite different.
For teleportation all you need to do is change the vector values of the transform of an object. If you want that to be event drive you can use Uriels suggested script as a good base for getting you started.
The same can be said for physics, you want collisions , fine. But having an ACTUAL collision can never cause a teleportation unless you cheat. You simply steal the event "collision" , preferably using the isTrigger property set on the collider and catching it with OnTrigger(Exit/Enter/Stay). The collision will be ignored this way but the event will remain. Catch the event, perform the teleportation and do using aforementioned sample, and you got what I think you asked for.
Let me know how it works out if you will, I'll lend a hand as soon as able, I may have just gotten the whole interpretation botched and gave an answer that doesn't fit. Let me know aswell then ok , see if we can get the script going.
okay, I have a question, i’ve put 4 doors, and want to when my player collide with any of them, go to the exit of that door, but that doesn’t happens, why? I need this, like, tonight, is for a gamejam
just in case, solved the similar problem by setting a door, (collider2d, tick trigger), and the spawnPoint to the second door (collider2d, tick trigger).
public var (public int) spawnPoint;
void OnTrigger(){
if (collide with player) {
transform position to the spawnPoint;
}