- Home /
 
Touch position off from real position?
Hello, I've been working on a new project... never had this issue before though. I'm using EasyTouch, but I've run into this with other things. As it is the same Asset I used in my last game with no problems.
I made a variable for the sake of recording it's touch position. I have a character, and I want to be able to have it change directions, if you touch on the left side or the ride side of him. Except my character is at around -25 units on the X axis. When I touch around him, it's such a huge different. Around position 600 units on the X axis.
Does anyone know what might be causing such a large offset?
It's not in the code, because I only have a few lines... only asking if gesture.position.x < the game objects position. or greater.
Edit: Here is the code I used (attached to the camera), I have also noticed... the character twitches and can't move past a certain point (it auto-flips him back around).
 var mainCharacter : exSpriteAnimation;
 var mainCharacterBase : exSprite;
 var mainCharacterObject : GameObject;
 var touchx : float;
 var touchy : float;
 
 function Start()
 {
     mainCharacter.Play("idle");
 
 }
 
 function On_TouchStart(gesture:Gesture)
 {
     // Play "walk right" animation if touch at ground level
     if(gesture.position.x > mainCharacterObject.transform.position.x){
             
             mainCharacter.Play("walk");
             mainCharacterObject.transform.position.y = mainCharacterObject.transform.position.y - 8;
             mainCharacterObject.transform.eulerAngles.y = 0;
             
     }
     
     // Play "walk right" animation if touch at ground level
     if(gesture.position.x < mainCharacterObject.transform.position.x){
 
             mainCharacter.Play("walk");
             mainCharacterObject.transform.position.y = mainCharacterObject.transform.position.y - 8;
             mainCharacterObject.transform.eulerAngles.y = 180;
             
     }
     
 }
 
 
 function On_TouchDown(gesture:Gesture)
 {
     // Translate "walk right" animation if touch at ground level
     if(gesture.position.x > mainCharacterObject.transform.position.x){
             mainCharacterObject.transform.Translate(2,0,0);
             mainCharacterObject.transform.eulerAngles.y = 0;
             
     }
     
     // Translate "walk right" animation if touch at ground level
     if(gesture.position.x < mainCharacterObject.transform.position.x){
             mainCharacterObject.transform.Translate(2,0,0);
             mainCharacterObject.transform.eulerAngles.y = 180;
             
     }
     
 }
 
 
 function On_TouchUp(gesture:Gesture)
 {
     mainCharacter.Play("idle");
     mainCharacterObject.transform.position.y = mainCharacterObject.transform.position.y + 8;
 
 }
 
              Answer by Bunny83 · Sep 23, 2012 at 03:51 AM
I never used EasyTouch, but it seems the touch position is, like almost always, in screen coordinates. As in the first code example on this page you can see he uses `cam.ScreenToWorldPoint`. Keep in mind that would only work with an orthographic camera. With a perspective camera you need to use a raycast to project the 2d point into world space.
A common way to project a screen point to world space for a 2d or 2.5d game is to use a Plane
See the edit in my answer on this question.
Thank you, weird how I never had to worry about that stuff before. Worked right out of the box in my first game. Though now I it works, but I have to figure out how to get my character working in the same coordinates as far as positioning. I did always use Orthographic though. As this is a 2D game, and so was the last.
I think I can take this and figure it out now. Thank you!
Your answer
 
             Follow this Question
Related Questions
GameObject's local position without parent 1 Answer
How to define a position local to an object? 2 Answers
Finding Position based on a local perspective 1 Answer
Move object on local axis instead of world axis after rotation 0 Answers
Why does the object alignment only work when the object isn't rotated? 2 Answers