LastPosition
How do I use Last Position
Below are practical examples compiled from projects for learning and reference purposes
Featured Snippets
Line | Code | Ex.. |
---|---|---|
53 | Vector3 movementVector = transform.position - m_LastPosition; | 1 |
20 | lastPosition = touch.position; | 2 |
File name: RPGMovement.cs
Copy
51 void UpdateAnimation()
52 {
53 Vector3 movementVector = transform.position - m_LastPosition;
54
55 float speed = Vector3.Dot( movementVector.normalized, transform.forward );
56 float direction = Vector3.Dot( movementVector.normalized, transform.right );
57
58 if( Mathf.Abs( speed ) < 0.2f )
59 {
60 speed = 0f;
61 }
62
63 if( speed > 0.6f )
64 {
65 speed = 1f;
66 direction = 0f;
67 }
68
69 if( speed >= 0f )
70 {
71 if( Mathf.Abs( direction ) > 0.7f )
72 {
73 speed = 1f;
74 }
75 }
76
77 m_AnimatorSpeed = Mathf.MoveTowards( m_AnimatorSpeed, speed, Time.deltaTime * 5f );
78
79 m_Animator.SetFloat( "Speed", m_AnimatorSpeed );
80 m_Animator.SetFloat( "Direction", direction );
81
82 m_LastPosition = transform.position;
83 }
File name: SwipeListenerScript.cs
Copy
10 void Update () {
11 //transform.guiText.text = "my text";
12 int fingerCount = 0;
13 foreach (Touch touch in Input.touches) {
14 if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
15 fingerCount++;
16
17 if (touch.phase == TouchPhase.Began)
18 {
19 startPosition = touch.position;
20 lastPosition = touch.position;
21 }
22
23 if (touch.phase == TouchPhase.Moved )
24 {
25 lastPosition = touch.position;
26 }
27 if(touch.phase == TouchPhase.Ended)
28 {
29
30 if((startPosition.x - lastPosition.x) > 80) // left swipe
31 {
32 this.Swipe ("x", -1);
33 }
34 else if((startPosition.x - lastPosition.x) < -80) // right swipe
35 {
36 this.Swipe ("x", 1);
37 }
38 else if((startPosition.y - lastPosition.y) < -80 ) // up swipe
39 {
40 this.Swipe ("y", 1);
41 }
42 else if((startPosition.y - lastPosition.y) > 80 ) // down swipe
43 {
44 this.Swipe ("y", -1);
45 }
46
47 }
48 }
49 }
LastPosition 115 lượt xem
Gõ tìm kiếm nhanh...