Question by
Zulqurnain · May 14, 2016 at 03:04 PM ·
c#animation2d
When i m destroying stationery object with this code it works but when i try to destroy moving objects i m unable to do so can any body help me .,Hy! I m new to unity ,Destroying the stationery gameobject but unable to destroy the moving object
void update(){
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began) {
var startpos = Camera.main.ScreenToWorldPoint (Input.GetTouch (0).position);
if (Physics2D.Raycast (startpos, Vector2.zero, 1 << LayerMask.NameToLayer ("Ballon"))) {
Debug.Log ("Hits a ballon");
Destroy (gameObject);
}
}
}
Comment
Answer by JedBeryll · May 14, 2016 at 03:11 PM
Raycast has no direction. Vector2.zero tells it to move 0 in x direction and 0 in y direction. If you want to cast towards the center, you need a directon not an end point. So:
Vector3 direction = Vector2.zero - startpos;
//subtracting from 0 will of course be the same as:
Vector3 direction = -startpos;
Or you can use a Physics.LineCast which takes a start and an end point.
Your answer
Follow this Question
Related Questions
Animation with different behaviors for different directions. 1 Answer
Unity 2D: Game freezes after the first frame of a particular animation 1 Answer
2D Animation disables Player Controller Script 1 Answer
Enemy death animation keeps looping (Unity 2D) 1 Answer
Trouble with Colliders / Animations 0 Answers