JumpForce
How do I use Jump 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: 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: PlayerMoveScript.cs
Copy
50 void Update () {
51
52 grounded = Physics2D.OverlapCircle(groundCheck.position, groundChecked, isGround);
53
54 if(transform.position.x > speedCount) {
55 speedCount += speedIncrease;
56 speedIncrease = speedIncrease * speedMultiplier;
57 moveSpeed = moveSpeed * speedMultiplier;
58 }
59
60 rigidbody.velocity = new Vector2 (moveSpeed, rigidbody.velocity.y);
61
62 if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) {
63
64 if(grounded) {
65 rigidbody.velocity = new Vector2 (rigidbody.velocity.x, jumpForce);
66 anim.SetTrigger ("Jump");
67 stopJumping = false;
68 }
69 }
70
71 if((Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0)) && !stopJumping) {
72 if(jumpTimeCounter > 0) {
73 rigidbody.velocity = new Vector2 (rigidbody.velocity.x, jumpForce);
74 jumpTimeCounter -= Time.deltaTime;
75 }
76 }
77
78 if(Input.GetKeyUp (KeyCode.Space) || Input.GetMouseButtonUp(0)) {
79 jumpTimeCounter = 0;
80 stopJumping = true;
81 }
82
83 if(grounded) {
84 jumpTimeCounter = jumpTime;
85 }
86
87
88 scoreCount += pointsPerSeconds = Time.deltaTime;
89
90 if(scoreCount > highScoreCount) {
91 highScoreCount = scoreCount;
92
93 }
94 scoreText.text = "Score: " + Mathf.Round(scoreCount);
95
96 }
File name: PlayerMoveScript.cs
Copy
98 void OnCollisionEnter2D(Collision2D target) {
99 if(target.gameObject.tag == "died" || target.gameObject.tag == "Crates") {
100 jumpForce = 0;
101 scoreCount = 0;
102 anim.SetTrigger ("Died");
103 scoreText.gameObject.SetActive (false);
104 audioSource.PlayOneShot (diedClip);
105 FindObjectOfType
106 FindObjectOfType
107 FindObjectOfType
108 }
109 }
JumpForce 124 lượt xem
Gõ tìm kiếm nhanh...