- Home /
Upon mouseclick, move sprite few pixels to right
I am trying to change a sprite so that when I click on it; it just shifts X pixels to the right, just now I am using 5 as an example. I have the click detection down but the way I am trying to do the transform isn't working. I am sure it is obvious to someone why, maybe you can help. Here is what I have tried;
void Update () {
RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector2.zero);
if (hit.collider != null) {
if(Input.GetMouseButtonDown(0) && hit.collider.gameObject.name.Equals("finger")){
Debug.Log ("made it to the danger zone");
Vector3 t = hit.collider.gameObject.transform.position;
t.Set(t.x+5,t.y,t.z);
}
}
}
Answer by robertbu · Mar 29, 2014 at 03:37 AM
You just need to assign it back:
hit.transform.position = t;
Vector3s are structs, so you get copies, not references to the original. So your current code is setting the value in the copy. Also note that 'position' is in world space units, not pixels. If you select the sprite texture in the inspector, you will see 'pixels' to units. It is 100 by default, so you want to add 0.05 to 'x' not 5 if you want a 5 pixel movement.