1 using UnityEngine;
2 using
System.Collections;
3
4 public
class HatController : MonoBehaviour {
5
6     
public Camera cam;
7     
private float maxWidth;
8     
private bool canControl;
9
10     
// Use this for initialization
11     
void Start () {
12     
13         
if (cam == null) {
14             cam = Camera.main;
15         }
16         canControl =
false;
17         Vector3 upperCorner =
new Vector3 (Screen.width, Screen.height, 0.0f);
18         Vector3 targetWidth = cam.ScreenToWorldPoint(upperCorner);
19         
float hatWidth = GetComponent<Renderer>().bounds.extents.x;
20         maxWidth = targetWidth.x-hatWidth;
21     }
22     
23     
// Update is called once per physics timestep
24     
void FixedUpdate () {
25         
if (canControl) {
26             Vector3 rawPosition = cam.ScreenToWorldPoint (Input.mousePosition);
27             Vector3 targetPosition =
new Vector3 (rawPosition.x, 0.0f, 0.0f);
28             
float targetWidth = Mathf.Clamp (targetPosition.x, -maxWidth, maxWidth);
29             targetPosition =
new Vector3 (targetWidth, targetPosition.y, targetPosition.z);
30             GetComponent<Rigidbody2D> ().MovePosition (targetPosition);
31         }
32     }
33
34     
public void toggledControl (bool toggle) {
35         canControl = toggle;
36     }
37 }



Full source code bắt bóng (chiếc mũ tí hon tung hoành khắp nơi) 23.719 lượt xem

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