- Home /
Teleporting when interacting with object
While there are a lot of answers about this topic, I still don't seem to understand how does it work. I wont the character (FPS character, basically a player object with a camera as a child) to teleport in another area of the scene when interacting with a series of objects object (in this case tagged with 'computer'. What I came up with, reading other answers, is this:
if(Physics.Raycast(ray, out hit, 2))
{
if(hit.collider.tag == "Computer")
{
if( Input.GetButton ("Fire1") ) {
player.transform.position = Vector3(-18,6,19);
}
}
}
But It keeps giving me errors with that last line. Is 'player' suppose to be the name of an object or tag? Or does the expression always needs to be written like this? And does this expression even exists?
Also, if I find the way to make it work It would seems that the character would be teleported but not rotated in the right direction, how can I make i so that it does?
Please, I'm writing it all in c#, don't post suggestions in java, I'm so clueless that I would still try to make it work without realizing.
Have you declared the variable 'player' anywhere? You need to tell the compiler what all the words you are using mean before you use them- in this case you probably need something like this at the top:
public GameObject player;
then assign it in the inspector when you add the component.
I tried that, and now it gives me this error on that last expression: Error CS0119: Expression denotes a type', where a
method group' was expected (CS0119) (Assembly-CSharp)
You said you're using C#? - Then don't forget your new
keyword.
player.transform.position = new Vector3(-18,6,19);
I don't know what that "new" does, but it worked, thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613114130im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Adding rotation to Physics.AddForce(); ... Getting weird error? 0 Answers
Roguelike Tutorial Expansion of Layout 1 Answer
CS1502 and CS1503 Error 0 Answers