AddForce









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

Featured Snippets


File name: JumpAndRunMovement.cs Copy
52     void UpdateJumping()
53     {
54         if( Input.GetKey( KeyCode.Space ) == true && m_IsGrounded == true )
55         {
56             m_Animator.SetTrigger( "IsJumping" );
57             m_Body.AddForce( Vector2.up * JumpForce );
58             m_PhotonView.RPC( "DoJump", PhotonTargets.Others );
59         }
60     }
File name: BallControl.cs Copy
10  void GoBall() {
11   float rand = Random.Range (0, 2);
12   if (rand < 1) {
13    rb2d.AddForce (new Vector2 (20, -15));
14   } else {
15    rb2d.AddForce (new Vector2 (-20, -15));
16   }
17  }
File name: MoveByKeys.cs Copy
37     public void FixedUpdate()
38     {
39         if (!photonView.isMine)
40         {
41             return;
42         }
43
44         if (Input.GetKey(KeyCode.A))
45         {
46             transform.position += Vector3.left*(this.Speed*Time.deltaTime);
47         }
48
49         if (Input.GetKey(KeyCode.D))
50         {
51             transform.position += Vector3.right*(this.Speed*Time.deltaTime);
52         }
53
54         // jumping has a simple "cooldown" time but you could also jump in the air
55         if (this.jumpingTime <= 0.0f)
56         {
57             if (this.body != null || this.body2d != null)
58             {
59                 // obj has a Rigidbody and can jump (AddForce)
60                 if (Input.GetKey(KeyCode.Space))
61                 {
62                     this.jumpingTime = this.JumpTimeout;
63
64                     Vector2 jump = Vector2.up*this.JumpForce;
65                     if (this.body2d != null)
66                     {
67                         this.body2d.AddForce(jump);
68                     }
69                     else if (this.body != null)
70                     {
71                         this.body.AddForce(jump);
72                     }
73                 }
74             }
75         }
76         else
77         {
78             this.jumpingTime -= Time.deltaTime;
79         }
80
81         // 2d objects can't be moved in 3d "forward"
82         if (!this.isSprite)
83         {
84             if (Input.GetKey(KeyCode.W))
85             {
86                 transform.position += Vector3.forward*(this.Speed*Time.deltaTime);
87             }
88
89             if (Input.GetKey(KeyCode.S))
90             {
91                 transform.position -= Vector3.forward*(this.Speed*Time.deltaTime);
92             }
93         }
94     }
File name: PlayerCtrl.cs Copy
102     void Jump()
103     {
104         if (isGrounded)
105         {
106             isJumping = true;
107             rb.AddForce(new Vector2(0, jumpSpeed)); // simply make the player jump in the y axis or upwards
108             anim.SetInteger("State", 2);
109
110             Invoke("EnableDoubleJump", delayForDoubleJump);
111         }
112
113         if (canDoubleJump && !isGrounded)
114         {
115             rb.velocity = Vector2.zero;
116             rb.AddForce(new Vector2(0, jumpSpeed)); // simply make the player jump in the y axis or upwards
117             anim.SetInteger("State", 2);
118
119             canDoubleJump = false;
120         }
121     }
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     }
File name: GameScreen.cs Copy
809         public void AnimalcollisionWithBullet(GameObject animalObject, GameObject bulletObject)
810         {
811             if (animalObject.GetComponent().isStanding) return;
812             SkillType type = bulletObject.GetComponent().skillType;
813             int animalIndex = animalObject.GetComponent().animalIndex;
814             switch (type)
815             {
816                 case SkillType.LUA:
817                     {
818                         GameObject effectObject = (GameObject)Instantiate(Resources.Load("Effects/LUA"));
819                         effectObject.transform.parent = effectLayer.transform;
820                         effectObject.GetComponent().mapObjectTransform = animalObject.transform;
821                         Destroy(bulletObject);
822                         animalObject.GetComponent().setStand(true, 2, Animal.STATE_FIRE);
823                         animalObject.GetComponent().AddForce(new Vector2(0, -300));
824                         if (animalIndex == 0)
825                         {
826                             showRevivalTask(2, EmotionType.CUOIDEU);
827                         }
828                     }
829                     break;
830                 case SkillType.DONGBANG:
831                     {
832                         GameObject effectObject = (GameObject)Instantiate(Resources.Load("Effects/DONGBANG"));
833                         effectObject.transform.parent = effectLayer.transform;
834                         effectObject.GetComponent().mapObjectTransform = animalObject.transform;
835                         Destroy(bulletObject);
836                         animalObject.GetComponent().setStand(true,2, Animal.STATE_RUN);
837                         animalObject.GetComponent().AddForce(new Vector2(0, -300));
838                         if (animalIndex == 0)
839                         {
840                             showRevivalTask(2, EmotionType.HOAMAT);
841                         }
842                     }
843                     break;
844                 case SkillType.THIENTHACH:
845                     {
846                         {
847                             GameObject effectObject = (GameObject)Instantiate(Resources.Load("Effects/THIENTHACH"));
848                             effectObject.transform.parent = effectLayer.transform;
849                             effectObject.GetComponent().mapObjectTransform = animalObject.transform;
850                         }
851                         {
852                             GameObject effectObject = (GameObject)Instantiate(Resources.Load("Effects/CHOANG"));
853                             effectObject.transform.parent = effectLayer.transform;
854                             effectObject.GetComponent().mapObjectTransform = animalObject.transform;
855                         }
856                         Destroy(bulletObject);
857                         animalObject.GetComponent().setStand(true, 2, Animal.STATE_SHOCK);
858                         animalObject.GetComponent().AddForce(new Vector2(0, -300));
859                         if (animalIndex == 0)
860                         {
861                             showRevivalTask(2, EmotionType.HOAMAT);
862                         }
863                     }
864                     break;
865                 case SkillType.SET:
866                     {
867                         GameObject effectObject = (GameObject)Instantiate(Resources.Load("Effects/SET"));
868                         effectObject.transform.parent = effectLayer.transform;
869                         effectObject.GetComponent().mapObjectTransform = animalObject.transform;
870                         animalObject.GetComponent().setStand(true, 2, Animal.STATE_FIRE);
871                         animalObject.GetComponent().AddForce(new Vector2(0, -300));
872                         if (animalIndex == 0)
873                         {
874                             showRevivalTask(2, EmotionType.HOAMAT);
875                         }
876                     }
877                     break;
878                 case SkillType.BOM:
879                     {
880                         GameObject effectObject = (GameObject)Instantiate(Resources.Load("Effects/LUA"));
881                         effectObject.transform.parent = effectLayer.transform;
882                         effectObject.GetComponent().mapObjectTransform = animalObject.transform;
883                         Destroy(bulletObject);
884                         animalObject.GetComponent().setStand(true, 2, Animal.STATE_FIRE);
885                         animalObject.GetComponent().AddForce(new Vector2(0, -300));
886                     }
887                     break;
888                 case SkillType.DOICHO:
889                     {
890                         GameObject effectObject = (GameObject)Instantiate(Resources.Load("Effects/KHOI"));
891                         effectObject.transform.parent = effectLayer.transform;
892                         effectObject.GetComponent().mapObjectTransform = animalObject.transform;
893                         int animalIndexb = bulletObject.GetComponent().animalIndex;
894                         Destroy(bulletObject);
895                         GameObject animal2 = animals.getAnimal(animalIndexb);
896                         GameObject effectObject2 = (GameObject)Instantiate(Resources.Load("Effects/KHOI"));
897                         effectObject.transform.parent = effectLayer.transform;
898                         effectObject2.GetComponent().mapObjectTransform = animal2.transform;
899                         Vector3 p1 = animalObject.transform.localPosition;
900                         Vector3 p2 = animal2.transform.localPosition;
901                         animal2.transform.localPosition = new Vector3(p1.x, p1.y, p2.z);
902                         animalObject.transform.localPosition = new Vector3(p2.x, p2.y, p1.z);
903                     }
904                     break;
905             }
906         }
File name: PlayerMovement.cs Copy
37  void MoveLeft() {
38   float forceX = 0f;
39   float vel = Mathf.Abs (rigidbody.velocity.x);
40   if(vel < max)
41    forceX = -speed;
42
43   Vector3 temp = transform.localScale;
44
45   transform.localScale = temp;
46
47   rigidbody.AddForce(new Vector2(forceX, 0));
48  }

AddForce 281 lượt xem

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