首页 > 代码库 > Unity3D 学习教程 12 C# 发射炮弹

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 frame14     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),因此你可以使用循环遍历子物体。

  • C#
  • JavaScript
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

 

Unity3D 学习教程 12 C# 发射炮弹