Vector2









How do I use Vector2
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: JumpAndRunMovement.cs Copy
68     void UpdateMovement()
69     {
70         Vector2 movementVelocity = m_Body.velocity;
71
72         if( Input.GetAxisRaw( "Horizontal" ) > 0.5f )
73         {
74             movementVelocity.x = Speed;
75
76         }
77         else if( Input.GetAxisRaw( "Horizontal" ) < -0.5f )
78         {
79             movementVelocity.x = -Speed;
80         }
81         else
82         {
83             movementVelocity.x = 0;
84         }
85
86         m_Body.velocity = movementVelocity;
87     }
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: 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: BallControl.cs Copy
25  void ResetBall() {
26   vel = new Vector2 (0, 0);
27   rb2d.velocity = vel;
28   transform.position = Vector2.zero;
29  }
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: PhotonRigidbody2DView.cs Copy
32     void OnPhotonSerializeView( PhotonStream stream, PhotonMessageInfo info )
33     {
34         if( stream.isWriting == true )
35         {
36             if( m_SynchronizeVelocity == true )
37             {
38                 stream.SendNext( m_Body.velocity );
39             }
40
41             if( m_SynchronizeAngularVelocity == true )
42             {
43                 stream.SendNext( m_Body.angularVelocity );
44             }
45         }
46         else
47         {
48             if( m_SynchronizeVelocity == true )
49             {
50                 m_Body.velocity = (Vector2)stream.ReceiveNext();
51             }
52
53             if( m_SynchronizeAngularVelocity == true )
54             {
55                 m_Body.angularVelocity = (float)stream.ReceiveNext();
56             }
57         }
58     }
File name: CustomTypes.cs Copy
32     internal static void Register()
33     {
34         PhotonPeer.RegisterType(typeof(Vector2), (byte)'W', SerializeVector2, DeserializeVector2);
35         PhotonPeer.RegisterType(typeof(Vector3), (byte)'V', SerializeVector3, DeserializeVector3);
36         PhotonPeer.RegisterType(typeof(Quaternion), (byte)'Q', SerializeQuaternion, DeserializeQuaternion);
37         PhotonPeer.RegisterType(typeof(PhotonPlayer), (byte)'P', SerializePhotonPlayer, DeserializePhotonPlayer);
38     }
File name: CustomTypes.cs Copy
79     private static short SerializeVector2(MemoryStream outStream, object customobject)
80     {
81         Vector2 vo = (Vector2)customobject;
82         lock (memVector2)
83         {
84             byte[] bytes = memVector2;
85             int index = 0;
86             Protocol.Serialize(vo.x, bytes, ref index);
87             Protocol.Serialize(vo.y, bytes, ref index);
88             outStream.Write(bytes, 0, 2 * 4);
89         }
90
91         return 2 * 4;
92     }
File name: CustomTypes.cs Copy
94     private static object DeserializeVector2(MemoryStream inStream, short length)
95     {
96         Vector2 vo = new Vector2();
97         lock (memVector2)
98         {
99             inStream.Read(memVector2, 0, 2 * 4);
100             int index = 0;
101             Protocol.Deserialize(out vo.x, memVector2, ref index);
102             Protocol.Deserialize(out vo.y, memVector2, ref index);
103         }
104
105         return vo;
106     }

Vector2 119 lượt xem

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