ForceMode2D









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

Featured Snippets


File name: CoinMotion.cs Copy
11  void Awake(){
12   myRigidBody = GetComponent ();
13   myRigidBody.AddForce (new Vector2(Random.Range(-0.5f, 0.5f), force), ForceMode2D.Impulse);
14  }
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: Cannon.cs Copy
120  void CannonShoot(){
121   if (Input.GetKey (KeyCode.Space)) {
122    if(shot != 0){
123     UpdatePowerLevel ();
124    }
125   }else if(Input.GetKeyUp(KeyCode.Space)){
126    if (shot != 0) {
127     GameObject newCannonBullet = Instantiate (cannonBullet, cannonTip.position, Quaternion.identity) as GameObject;
128     newCannonBullet.transform.GetComponent ().AddForce (cannonTip.right * powerLevel.value, ForceMode2D.Impulse);
129     if (GameController.instance != null && MusicController.instance != null) {
130      if (GameController.instance.isMusicOn) {
131       audioSource.PlayOneShot (cannonShot);
132      }
133     }
134     shot--;
135     powerLevel.value = 0;
136     readyToShoot = false;
137    }
138   }
139
140  }
File name: BallScript.cs Copy
24     private void GiveBoostIfMovingOnXorYAxis()
25     {
26         if (Mathf.Abs(GetComponent().velocity.x - 0.2f) <= 0.2f)
27         {
28             //left or right?
29             bool right = Random.Range(-1.0f, 1.0f) >= 0;
30             GetComponent().AddForce(new Vector2(right ? 5.0f : -5.0f, 0), ForceMode2D.Impulse);
31         }
32
33         if (Mathf.Abs(GetComponent().velocity.y - 0.2f) <= 0.2f)
34         {
35             //up or down?
36             bool down = Random.Range(-1.0f, 1.0f) >= 0;
37             GetComponent().AddForce(new Vector2(0, down ? 5.0f : -5.0f), ForceMode2D.Impulse);
38         }
39     }

ForceMode2D 130 lượt xem

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