- Home /
Teleport to where I click 2D
I'm creating a 2d game where I will not be able to move but to teleport.
I've made a 2d character (with no tags or scripts or anything)and attatched the camera to it (so the camera will follow the player). But I am not able to create a script that makes me able to tp to where you click.
If anyone can help please tell me.
Sorry for mt english btw.
I havent meddled with Unity 2D stuff as of yet, however have you tried grabbing Input.$$anonymous$$ousePosition and changing the location of the player to that? Pseudo code:
if(Input.Get$$anonymous$$ouseButtonDown(0)) { transform.position = vector2(mouseposition.x, mouseposition.y); }
In 3D you would need to getWorld space from the mouse position but not so sure about 2D side sorry!
@Alismuffin, my previous question about teleporting wasn't answered in a form that I could understand(new to program$$anonymous$$g).
Could you actually tell me how to make my character teleport to the position of the mouse? In 3D
So how did you get along with your game? I'm currently also building a 2D teleport game and would like some advice.
Answer by BlackHoleStorm · Apr 27, 2014 at 10:55 AM
Set your players tag to Player, then this script should do it. You can set this script to any object in the game too, as it checks the mouse position from the main camera, and it stores your player as a variable.
//Set up a variable to access the player from
private Transform player;
void Awake()
{
//Find the player object and set it
player = GameObject.FindGameObjectWithTag("Player").transform;
}
void Update()
{
// Check if you click the left mouse button then change position
if (Input.GetMouseButtonDown(0))
player.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
Assets/2D Assets Pack/Scripts/tp.js(2,9): BCE0043: Unexpected token: Transform.
:(
Remember this:
Common Sense:
Function: JavaScript
Void: C#
this was actually in c# not javascript
//Set up a variable to access the player from
var player:Transform;
function Awake()
{
//Find the player object and set it
player = GameObject.FindGameObjectWithTag("Player").transform;
}
function Update()
{
// Check if you click the left mouse button then change position
if (Input.Get$$anonymous$$ouseButtonDown(0))
player.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
Assets/Standard Assets/Character Controllers/Sources/Scripts/teleport.cs(2,11): error CS8025: Parsing error
I have put in into a c# but this pops up :/ am I doing something wrong?
//Set up a variable to access the player from
var player:Transform;
function Awake()
{
//Find the player object and set it
player = GameObject.FindGameObjectWithTag("Player").transform;
}
function Update()
{
// Check if you click the left mouse button then change position
if (Input.Get$$anonymous$$ouseButtonDown(0))
player.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
C'mon- function is for JavaScript, void is for c#!
Originally you put c# code into a javascript(unitscript) file (filename.js, the extension is .js for java/unityscript and .cs for c# scripts and code) what i did was java/unityscript, it belongs in a .js file, if you want to use the c# version, BlackHoleStorm's answer will work.
Hi, I tried out your code but can see that when testing my player isn't visible to the game window but is visible to the scene window. Do you have any idea how to fix this?
I can't say exactly, but check to make sure it's in view of the camera. Also check to make sure it's not behind anything (it's z position is 0), and make your camera's z position -10. I hope this helps.
Thank you for replying, I see the problem! turns out the z position for my player is setting it's self to -12.39203, even though it was originally set to 0. Do you know how to fix this?
Answer by DanielSan · Apr 27, 2014 at 10:56 AM
Something like this?
public void Teleport(Transform tp_trans)
{
Vector3 new_pos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
new_pos.z = tp_trans.position.z;
tp_trans.position = new_pos;
}
I've assumed that your Z position is constant.
Hi, I adjust your code a bit and it fixed my problem I was having before! Thank you :)