- Home /
Updating localPosition using parents' position
Currently I have this piece of code that update the position of a sprite on the screen when I drag it with the mouse. I want another object (the arrow) to follow it exactly at one specific point (the feet of the sprite, the middle of the bottom border of the collider), but this script doesnt work in fullscreen mode as it does in window mode because of differing scales ( I think ). So in order to get it working I need to use to localPosition I suppose, but I have no idea what to do here.
As you can see , I've set the offset in the arrow position to 60f but this isnt relative to screen size it seems.
void OnMouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
void OnMouseDrag()
{
if(!colliding){
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
Vector3 curArrowToScreenPoint = new Vector3(curPosition.x, curPosition.y**-60f**,screenPoint.z);
Vector3 curArrowPosition = Camera.main.ScreenToWorldPoint(curArrowToScreenPoint)+ offset;
transform.position = curPosition;
arrow.transform.position = curArrowPosition;
}
}
Answer by EvilTak · Dec 08, 2014 at 03:04 PM
Make the arrow a child of the sprite, position it exactly where you want and move only the sprite from your script. The arrow will stay at the same position relative to the sprite, i.e. it will move along with it the way you want.
Answer by taxvi · Dec 08, 2014 at 03:06 PM
why don't you change line 17 like this:
arrow.transform.position = transform.position + new Vector3(0, -1, 0);
and instead of -1 put the number you need. should work in all the resolutions.
Your answer
Follow this Question
Related Questions
Getting to move object with mouse position? 0 Answers
Adding a value to transform.position.y runs very untrusted 0 Answers
How to make object follow y-axis of another object. 1 Answer
2D RPG boomerang instantiation help. 2 Answers
Unity3D - Playback object array of position (with dynamic velocity) 0 Answers