I am attempting to make a portal. I have a particle system for the portal and it is fine but the script I am using doesn’t work. The script is attached to the portal. I have the car drive through the portal and thats it all it does is drive through. I am only changing the position of the car right now for testing purposes.
Im trying to understand so forgive if Im off the mark: the transform in this case is the portal (the object the script is attach to), where the “other” here is your car (the object colliding with the portal) - so your trying to change the position of the car correct? So in order to do so you would change the position of the “other” object
I think Zwacky was on the right direction, however he did not really help you.
First, you are using a trigger that MUST have a collider pass through it to get it to cause the event. Next, you are creating a Vector3 with no purpose. (you need to purpose the vector) Lastly, in Zwacky’s response; he was on the right track but failed to help you out just a little more.
First, when the other game object’s name is “Car Prefab” you want to change the position of the car. In the case of what I provided, you really want to change the root’s location. If the car’s collider is not on the root, then you will just move the collider of the car, and we dont want that. So we make sure the root of the car is what is moving.
Next, You created a purposeless Vector3. This means you didn’t really assign it anywhere, you just created it. What I did was us the += operator to make it add something to the position of the root transform. This means that I can simplify the Vector3 as I do not need to write in all the transform.position junk to it.
Observe other notations for defining a position:
other.transform.root.position=Vector3(10,10,0);
other.transform.root.position=transform.position + Vector3(10,10,0);
var portalTo : Transform;// create a ending for your portal
other.transform.root.position=portalTo.position;
other.transform.root.rotation=portalTo.rotation;// note you can use the rotation of the object so it always faces a certain direction.
I may have done something wrong but I am not sure. I placed the new script on the portal and I have the car go through it but it doesn’t work.
This is the new script based on what you gave me.
function OnTriggerEnter (other:Collider) {
if(other.gameObject.name == "Car Prefab"){
other.transform.root.position+=Vector3(10,10,0);
other.transform.root.position=transform.position + Vector3(10,10,0);
var portalTo : Transform;
other.transform.root.position=portalTo.position;
other.transform.root.rotation=portalTo.rotation;
}
}
Im a beginner/novice, so Im trying to understand this as well. I dont understand what the rest of that code is doing, seems to me the portalTo piece of the code is setting the car back to the portal position??
I was thinking this is all you need. Can you try just this
So, you say it “doesn’t work”. What does that mean? Does it not work ‘correctly’ or it doesn’t work at all?
If it doesn’t work at all, then it means your collider and rigidbody requirements are not met. Check the documentation. OnCollision and OnTrigger confuses me. One has a “one or both objects must have a collider” but only one needs a rigidbody or “both needs at least one of collider or rigidbody” or… I just don’t know! Always a pain for me getting this function to trigger… Just check the docs.
Anyways, there is a very simple way to test if it works or not. Use this code:
function OnTriggerEnter (other:Collider) {
Destroy(other.transform.root.gameObject);
Destroy(gameObject);
}
This way you will DEFINITELY know if your colliders / rigidbodies are setup properly.
Just a little note here for the fun of it… I notice in the code provided you above, they test if the collider is called “Car Prefab” but they assume the collider is a child object and want to move the root object… Funny that. This means they assume you gave the name “Car Prefab” to a child object, not the root… So the question remains: Is your prefab called Car Prefab and if so, is the collider called that or the root object? In fact, IS there a root object to your model or is the car the only object.
If your car is the root object and you use transform.root.gameObject, you will be pointing at the world’s gameobject… that should also give you an error because the root is NULL. TRUST ME on this one… argggh! I have tried doing
and in both cases I got a NULL reference error because I was trying to point to the world. I don’t know why assigning a NULL value to a transform would cause a runtime error(???) but yeah, it did. So the root is definitely null.
Anyways, I had an alternative method for you to do your transportation and wanted to suggest it but then I noticed you already had the same idea, you just implemented it wrong. What I would suggest is:
you create a new empty game object and call it TransporterPrefab
you create your transporter object and place it inside the TransporterPrefab and move it to 0,0,0
you create another empty object and call it portalTo and place that inside TransporterPrefab also
now you move that gameobject to whereever you want it to be. It is irrelevant for now.
save this entire thing as a prefab and call it TransporterPrefab
change your script to this:
var portalTo : Transform;
function OnTriggerEnter (other : Collider)
{
//pick one of these as appropriate
if(other.gameObject.name == "Car Prefab")
// if(other.transform.root.name == "Car Prefab")
// if(other.transform.tag.CompareTo("Car Prefab")
// if(other.transform.root.tag.CompareTo("Car Prefab")
{
other.transform.root.position=portalTo.position;
other.transform.root.rotation=portalTo.rotation;
}
}
now in your PREFAB, not in the scene, in your PREFAB, drag the portalTo gameObject onto your portalTO field in the script.
That’s it, you are done. Now you have a prefab you can drop anywhere you want and when you do it will have an object you can select and drag to anywhere in the game world and it will teleport the “Car Prefab” to wherever you dragged that object to. Gives you a visual teleporter setup instead of a vector math solution
First thing’s first, though, see if your coliders are setup properly or not.
Next, do the above…
Well, my colliders work because they have worked before with a code i just recently found but it uses vector math and your solution seems a lot simpler, and when i go through the portal with the old code i lose my mesh colliders and therefore go through certain objects. I have tested numerous times different things and what I’ve found is that pre-portal entry i collide with everything in game and post-portal entry i collide only with the ground unless i use my respawn code i have in which case i fall through the ground. Also on my mesh collider(which is the only way it has worked so far)when i have the is trigger box checked my car doesn’t run properly. it begins to look like it has less “gravity” by which i mean that i will go into wheelies if to much speed is attained whereas if i don’t have the is trigger box checked that doesn’t happen but it works with the portal only when i the is trigger box checked. i have tried this with every other possible collider and mesh works the best. it is not an issue i’m just wondering why it’s happening and how to fix it. now for the question in your code quote: 1: the car is actually named car prefab. i have found no reason to change it other than simplicity so i just ignore it. note 1: it is moving it 10,10,10 just for simplicity’s sake because i have no idea where the final location will be. note 2: i have never noticed that before i just copied the code off of BigMisterB’s post and as such never paid attention to it accept for how it worked, and as i have never used root’s before and never thought about it much, i just thought it was necessary. note 3 and 4: i have not really done much with this sort of scripting(for me this is advanced transformations) so i did not make the connection that it was a null object. ok now onto the actual post. I have some basic knowledge of how oncollisions and ontriggers work. by doesn’t work i mean it doesn’t “teleport” when it goes through the portal, it does just that goes through the portal and to the other side of the particle system i am using to represent a portal, if that makes sense. i have no need to check if they work because as i have said i know that it works. i am not getting any reference errors when i go through the portal, working or not. now i hope this helps in case the new code doesn’t work, i can try to elaborate further.
ok i have no idea what is going on but it doesnt work. i have done these steps. made the new script, made the emptygame object and those other steps. i placed the script on the portal part of it, and have the portalto as the location. pretty much everything you said to do i did and it doesnt work.
Interesting… did you say the isTrigger is on the Car? Wouldn’t it make a lot more sense to place it on the teleporter instead? Also, if it is on the car and the OnCollisionEnter code is on the car also then the “other” mentioned above would actually teleport the teleporter not the car. The fact that you say nothing is moved suggests to me the colliders are not setup correctly but if you are certain they are correct, then I have no idea…
A couple of years ago I had an issue where I had to add 3 colliders to a single mesh in order to get it to move, not fall through the floor, not fly away in random rotations and actually trigger my OnTriggerEnter code. One collider had to be on the same level as the rigidboby and the mesh, the other was attached higher up in the hierarchy … it was a mess but it was the only way I could get it to work as I wanted the model to be separate from the character controller that moved it. Based on the hierarchy, placing a colider in one place would allow it to move but not trigger the event while placing it on the other part it would trigger the event but tying to move me character would send it shooting out into space with random rotations… Took me ages to get it working. This was what i thought you were experiencing.
Just do me this one little favour, yes: Add any Debug.Log message into your OnTriggerEnter function as the first line and run the game. Just confirm for me that the message does actually print…
I will recheck my colliders as they were set up for my previous portal and i don’t believe i changed anything that would make that happen. But i will also try the multiple collider idea so it stops glitching, and if all else fails i will try your solution johnessy, as i just want to get this code working.
I’m not sure who is still following this but here is what i have come up with, i have a added a debug log command into the script and low and behold, the debug worked. although the car will still not teleport at all. i have no idea what is going on. any help will be greatly appreciated.