Physics
How do I use Physics
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
File name: Demo2DJumpAndRun.cs
Copy
6 void OnJoinedRoom()
7 {
8 if( PhotonNetwork.isMasterClient == false )
9 {
10 return;
11 }
12
13 PhotonNetwork.InstantiateSceneObject( "Physics Box", new Vector3( -4.5f, 5.5f, 0 ), Quaternion.identity, 0, null );
14 PhotonNetwork.InstantiateSceneObject( "Physics Box", new Vector3( -4.5f, 4.5f, 0 ), Quaternion.identity, 0, null );
15 PhotonNetwork.InstantiateSceneObject( "Physics Box", new Vector3( -4.5f, 3.5f, 0 ), Quaternion.identity, 0, null );
16
17 PhotonNetwork.InstantiateSceneObject( "Physics Box", new Vector3( 4.5f, 5.5f, 0 ), Quaternion.identity, 0, null );
18 PhotonNetwork.InstantiateSceneObject( "Physics Box", new Vector3( 4.5f, 4.5f, 0 ), Quaternion.identity, 0, null );
19 PhotonNetwork.InstantiateSceneObject( "Physics Box", new Vector3( 4.5f, 3.5f, 0 ), Quaternion.identity, 0, null );
20 }
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: HubGui.cs
Copy
28 void OnGUI()
29 {
30 GUI.skin = this.Skin;
31 GUILayout.Space(10);
32
33 GUILayout.BeginHorizontal();
34 GUILayout.Space(10);
35 scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.Width(320));
36
37 GUILayout.Label("Basics", m_Headline);
38 if (GUILayout.Button("Demo Boxes", GUILayout.Width(280)))
39 {
40 demoDescription = "
41 demoBtn = new DemoBtn() { Text = "Start", Link = "DemoBoxes-Scene" };
42 }
43 if (GUILayout.Button("Demo Worker", GUILayout.Width(280)))
44 {
45 demoDescription = "
46 demoBtn = new DemoBtn() { Text = "Start", Link = "DemoWorker-Scene" };
47 }
48 if (GUILayout.Button("Movement Smoothing", GUILayout.Width(280)))
49 {
50 demoDescription = "
51 demoBtn = new DemoBtn() { Text = "Start", Link = "DemoSynchronization-Scene" };
52 }
53
54 GUILayout.Label("Advanced", m_Headline);
55 if (GUILayout.Button("Ownership Transfer", GUILayout.Width(280)))
56 {
57 demoDescription = "
58 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoChangeOwner-Scene" };
59 this.webLink = new DemoBtn();
60 }
61 if (GUILayout.Button("Pickup, Teams, Scores", GUILayout.Width(280)))
62 {
63 demoDescription = "
64 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoPickup-Scene" };
65 this.webLink = new DemoBtn();
66 }
67
68 GUILayout.Label("Feature Demos", m_Headline);
69 if (GUILayout.Button("Chat", GUILayout.Width(280)))
70 {
71 demoDescription = "
72 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoChat-Scene" };
73 this.webLink = new DemoBtn();
74 }
75 if (GUILayout.Button("RPG Movement", GUILayout.Width(280)))
76 {
77 demoDescription = "
78 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoRPGMovement-Scene" };
79 this.webLink = new DemoBtn();
80 }
81 if (GUILayout.Button("Mecanim Animations", GUILayout.Width(280)))
82 {
83 demoDescription = "
84 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoMecanim-Scene" };
85 this.webLink = new DemoBtn();
86 }
87 if (GUILayout.Button("2D Game", GUILayout.Width(280)))
88 {
89 demoDescription = "
90 this.demoBtn = new DemoBtn() { Text = "Start", Link = "Demo2DJumpAndRunWithPhysics-Scene" };
91 this.webLink = new DemoBtn();
92 }
93 if (GUILayout.Button("Friends & Authentication", GUILayout.Width(280)))
94 {
95 demoDescription = "
96 this.demoBtn = new DemoBtn() { Text = "Start", Link = "DemoFriends-Scene" };
97 this.webLink = new DemoBtn();
98 }
99
100 GUILayout.Label("Tutorial", m_Headline);
101 if (GUILayout.Button("Marco Polo Tutorial", GUILayout.Width(280)))
102 {
103 demoDescription = "
104 this.demoBtn = new DemoBtn() { Text = "Start", Link = "MarcoPolo-Scene" };
105 this.webLink = new DemoBtn() { Text = "Open Tutorial (www)", Link = "http://tinyurl.com/nmylf44" };
106 }
107 GUILayout.EndScrollView();
108
109 GUILayout.BeginVertical(GUILayout.Width(Screen.width - 345));
110 GUILayout.Label(demoDescription);
111 GUILayout.Space(10);
112 if (!string.IsNullOrEmpty(this.demoBtn.Text))
113 {
114 if (GUILayout.Button(this.demoBtn.Text))
115 {
116 Application.LoadLevel(this.demoBtn.Link);
117 }
118 }
119 if (!string.IsNullOrEmpty(this.webLink.Text))
120 {
121 if (GUILayout.Button(this.webLink.Text))
122 {
123 Application.OpenURL(this.webLink.Link);
124 }
125 }
126 GUILayout.EndVertical();
127
128
129 GUILayout.EndHorizontal();
130 }
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
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: 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: PlayerCtrl.cs
Copy
37 void Update () {
38 isGrounded = Physics2D.OverlapBox(new Vector2(feet.position.x, feet.position.y), new Vector2(boxWidth, boxHeight), 360.0f, whatIsGround);
39
40 float playerSpeed = Input.GetAxisRaw("Horizontal"); // value will be 1, -1 or 0
41
42 playerSpeed *= speedBoost;
43
44 if (playerSpeed != 0)
45 MovePlayer(playerSpeed);
46 else
47 StopMoving();
48
49 if (Input.GetButtonDown("Jump"))
50 Jump();
51
52 if (Input.GetButtonDown("Fire1"))
53 {
54 FireBullets();
55 }
56
57 ShowFalling();
58
59 if (leftPressed)
60 MovePlayer(-speedBoost);
61
62 if (rightPressed)
63 MovePlayer(speedBoost);
64 }
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
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
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 }
Download file with original file name:Physics
Physics 105 lượt xem
Gõ tìm kiếm nhanh...