RaycastHit2D









How do I use Raycast Hit2 D
Below are practical examples compiled from projects for learning and reference purposes

Featured Snippets


File name: JumpAndRunMovement.cs Copy
94     void UpdateIsGrounded()
95     {
96         Vector2 position = new Vector2( transform.position.x, transform.position.y );
97
98         RaycastHit2D hit = Physics2D.Raycast( position, -Vector2.up, 0.1f, 1 << LayerMask.NameToLayer( "Ground" ) );
99
100         m_IsGrounded = hit.collider != null;
101         m_Animator.SetBool( "IsGrounded", m_IsGrounded );
102     }
File name: Cannon.cs Copy
63  void TouchCannonShoot(){
64   if(Input.touchCount > 0){
65    Touch touch = Input.GetTouch (0);
66
67    touchPos = Camera.main.ScreenToWorldPoint (touch.position);
68
69    Vector2 touchRayHit = new Vector2 (touchPos.x, touchPos.y);
70
71    RaycastHit2D hit = Physics2D.Raycast (touchRayHit , Vector2.zero);
72
73    if(hit.collider != null){
74     if(hit.collider.CompareTag("Player")){
75      if(touch.phase == TouchPhase.Stationary){
76       if (shot != 0) {
77        UpdatePowerLevel ();
78        isCharging = true;
79       }
80      }else if(touch.phase == TouchPhase.Ended){
81       if (shot != 0) {
82        GameObject newCannonBullet = Instantiate (cannonBullet, cannonTip.position, Quaternion.identity) as GameObject;
83        newCannonBullet.transform.GetComponent ().AddForce (cannonTip.right * powerLevel.value, ForceMode2D.Impulse);
84        if (GameController.instance != null && MusicController.instance != null) {
85         if (GameController.instance.isMusicOn) {
86          audioSource.PlayOneShot (cannonShot);
87         }
88        }
89        shot--;
90        powerLevel.value = 0;
91        readyToShoot = false;
92        isCharging = false;
93       }
94      }
95     }
96    }
97
98    /*if(touch.phase == TouchPhase.Stationary){
99     if (shot != 0) {
100      UpdatePowerLevel ();
101     }
102    }else if(touch.phase == TouchPhase.Ended){
103     if (shot != 0) {
104      GameObject newCannonBullet = Instantiate (cannonBullet, cannonTip.position, Quaternion.identity) as GameObject;
105      newCannonBullet.transform.GetComponent ().AddForce (cannonTip.right * powerLevel.value, ForceMode2D.Impulse);
106      if (GameController.instance != null && MusicController.instance != null) {
107       if (GameController.instance.isMusicOn) {
108        audioSource.PlayOneShot (cannonShot);
109       }
110      }
111      shot--;
112      powerLevel.value = 0;
113      readyToShoot = false;
114     }
115    }*/
116
117   }
118  }
File name: MovingObject.cs Copy
34   protected bool Move (int xDir, int yDir, out RaycastHit2D hit)
35   {
36    //Store start position to move from, based on objects current transform position.
37    Vector2 start = transform.position;
38
39    // Calculate end position based on the direction parameters passed in when calling Move.
40    Vector2 end = start + new Vector2 (xDir, yDir);
41
42    //Disable the boxCollider so that linecast doesn't hit this object's own collider.
43    boxCollider.enabled = false;
44
45    //Cast a line from start point to end point checking collision on blockingLayer.
46    hit = Physics2D.Linecast (start, end, blockingLayer);
47
48    //Re-enable boxCollider after linecast
49    boxCollider.enabled = true;
50
51    //Check if anything was hit
52    if(hit.transform == null)
53    {
54     //If nothing was hit, start SmoothMovement co-routine passing in the Vector2 end as destination
55     StartCoroutine (SmoothMovement (end));
56
57     //Return true to say that Move was successful
58     return true;
59    }
60
61    //If something was hit, return false, Move was unsuccesful.
62    return false;
63   }
File name: MovingObject.cs Copy
93   protected virtual void AttemptMove (int xDir, int yDir)
95   {
96    //Hit will store whatever our linecast hits when Move is called.
97    RaycastHit2D hit;
98
99    //Set canMove to true if Move was successful, false if failed.
100    bool canMove = Move (xDir, yDir, out hit);
101
102    //Check if nothing was hit by linecast
103    if(hit.transform == null)
104     //If nothing was hit, return and don't execute further code.
105     return;
106
107    //Get a component reference to the component of type T attached to the object that was hit
108    T hitComponent = hit.transform.GetComponent ();
109
110    //If canMove is false and hitComponent is not equal to null, meaning MovingObject is blocked and has hit something it can interact with.
111    if(!canMove && hitComponent != null)
112
113     //Call the OnCantMove function and pass it hitComponent as a parameter.
114     OnCantMove (hitComponent);
115   }
File name: Player.cs Copy
131   protected override void AttemptMove (int xDir, int yDir)
132   {
133    //Every time player moves, subtract from food points total.
134    food--;
135
136    //Update food text display to reflect current score.
137    foodText.text = "Food: " + food;
138
139    //Call the AttemptMove method of the base class, passing in the component T (in this case Wall) and x and y direction to move.
140    base.AttemptMove (xDir, yDir);
141
142    //Hit allows us to reference the result of the Linecast done in Move.
143    RaycastHit2D hit;
144
145    //If Move returns true, meaning Player was able to move into an empty space.
146    if (Move (xDir, yDir, out hit))
147    {
148     //Call RandomizeSfx of SoundManager to play the move sound, passing in two audio clips to choose from.
149     SoundManager.instance.RandomizeSfx (moveSound1, moveSound2);
150    }
151
152    //Since the player has moved and lost food points, check if the game has ended.
153    CheckIfGameOver ();
154
155    //Set the playersTurn boolean of GameManager to false now that players turn is over.
156    GameManager.instance.playersTurn = false;
157   }

RaycastHit2D 191 lượt xem

Gõ tìm kiếm nhanh...