Author Topic: Spawning a sprite at a touch  (Read 2387 times)

Zophiel

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 54
    • View Profile
Spawning a sprite at a touch
« on: June 26, 2013, 09:25:36 AM »
I've been going through search trying to find an answer but i think i dont understand what some of the data under UICamera.currentTouch is for. Right now I use the following which doesnt seem to get me what i need:

  1.                         Ray ray = UICamera.mainCamera.ScreenPointToRay( UICamera.currentTouch.pos );
  2.                         RaycastHit hit;
  3.                         Physics.Raycast( ray, out hit );

I have a center anchor point where I parent the widgets and then i need to set their localPosition to the touch position.

Zophiel

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 54
    • View Profile
Re: Spawning a sprite at a touch
« Reply #1 on: June 26, 2013, 09:59:49 AM »
Currently i got it working as follows by adding a collider in the area i want my sprites to spawn:

  1. Ray ray = UICamera.mainCamera.ScreenPointToRay( Input.mousePosition );
  2.                         RaycastHit rayHit;
  3.                         dropArea.Raycast( ray, out rayHit, 100 );
  4.                         GameObject decoration = (GameObject)Instantiate( selectedDecoration, rayHit.point, Quaternion.identity );
  5.                         decoration.transform.parent = mountPoint.transform;
  6.                         decoration.GetComponent<UISprite>().MakePixelPerfect();

Does anyone have any neater idea's?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Spawning a sprite at a touch
« Reply #2 on: June 26, 2013, 12:43:32 PM »
UICamera.lastHit already gives you the result of the raycast underneath the touch / mouse. There is no need to raycast yourself.

Likewise, never use Instantiate. Use NGUITools.AddChild instead. You can pass a parent and it will parent the object correctly, and will reset the transform for you, as well as set the game object layer.