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;
}
}
}

參見: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軸。
  • up
    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 name of the object. //物體的名字
  • Should the object be hidden, saved with the scene or modifiable by the user? 物體是否被隱藏、保存在場景中或被用戶修改?

Inherited Functions繼承函數

  • 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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM