- Home /
 
               Question by 
               martinpinky · Jul 01, 2014 at 11:36 PM · 
                c#  
              
 
              need teleport script fixed slightly
so i found this script by blackstorm to teleport the player on mouse click in a 2D game but it uses the camera's position the bad thing is my camera sits at -1 on the z position and everything else on the 0 z, if i dont do this the camera will not see the player or anything in the game for that matter how would i teleport to the mouse click but keep my players Z position? help would be appreciated.
 //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
              
 
               
              Answer by Kiwasi · Jul 01, 2014 at 11:40 PM
Use a temporary variable like so, insert instead of line 14
 Vector3 targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
 targetPosition.z = 0;
 player.position = targetPosition;
Your answer
 
 
             Follow this Question
Related Questions
C# Script Template - how to make custom changes? 1 Answer
Health Regen 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
press e to speek -1 Answers
Press 'e' key on object? (C#) 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                