Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Amman98 · Apr 27, 2014 at 09:04 AM · script.teleport

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Alismuffin · Apr 27, 2014 at 09:11 AM 0
Share

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!

avatar image death · May 08, 2014 at 05:04 PM 0
Share

@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

avatar image shieldgenerator7 · Dec 25, 2015 at 01:10 AM 0
Share

So how did you get along with your game? I'm currently also building a 2D teleport game and would like some advice.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
 }
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Amman98 · May 06, 2014 at 03:33 PM 1
Share

Assets/2D Assets Pack/Scripts/tp.js(2,9): BCE0043: Unexpected token: Transform.

:(

avatar image AndrewBilotti Amman98 · Jan 07, 2016 at 07:48 PM 0
Share

Remember this:

Common Sense:

Function: JavaScript

Void: C#

avatar image Landern · May 06, 2014 at 03:40 PM 0
Share

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);
 }
avatar image Amman98 · May 08, 2014 at 03:58 PM 0
Share

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);
 }
avatar image AndrewBilotti Amman98 · Jan 07, 2016 at 07:49 PM 0
Share

C'mon- function is for JavaScript, void is for c#!

avatar image Landern · May 08, 2014 at 04:00 PM 0
Share

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.

avatar image wasicool7 · Dec 24, 2016 at 01:06 AM 0
Share

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?

avatar image shieldgenerator7 wasicool7 · Dec 24, 2016 at 02:21 AM 0
Share

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.

avatar image wasicool7 shieldgenerator7 · Dec 24, 2016 at 04:04 PM 0
Share

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?

avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image wasicool7 · Dec 24, 2016 at 05:12 PM 0
Share

Hi, I adjust your code a bit and it fixed my problem I was having before! Thank you :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to import the object from server to unity 2 Answers

Material doesn't have a color property '_Color' 4 Answers

Teleport enemy after player collides with an object? 0 Answers

How to make a cooldown on a script 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges