Unity3D 學習教程 12 C# 發射炮彈,
建立一個炮彈
一個球體
雙擊腳本
進入編輯器
1 using UnityEngine;
2 using System.Collections;
3
4 public class acc : MonoBehaviour {
5
6 // Use this for initialization
7 public Transform Q;
8 int speed=50;
9 void Start () {
10
11 }
12
13 // Update is called once per frame
14 void Update () {
15 float x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;//左右移動
16 float z = Input.GetAxis("Vertical") * Time.deltaTime * speed;// 前後移動
17 //主攝像機物體 移動
18 transform.Translate(x,0,z);
19
20 if(Input.GetKeyDown(KeyCode.Mouse0))
21 {
22 Transform n = (Transform)Instantiate(Q,transform.position,transform.rotation);
23 Vector3 f = transform.TransformDirection(Vector3.forward);
24 n.rigidbody.AddForce(f*2000);
25 }
26
27 print(x+"--"+z);
28 }
29 }
public Transform Q;
建立一個炮彈 Q
Transform
Transform 變換
Inherits from Component,IEnumerable
Position, rotation and scale of an object.
物體的位置、旋轉和縮放。
Every object in a scene has a Transform. It's used to store and manipulate the position, rotation and scale of the object. Every Transform can have a parent, which allows you to apply position, rotation and scale hierarchically. This is the hierarchy seen in the Hierarchy pane. They also support enumerators so you can loop through children using:
場景中的每一個物體都有一個Transform。用於儲存並操控物體的位置、旋轉和縮放。每一個Transform可以有一個父級,允許你分層次應用位置、旋轉和縮放。可以在Hierarchy面板查看層次關系。他們也支持計數器(enumerator),因此你可以使用循環遍歷子物體。
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public void Awake() {
foreach (Transform child in transform) {
child.position += Vector3.up * 10.0F;
}
}
}
// Moves all transform children 10 units upwards!
//向上移動所有變換的子物體10個單位
for (var child : Transform in transform) {
child.position += Vector3.up * 10.0;
}
參見:Physics 類.
Variables變量
-
position
The position of the transform in world space. 在世界空間坐標transform的位置。
-
localPosition
Position of the transform relative to the parent transform. 相對於父級的變換的位置。
-
eulerAngles
The rotation as Euler angles in degrees. 旋轉作為歐拉角度。
-
localEulerAngles
The rotation as Euler angles in degrees relative to the parent transform's rotation. 旋轉作為歐拉角度,相對於父級的變換旋轉角度。
-
right
The red axis of the transform in world space. 在世界空間坐標變換的紅色軸。也就是x軸。
-
up
The green axis of the transform in world space. 在世界空間坐標變換的綠色軸。也就是y軸。
-
forward
The blue axis of the transform in world space. 在世界空間坐標變換的藍色軸。也就是z軸。
-
rotation
The rotation of the transform in world space stored as a Quaternion. 在世界空間坐標物體變換的旋轉角度作為Quaternion儲存。
-
localRotation
The rotation of the transform relative to the parent transform's rotation. 物體變換的旋轉角度相對於父級的物體變換的旋轉角度。
-
localScale
The scale of the transform relative to the parent. 相對於父級物體變換的縮放。
-
parent
The parent of the transform. 物體變換的父級。
-
worldToLocalMatrix
Matrix that transforms a point from world space into local space (Read Only). 矩陣變換的點從世界坐標轉為自身坐標(只讀)。
-
localToWorldMatrix
Matrix that transforms a point from local space into world space (Read Only). 矩陣變換的點從自身坐標轉為世界坐標(只讀)。
-
root
Returns the topmost transform in the hierarchy. 返回層次最高的變換。
-
childCount
The number of children the Transform has. 變換的子物體數量。
-
lossyScale
The global scale of the object (Read Only). 物體的全局縮放(只讀)。
Functions函數
-
Translate
Moves the transform in the direction and distance of translation. 移動transform在translation的方向和距離。
-
Rotate
Applies a rotation of eulerAngles.z degrees around the z axis, eulerAngles.x degrees around the x axis, and eulerAngles.y degrees around the y axis (in that order). 應用一個歐拉角的旋轉角度,eulerAngles.z度圍繞z軸,eulerAngles.x度圍繞x軸,eulerAngles.y度圍繞y軸(這樣的順序)。
-
RotateAround
Rotates the transform about axis passing through point in world coordinates by angle degrees. 按照angle度通過在世界坐標的point軸旋轉物體。
-
LookAt
Rotates the transform so the forward vector points at /target/'s current position. 旋轉物體,這樣向前向量指向target的當前位置。
-
TransformDirection
Transforms direction from local space to world space. 從自身坐標到世界坐標變換方向。
-
InverseTransformDirection
Transforms a direction from world space to local space. The opposite of Transform.TransformDirection. 變換方向從世界坐標到自身坐標。和Transform.TransformDirection相反。
-
TransformPoint
Transforms position from local space to world space. 變換位置從自身坐標到世界坐標。
-
InverseTransformPoint
Transforms position from world space to local space. The opposite of Transform.TransformPoint. 變換位置從世界坐標到自身坐標。和Transform.TransformPoint相反。
-
DetachChildren
Unparents all children. 所有子物體解除父子關系。
-
Find
Finds a child by name and returns it. 通過名字查找子物體並返回它。
-
IsChildOf
Is this transform a child of parent? 這個變換是父級的子物體?
Inherited members繼承成員
Inherited Variables繼承變量
-
transform
The Transform attached to this GameObject (null if there is none attached). Transform附加到GameObject(游戲物體)(如無附加則為空)。
-
rigidbody
The Rigidbody attached to this GameObject (null if there is none attached). Rigidbody附加到GameObject(游戲物體)(如無附加則為空)。
-
camera
The Camera attached to this GameObject (null if there is none attached). Camera附加到GameObject(游戲物體)(如無附加則為空)。
-
light
The Light attached to this GameObject (null if there is none attached). Light附加到GameObject(游戲物體)(如無附加則為空)。
-
animation
The Animation attached to this GameObject (null if there is none attached). Animation附加到GameObject(游戲物體)(如無附加則為空)。
-
constantForce
The ConstantForce attached to this GameObject (null if there is none attached). ConstantForce附加到GameObject(游戲物體)(如無附加則為空)。
-
renderer
The Renderer attached to this GameObject (null if there is none attached). Renderer附加到GameObject(游戲物體)(如無附加則為空)。
-
audio
The AudioSource attached to this GameObject (null if there is none attached). AudioSource附加到GameObject(游戲物體)(如無附加則為空)。
-
guiText
The GUIText attached to this GameObject (null if there is none attached). GUIText附加到GameObject(游戲物體)(如無附加則為空)。
-
networkView
The NetworkView attached to this GameObject (Read Only). (null if there is none attached) NetworkView附加到GameObject(游戲物體)(只讀)(如無附加則為空)。
-
guiTexture
The GUITexture attached to this GameObject (Read Only). (null if there is none attached) GUITexture附加到GameObject(游戲物體)(只讀)(如無附加則為空)。
-
collider
The Collider attached to this GameObject (null if there is none attached). Collider附加到GameObject(游戲物體)(如無附加則為空)。
-
hingeJoint
The HingeJoint attached to this GameObject (null if there is none attached). HingeJoint附加到GameObject(游戲物體)(如無附加則為空)。
-
particleEmitter
The ParticleEmitter attached to this GameObject (null if there is none attached). ParticleEmitter附加到GameObject(游戲物體)(如無附加則為空)。
-
gameObject
The game object this component is attached to. A component is always attached to a game object. 組件附加的游戲物體。一個組件總是被附加到一個游戲物體。
-
tag
The tag of this game object. 游戲物體的標簽。
-
name
The name of the object. //物體的名字
-
hideFlags
Should the object be hidden, saved with the scene or modifiable by the user? 物體是否被隱藏、保存在場景中或被用戶修改?
Inherited Functions繼承函數
-
GetComponent
Returns the component of Type type if the game object has one attached, null if it doesn't. 如果游戲物體有一個附加,則返回Type類型的組件,如果沒有則為null。
-
GetComponent.<T>
-
GetComponent
Returns the component with name type if the game object has one attached, null if it doesn't. 如果游戲物體有一個附加,則返回名字類型組件,如果沒有則為null。
-
GetComponentInChildren
Returns the component of Type type in the GameObject or any of its children using depth first search. 返回Type類型組件,在GameObject或它的任何子物體使用深度優先搜索,僅返回激活的組件。
-
GetComponentInChildren.<T>
-
GetComponentsInChildren
Returns all components of Type type in the GameObject or any of its children. 在GameObject或任何它的子物體,返回全部Type類型組件
-
GetComponentsInChildren.<T>
-
GetComponents
Returns all components of Type type in the GameObject. 在游戲物體返回全部Type類型組件。
-
GetComponents.<T>
-
CompareTag
Is this game object tagged tag? 游戲物體有被標記標簽麼?
-
SendMessageUpwards
Calls the method named methodName on every MonoBehaviour in this game object and on every ancestor of the behaviour 在游戲物體每一個MonoBehaviour和每一個behaviour的祖先上調用名為methodName的方法。
-
SendMessage
Calls the method named methodName on every MonoBehaviour in this game object. 在游戲物體每一個MonoBehaviour上調用名為methodName的方法。
-
BroadcastMessage
Calls the method named methodName on every MonoBehaviour in this game object or any of its children. 在游戲物體每一個MonoBehaviour和它的全部子物體上調用名為methodName的方法。
-
GetInstanceID
Returns the instance id of the object. 返回物體的實例ID
-
ToString
Returns the name of the game object. 返回游戲物體的名稱。
Inherited Class Functions繼承類函數
-
operator bool
Does the object exist? 物體是否存在?
-
Instantiate
Clones the object original and returns the clone. 克隆原始物體,並返回克隆的物體
-
Instantiate.<T>
-
Destroy
Removes a gameobject, component or asset. 刪除一個游戲物體、組件或資源
-
DestroyImmediate
Destroys the object obj immediately. It is strongly recommended to use Destroy instead. 立即銷毀物體obj,強烈建議使用Destroy代替。
-
FindObjectsOfType
Returns a list of all active loaded objects of Type type. 返回Type類型的所有激活的加載的物體列表
-
FindObjectOfType
Returns the first active loaded object of Type type. 返回Type類型第一個激活的加載的物體。
-
operator ==
Compares if two objects refer to the same 比較如果兩個物體相同
-
operator !=
Compares if two objects refer to a different object 比較如果兩個物體不同
-
DontDestroyOnLoad
Makes the object target not be destroyed automatically when loading a new scene. 加載新場景的時候使目標物體不被自動銷毀。
int speed=50;
是步長
if(Input.GetKeyDown(KeyCode.Mouse0))
按下鼠標左鍵
Transform n = (Transform)Instantiate(Q,transform.position,transform.rotation);
創建一個新炮彈
Instantiate 是object類型 需要強制轉換
Vector3 f = transform.TransformDirection(Vector3.forward);
三維坐標 Vector3.forward 0 0 1 向前
n.rigidbody.AddForce(f*2000);
給跑斷加力
最後
點擊 攝像頭 把炮彈拖向 定義的public的變量
520 520小說 小說520 小說520 5200 小說5200 5200小說 5200小說網
www.520books.com
http://www.cnblogs.com/goodchenqing/
http://blog.sina.com.cn/goodchenqing
http://goodchenqing.bokee.com/
http://blog.csdn.net/abc288abcd/article/category/2786567