I'm really new to unity and I'm trying to do a card game , And i get faced with the problem of coordinates . I'm trying to get the position of a the touch and make the sprite move for a specific position in the screen . Here is the my workaround script in c#:
using UnityEngine;
using System.Collections;
public class CardMovement : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
Rect recta = new Rect (-4.71f,-3.98f,4.52f,6.8f);
Touch To = new Touch ();
Camera C = GetComponent<Camera>();
Vector3 p = new Vector3 ();
p = C.ScreenToWorldPoint (To.position);
if (recta.Contains(p)== true){
transform.Translate(0.79f,-1.13f,0f);
}
}
}
The problem is that i can't get it to move to that specific position .is it because of the coordinates that aren't matching ? and is there a way to get the coordinates of the sprite directly without typing them ?thanks for help ;)
If what you want to do is detect the position where the user is tapping against a surface/zone, this object must have a collider and you should use a Raycast to detect where is the collision.
Moving an Agent to a Position Clicked by the Mouse
In this example they use the navmesh from unity but you don't have to, the information you are looking for is in hit.point
I managed to move the card to the position I wanted ( not exactly ) And used another code that i built myself , your proposition I didn't use because I didn't understand or i guess it's using 3D script ( If i'm wrong please correct me ) so here is the updated version of my code :
using UnityEngine;
using System.Collections;
public class CardMovement : MonoBehaviour {
public RectTransform rectangle;
// Use this for initialization
void Start () {
rectangle = GetComponent<RectTransform> ();
}
// Update is called once per frame
void Update () {
if (rectangle.rect.Contains (Input.GetTouch (0).deltaPosition)){
Vector2 V = new Vector2 (0.125f, 0.125f);
transform.position = V;
}
}
}
Now the main problems that i face :
Camera.ScreenToWorldpoint
or Camera.ScreenToViewportpoint
the card didn't respond to the touch ( i guess it a problem of coordinates ) NOW THE MAIN QUESTION : WHAT IS THE DIFFERENCE BETWEEN THESE COORDINATES : World coordinates , screen coordinates and view port coordinates . I didn't find an explanation in the unity manual .
Thanks for help :) .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.