Anda di halaman 1dari 6

var animation: Animation;

animation.Play();
var transform: Transform;
transform.Translate(1, 1, 1);
static
function Instantiate(original: Object, position: Vector3, rotation: Quaternion): Object;
static function Instantiate(original: Object): Object;


// Instantiates 10 copies of prefab each 2 units apart from each
other
var prefab : Transform;
for (var i : int = 0;i < 10; i++) {
Instantiate (prefab, Vector3(i * 2.0, 0, 0),
Quaternion.identity);
}
Instantiate is most commonly used to instantiate projectiles, AI Enemies, particle
explosions or wrecked object replacements.
// Instantiate a rigidbody then set the velocity
var projectile : Rigidbody;
function Update () {
// Ctrl was pressed, launch a projectile
if (Input.GetButtonDown("Fire1")) {
// Instantiate the projectile at the position
and rotation of this transform
var clone : Rigidbody;
clone = Instantiate(projectile,
transform.position, transform.rotation);

// Give the cloned object an initial velocity
along the current
// object's Z axis
clone.velocity = transform.TransformDirection
(Vector3.forward * 10);
}
}
Instantiate can also clone script instances directly. The entire game object hierarchy will be
cloned and the cloned script instance will be returned.
// Instantiate a prefab with an attached Missile script
var projectile : Missile;
function Update () {
// Ctrl was pressed, launch a projectile
if (Input.GetButtonDown("Fire1")) {
// Instantiate the projectile at the position
and rotation of this transform
var clone : Missile;
clone = Instantiate(projectile,
transform.position, transform.rotation);

// Set the missiles timeout destructor to 5
clone.timeoutDestructor = 5;
}
}


1. //You could put as many of these as you want for more animations. 1 - Walk 2 -
Sprint 3 - Crouch
2. var animationToPlay1 : String;
3. var keyToPress1 : KeyCode;
4. var animationToPlay2 : String;
5. var keyToPress2 : KeyCode;
6. var animationToPlay3 : String;
7. var keyToPress3 : KeyCode;
8.
9.
10. function Update()
11. {
12. if(Input.GetKey(keyToPress1))
13. {
14. animation.CrossFade(animationToPlay1);
15. }
16. if(Input.GetKey(keyToPress2))
17. {
18. animation.CrossFade(animationToPlay2);
19. }
20. if(Input.GetKey(keyToPress3))
21. {
22. animation.CrossFade(animationToPlay3);
23. }
24. }


Touch code


1. using UnityEngine;
2. using System.Collections;
3.
4. public class TouchLogic : MonoBehaviour
5. {
6. public static int currTouch = 0;//so other scripts can
know what touch is currently on screen
7. private Ray ray;//this will be the ray that we cast from
our touch into the scene
8. private RaycastHit rayHitInfo = new RaycastHit();//return
the info of the object that was hit by the ray
9. [HideInInspector]
10. public int touch2Watch = 64;
11.
12. void Update ()
13. {
14. //is there a touch on screen?
15. if(Input.touches.Length <= 0)
16. {
17. //if no touches then execute this code
18. }
19. else //if there is a touch
20. {
21. //loop through all the the touches on screen
22. for(int i = 0; i < Input.touchCount; i++)
23. {
24. currTouch = i;
25. Debug.Log(currTouch);
26. //executes this code for current touch (i) on screen
27. if(this.guiTexture != null &&
(this.guiTexture.HitTest(Input.GetTouch(i).position)))
28. {
29. //if current touch hits our guitexture, run this code
30. if(Input.GetTouch(i).phase == TouchPhase.Began)
31. {
32. //need to send message b/c function is not present in
this script
33. //OnTouchBegan();
34. this.SendMessage("OnTouchBegan");
35. }
36. if(Input.GetTouch(i).phase == TouchPhase.Ended)
37. {
38. //OnTouchEnded();
39. this.SendMessage("OnTouchEnded");
40. }
41. if(Input.GetTouch(i).phase == TouchPhase.Moved)
42. {
43. //OnTouchMoved();
44. this.SendMessage("OnTouchMoved");
45. }
46. if(Input.GetTouch(i).phase == TouchPhase.Stationary)
47. {
48. //OnTouchStayed();
49. this.SendMessage("OnTouchStayed");
50. }
51. }
52.
53. //outside so it doesn't require the touch to be over
the guitexture
54. ray =
Camera.mainCamera.ScreenPointToRay(Input.GetTouch(i).position);/
/creates ray from screen point position
55. switch(Input.GetTouch(i).phase)
56. {
57. case TouchPhase.Began:
58. //OnTouchBeganAnywhere();
59. this.SendMessage("OnTouchBeganAnyWhere");
60. if(Physics.Raycast(ray, out rayHitInfo))
61.
rayHitInfo.transform.gameObject.SendMessage("OnTouchBegan3D");
62. break;
63. case TouchPhase.Ended:
64. //OnTouchEndedAnywhere();
65. this.SendMessage("OnTouchEndedAnywhere");
66. if(Physics.Raycast(ray, out rayHitInfo))
67.
rayHitInfo.transform.gameObject.SendMessage("OnTouchEnded3D");
68. break;
69. case TouchPhase.Moved:
70. //OnTouchMovedAnywhere();
71. this.SendMessage("OnTouchMovedAnywhere");
72. if(Physics.Raycast(ray, out rayHitInfo))
73.
rayHitInfo.transform.gameObject.SendMessage("OnTouchMoved3D");
74. break;
75. case TouchPhase.Stationary:
76. //OnTouchStayedAnywhere();
77. this.SendMessage("OnTouchStayedAnywhere");
78. if(Physics.Raycast(ray, out rayHitInfo))
79.
rayHitInfo.transform.gameObject.SendMessage("OnTouchStayed3D");
80. break;
81. }
82. }
83. }
84. }
85. }





Javascript movimiento teclas ejemplo ataque y aplicacin audio a player

var animationToPlay : String;
var keyToPress : KeyCode;
var clip: AudioClip;


function Update()
{
if(Input.GetKey(keyToPress))
animation.CrossFade(animationToPlay);
else(audio.Play());
}

Anda mungkin juga menyukai