Raycast









How do I use Raycast
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: ClickDetector.cs Copy
7  void Update()
8     {
9         // if this player is not "it", the player can't tag anyone, so don't do anything on collision
10         if (PhotonNetwork.player.ID != GameLogic.playerWhoIsIt)
11         {
12             return;
13         }
14
15         if (Input.GetButton("Fire1"))
16         {
17             GameObject goPointedAt = RaycastObject(Input.mousePosition);
18
19             if (goPointedAt != null && goPointedAt != this.gameObject && goPointedAt.name.Equals("monsterprefab(Clone)", StringComparison.OrdinalIgnoreCase))
20             {
21                 PhotonView rootView = goPointedAt.transform.root.GetComponent();
22                 GameLogic.TagPlayer(rootView.owner.ID);
23             }
24         }
25  }
File name: ClickDetector.cs Copy
27     private GameObject RaycastObject(Vector2 screenPos)
28     {
29         RaycastHit info;
30         #if UNITY_3_5
31         Camera cam = Camera.mainCamera;
32         #else
33         Camera cam = Camera.main;
34         #endif
35
36         if (Physics.Raycast(cam.ScreenPointToRay(screenPos), out info, 200))
37         {
38             return info.collider.gameObject;
39         }
40
41         return null;
42     }
File name: InputToEvent.cs Copy
32     void Update()
33     {
34         if( DetectPointedAtGameObject )
35         {
36             goPointedAt = RaycastObject( Input.mousePosition );
37         }
38
39         if( Input.touchCount > 0 )
40         {
41             Touch touch = Input.GetTouch( 0 );
42             this.currentPos = touch.position;
43
44             if( touch.phase == TouchPhase.Began )
45             {
46                 Press( touch.position );
47             }
48             else if( touch.phase == TouchPhase.Ended )
49             {
50                 Release( touch.position );
51             }
52
53             return;
54         }
55
56         currentPos = Input.mousePosition;
57         if( Input.GetMouseButtonDown( 0 ) )
58         {
59             Press( Input.mousePosition );
60         }
61         if( Input.GetMouseButtonUp( 0 ) )
62         {
63             Release( Input.mousePosition );
64         }
65
66         if( Input.GetMouseButtonDown( 1 ) )
67         {
68             pressedPosition = Input.mousePosition;
69             lastGo = RaycastObject( pressedPosition );
70             if( lastGo != null )
71             {
72                 lastGo.SendMessage( "OnPressRight", SendMessageOptions.DontRequireReceiver );
73             }
74         }
75     }
File name: InputToEvent.cs Copy
78     private void Press( Vector2 screenPos )
79     {
80         pressedPosition = screenPos;
81         this.Dragging = true;
82
83         lastGo = RaycastObject( screenPos );
84         if( lastGo != null )
85         {
86             lastGo.SendMessage( "OnPress", SendMessageOptions.DontRequireReceiver );
87         }
88     }
File name: InputToEvent.cs Copy
90     private void Release( Vector2 screenPos )
91     {
92         if( lastGo != null )
93         {
94             GameObject currentGo = RaycastObject( screenPos );
95             if( currentGo == lastGo ) lastGo.SendMessage( "OnClick", SendMessageOptions.DontRequireReceiver );
96             lastGo.SendMessage( "OnRelease", SendMessageOptions.DontRequireReceiver );
97             lastGo = null;
98         }
99
100         pressedPosition = Vector2.zero;
101         this.Dragging = false;
102     }
File name: InputToEvent.cs Copy
104     private GameObject RaycastObject( Vector2 screenPos )
105     {
106         RaycastHit info;
107         if( Physics.Raycast( m_Camera.ScreenPointToRay( screenPos ), out info, 200 ) )
108         {
109             inputHitPos = info.point;
110             return info.collider.gameObject;
111         }
112
113         return null;
114     }
File name: Finder.cs Copy
8  public static GameObject RayHitFromScreen(Vector3 hitPosition) {
9   Ray ray = Converter.ScreenPointToRay(hitPosition);
10   RaycastHit hit;
11   if (Physics.Raycast(ray, out hit, float.PositiveInfinity, GameManager.Instance.CLickableMask)) {
12      return hit.transform.gameObject;
13   }
14
15   return null;
16  }
File name: Finder.cs Copy
26  public static IClickable IClickableRayHitFromScreen(Vector3 hitPosition) {
27   Ray ray = Converter.ScreenPointToRay(hitPosition);
28   RaycastHit hit;
29   if (Physics.Raycast(ray, out hit, float.PositiveInfinity, GameManager.Instance.CLickableMask)) {
30    return hit.transform.gameObject.GetComponent(typeof(IClickable)) as IClickable;
31   }
32
33   return null;
34  }
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  }

Raycast 153 lượt xem

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