建立一個炮彈
一個球體
雙擊腳本
進入編輯器
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;
}
}
}
參見:Physics 類.
Variables變量
-
The position of the transform in world space. 在世界空間坐標transform的位置。
-
Position of the transform relative to the parent transform. 相對於父級的變換的位置。
-
The rotation as Euler angles in degrees. 旋轉作為歐拉角度。
-
The rotation as Euler angles in degrees relative to the parent transform's rotation. 旋轉作為歐拉角度,相對於父級的變換旋轉角度。
-
The red axis of the transform in world space. 在世界空間坐標變換的紅色軸。也就是x軸。
-
The green axis of the transform in world space. 在世界空間坐標變換的綠色軸。也就是y軸。
-
The blue axis of the transform in world space. 在世界空間坐標變換的藍色軸。也就是z軸。
-
The rotation of the transform in world space stored as a Quaternion. 在世界空間坐標物體變換的旋轉角度作為 Quaternion儲存。
-
The rotation of the transform relative to the parent transform's rotation. 物體變換的旋轉角度相對於父級的物體變換的旋轉角度。
-
The scale of the transform relative to the parent. 相對於父級物體變換的縮放。
-
The parent of the transform. 物體變換的父級。
-
Matrix that transforms a point from world space into local space (Read Only). 矩陣變換的點從世界坐標轉為自身坐標(只讀)。
-
Matrix that transforms a point from local space into world space (Read Only). 矩陣變換的點從自身坐標轉為世界坐標(只讀)。
-
Returns the topmost transform in the hierarchy. 返回層次最高的變換。
-
The number of children the Transform has. 變換的子物體數量。
-
The global scale of the object (Read Only). 物體的全局縮放(只讀)。
Functions函數
-
Moves the transform in the direction and distance of translation. 移動transform在translation的方向和距離。
-
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軸(這樣的順序)。
-
Rotates the transform about axis passing through point in world coordinates by angle degrees. 按照angle度通過在世界坐標的point軸旋轉物體。
-
Rotates the transform so the forward vector points at /target/'s current position. 旋轉物體,這樣向前向量指向 target的當前位置。
-
Transforms direction from local space to world space. 從自身坐標到世界坐標變換方向。
-
Transforms a direction from world space to local space. The opposite of Transform.TransformDirection. 變換方向從世界坐標到自身坐標。和 Transform.TransformDirection相反。
-
Transforms position from local space to world space. 變換位置從自身坐標到世界坐標。
-
Transforms position from world space to local space. The opposite of Transform.TransformPoint. 變換位置從世界坐標到自身坐標。和 Transform.TransformPoint相反。
-
Unparents all children. 所有子物體解除父子關系。
-
Finds a child by name and returns it. 通過名字查找子物體並返回它。
-
Is this transform a child of parent? 這個變換是父級的子物體?
Inherited members繼承成員
Inherited Variables繼承變量
-
The Transform attached to this GameObject (null if there is none attached). Transform附加到 GameObject(游戲物體)(如無附加則為空)。
-
The Rigidbody attached to this GameObject (null if there is none attached). Rigidbody附加到 GameObject(游戲物體)(如無附加則為空)。
-
The Camera attached to this GameObject (null if there is none attached). Camera附加到 GameObject(游戲物體)(如無附加則為空)。
-
The Light attached to this GameObject (null if there is none attached). Light附加到 GameObject(游戲物體)(如無附加則為空)。
-
The Animation attached to this GameObject (null if there is none attached). Animation附加到 GameObject(游戲物體)(如無附加則為空)。
-
The ConstantForce attached to this GameObject (null if there is none attached). ConstantForce附加到 GameObject(游戲物體)(如無附加則為空)。
-
The Renderer attached to this GameObject (null if there is none attached). Renderer附加到 GameObject(游戲物體)(如無附加則為空)。
-
The AudioSource attached to this GameObject (null if there is none attached). AudioSource附加到 GameObject(游戲物體)(如無附加則為空)。
-
The GUIText attached to this GameObject (null if there is none attached). GUIText附加到 GameObject(游戲物體)(如無附加則為空)。
-
The NetworkView attached to this GameObject (Read Only). (null if there is none attached) NetworkView附加到 GameObject(游戲物體)(只讀)(如無附加則為空)。
-
The GUITexture attached to this GameObject (Read Only). (null if there is none attached) GUITexture附加到 GameObject(游戲物體)(只讀)(如無附加則為空)。
-
The Collider attached to this GameObject (null if there is none attached). Collider附加到 GameObject(游戲物體)(如無附加則為空)。
-
The HingeJoint attached to this GameObject (null if there is none attached). HingeJoint附加到 GameObject(游戲物體)(如無附加則為空)。
-
The ParticleEmitter attached to this GameObject (null if there is none attached). ParticleEmitter附加到 GameObject(游戲物體)(如無附加則為空)。
-
The game object this component is attached to. A component is always attached to a game object. 組件附加的游戲物體。一個組件總是被附加到一個游戲物體。
-
The tag of this game object. 游戲物體的標簽。
-
The name of the object. //物體的名字
-
Should the object be hidden, saved with the scene or modifiable by the user? 物體是否被隱藏、保存在場景中或被用戶修改?
Inherited Functions繼承函數
-
Returns the component of Type type if the game object has one attached, null if it doesn't. 如果游戲物體有一個附加,則返回Type類型的組件,如果沒有則為null。
-
-
Returns the component with name type if the game object has one attached, null if it doesn't. 如果游戲物體有一個附加,則返回名字類型組件,如果沒有則為null。
-
Returns the component of Type type in the GameObject or any of its children using depth first search. 返回Type類型組件,在 GameObject或它的任何子物體使用深度優先搜索,僅返回激活的組件。
-
-
Returns all components of Type type in the GameObject or any of its children. 在 GameObject或任何它的子物體,返回全部Type類型組件
-
-
Returns all components of Type type in the GameObject. 在游戲物體返回全部Type類型組件。
-
-
Is this game object tagged tag? 游戲物體有被標記標簽么?
-
Calls the method named methodName on every MonoBehaviour in this game object and on every ancestor of the behaviour 在游戲物體每一個 MonoBehaviour和每一個behaviour的祖先上調用名為methodName的方法。
-
Calls the method named methodName on every MonoBehaviour in this game object. 在游戲物體每一個 MonoBehaviour上調用名為methodName的方法。
-
Calls the method named methodName on every MonoBehaviour in this game object or any of its children. 在游戲物體每一個 MonoBehaviour和它的全部子物體上調用名為methodName的方法。
-
Returns the instance id of the object. 返回物體的實例ID
-
Returns the name of the game object. 返回游戲物體的名稱。
Inherited Class Functions繼承類函數
-
Does the object exist? 物體是否存在?
-
Clones the object original and returns the clone. 克隆原始物體,並返回克隆的物體
-
-
Removes a gameobject, component or asset. 刪除一個游戲物體、組件或資源
-
Destroys the object obj immediately. It is strongly recommended to use Destroy instead. 立即銷毀物體obj,強烈建議使用Destroy代替。
-
Returns a list of all active loaded objects of Type type. 返回Type類型的所有激活的加載的物體列表
-
Returns the first active loaded object of Type type. 返回Type類型第一個激活的加載的物體。
-
Compares if two objects refer to the same 比較如果兩個物體相同
-
Compares if two objects refer to a different object 比較如果兩個物體不同
-
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