Hi, Im working on a maze game and I want a portal in it to teleport the character to a new location.
var spawn : Transform;
var target : GameObject;
function Start()
{
target = gameObject.FindGameObjectWithTag("Player");
}
function OnCollisionEnter(hit : Collision)
{
if(hit.gameObject.tag == "Player")
Debug.Log("Hit!");
target.position = spawn;
}
Theres the code Im using. I wasnt sure whether to put target.transform or target.position, but I tried both and neither worked. When I play the game no errors pop up, but when I touch the portal nothing happens. Any ideas?
hey there, i am not one of the problem solving guys of this forum but from your code i see an obvious error.
On Line 13 : target.position = spawn;
should be => target.position = spawn.position;
because your variable spawn is of type “Transform” and from API Transform class has a “position” field parameter which u should be using.
since ur doing position = Transform which doesnt make sense its giving you error.
Another last point, if problem persists then open console at the bottom (double click) in unity3D and paste here the error. If you click on the error it will show u what line the error is (paste that too).
Another mistake
On Line 13: target.position = spawn;
or
On Line 2: var target : GameObject;
GameObject class according to API doesnt have position as field parameter. Class Transform is subclass of GameObject and Transform does have position.
So unless you wanna correct Line 2 to => var target : Transform;
, dont use GameObject.
Just another thing to be careful of is using Start() function. If things don’t work out right (not sure how you want the code to run) Put the code on
Line 6: target = gameObject.FindGameObjectWithTag(“Player”);
to blank Line 3 and leave Line 6 blank
I don’t believe it is any of these. I did change the spawn to spawn.position, but that wasn’t really where the current problem was. I’ve used GameObject and Transform somewhat interchangeably before, so I don’t think that’s it. The main problem is that when I collide with the portal nothing pops up in the consol. As you can see I put a Debug.Log command in there, but still its not registering the hit. So the problem isnt in what the hit causes to happen, it’s in what causes the hit, if that makes sense.
The script didnt work. It gave me an error about ObjController. It said it didnt recognize it. Also, in my script there is no Update function. That doesnt have anyhting to do with the Collision detection does it?
Hey, i am back with a small code. The below code works but apparently only wen the object who is assigned this script has gravity and falls on that target object that is to be Teleported. Just in case apply Character Control script too. Its awkward why it doesnt work when the i take third person control and make it move to that teleporter if though it works wen the teleporter is dropped on it. Didnt do a lotta testing but if ur happy with “distance” method i wont work on it. Btw according to zemog86 cud be right as well.
function OnCollisionEnter(collision : Collision) {
print("OnCollisionEnter name = " +collision.collider.transform.name);
collision.collider.transform.position = Vector3.zero;
}