1、實現吊機吊物體的功能
效果圖
上面做了個橫樑,上面一塊Cube作爲鉤子,下面的Cube作爲要吊起的重物,中間的繩子用Capsule
思路:鼠標左右滑動實現鉤子的左右滑動,鬆開鼠標---鉤子下滑,當鉤子等撞到重物的時候停止下降 並帶着重物一同上升,回到一定高度後,開始水平回到初始位置,並判斷(是否勾住重物)如果勾住重物了 ,在原點位置,下降 然後放下重物,如果沒勾住重物 則再遠點不動,等待第二次操作。
在每次操作的過程中,不能進行第二次操作
結構:
上代碼:
QizhongjiCS.cs控制鉤子的滑動、鉤子的下降、上升
using UnityEngine;
using System.Collections;
public class QizhongjiCS : MonoBehaviour {
private float dianX = 0;
public GameObject hook;
private float yuandianX;
private Vector3 hookV;
private int flag=0;
private delegate void HookMove();
private HookMove _hookMove=null;
private int speed=3;
public HookZhongWu hzw;
private int zwFlag=0;
private GameObject zhongwu; //抓住的重物;
public GameObject shengzi;
public GameObject heng;
// Use this for initialization
void Start () {
hookV = hook.transform.localPosition;
yuandianX = hookV.x;
}
// Update is called once per frame
void Update () {
if (flag == 0) {
if (Input.GetMouseButtonDown(0))
{
dianX = Input.mousePosition.x;
}
else if (Input.GetMouseButton(0))
{
float dx = Input.mousePosition.x - dianX;
if (Mathf.Abs(dx) > 0.1f)
{
hookV.x = yuandianX + dx / 32;
if (hookV.x > -6 && hookV.x < 6)
{
hook.transform.localPosition = hookV;
}
}
}
else if (Input.GetMouseButtonUp(0))
{
//yuandianX = hookV.x;
flag = 1;
_hookMove = hookDown;
StartCoroutine(downZhua());
}
}
}
IEnumerator downZhua() {
yield return new WaitForSeconds(0.01f);
//1、向下移動, +speed
//2、判斷移動的位置 如果大於某個位置 ,返回 speed爲負
if (_hookMove != null)
{
_hookMove();
yield return StartCoroutine(downZhua());
}
else {
yield return null;
}
//3、判斷移動回到原點 整個鉤子向原始位置水平運動
//4、判斷鉤子回到原點 停止協程 flag=0
yield return StartCoroutine(downZhua());
}
void hookDown() {
hook.transform.Translate(Vector3.down * Time.deltaTime * speed);
changeShengZi();
if (hook.transform.localPosition.y < -2) {
if (zwFlag == 1) {
zhongwu.transform.parent = null;
zhongwu = null;
zwFlag = 0;
}
_hookMove = hookUp;
}
}
void hookUp()
{
hook.transform.Translate(Vector3.up * Time.deltaTime * speed);
changeShengZi();
if (hook.transform.localPosition.y >3.2)
{
_hookMove = HMove;
}
}
void HMove()
{
hook.transform.Translate(Vector3.left * Time.deltaTime * speed);
if (hook.transform.localPosition.x <=-4.5)
{
if (zwFlag == 0)
{
flag = 0;
_hookMove = null;
}
else {
_hookMove = hookDown;
}
}
}
public void zhuaZhongWu(GameObject zhongwu) {
_hookMove = hookUp;
zwFlag = 1;
this.zhongwu = zhongwu;
}
void changeShengZi() {
Vector3 hookPosition = hook.transform.position;
Vector3 hengliangP = heng.transform.position;
float dy = hookPosition.y - hengliangP.y;
Vector3 shengziP = shengzi.transform.position;
shengziP.y = hengliangP.y + dy / 2;
shengzi.transform.position = shengziP;
//改變 繩子長度
Vector3 shengziSclae = shengzi.transform.localScale;
shengziSclae.y = dy/2;
shengzi.transform.localScale = shengziSclae;
}
}
----------------------------------------------
HookZhongWu.cs用來判斷 鉤子是否和重物碰撞
碰上的時候 將重物設置成鉤子的子對象 就可以實現帶着往上升的效果了
sing UnityEngine;
using System.Collections;
public class HookZhongWu : MonoBehaviour {
public QizhongjiCS qzj;
void OnTriggerEnter(Collider collision)
{//當碰撞時
print("OnTriggertEnter+" + collision.gameObject.name);
if (collision.gameObject.name == "zhongwu") {
collision.gameObject.transform.parent = this.gameObject.transform;
Vector3 v = collision.gameObject.transform.localPosition;
v.y = -1.2f;
collision.gameObject.transform.localPosition = v;
qzj.zhuaZhongWu(collision.gameObject);
}
}
}
2、寫一個計時器工具,從整點開始計時,格式爲:00:00:00
創建工程後添加一個Cube物體,爲其添加一個腳本。
using UnityEngine;
using System.Collections;
public class Cube : MonoBehaviour {
private float timer = 0f;
private int h = 0;
private int m = 0;
private int s = 0;
private string timeStr = string.Empty;
// Update is called once per frame
void Update () {
timer += Time.deltaTime;
if (timer >= 1f) {
s++;
timer = 0;
}
if (s >= 60) {
m++;
s = 0;
}
if (m >= 60) {
h++;
m = 0;
}
if (h >= 99) {
h = 0;
}
}
void OnGUI(){
timeStr = string.Format ("{0:D2}:{1:D2}:{2:D2}", h, m, s);
GUI.Label (new Rect (10, 10, 100, 200), timeStr);
}
}
3、用鼠標實現在場景中拖動物體,用鼠標滾輪實現縮放
在場景中添加一個Plan,Camera,Directional Light,Cube。添加兩個腳本scrollerScirpt(掛在Camera),CubeDragScript(掛在Cube上)。
1.鼠標滾輪實現縮放:
將攝像機的鏡頭拉近或者拉遠,調整攝像機的視角就可以實現,主要實現代碼如下:
void Update () {
//鼠標滾輪的效果
//Camera.main.fieldOfView 攝像機的視野
//Camera.main.orthographicSize 攝像機的正交投影
//Zoom out
if (Input.GetAxis("Mouse ScrollWheel") < 0)
{
if (Camera.main.fieldOfView <= 100)
Camera.main.fieldOfView += 2;
if (Camera.main.orthographicSize <= 20)
Camera.main.orthographicSize += 0.5F;
}
//Zoom in
if (Input.GetAxis("Mouse ScrollWheel") > 0)
{
if (Camera.main.fieldOfView > 2)
Camera.main.fieldOfView -= 2;
if (Camera.main.orthographicSize >= 1)
Camera.main.orthographicSize -= 0.5F;
}
}
2.鼠標實現在場景中拖動物體:
解決思路就是將世界座標轉換成屏幕座標,然後計算物體與鼠標之間移動量,循環鼠標被按下操作,得到鼠標的當前位置,加上計算好的移動量,將新的座標賦值給物理就行了。主要是開啓一個協同程序(Corountine)來處理
主要代碼如下:
void Start ()
{
StartCoroutine(OnMouseDown());
}
IEnumerator OnMouseDown()
{
//將物體由世界座標系轉換爲屏幕座標系
Vector3 screenSpace = Camera.main.WorldToScreenPoint(transform.position);//三維物體座標轉屏幕座標
//完成兩個步驟 1.由於鼠標的座標系是2維,需要轉換成3維的世界座標系
// // 2.只有3維座標情況下才能來計算鼠標位置與物理的距離,offset即是距離
//將鼠標屏幕座標轉爲三維座標,再算出物體位置與鼠標之間的距離
Vector3 offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z));
while (Input.GetMouseButton(0))
{
//得到現在鼠標的2維座標系位置
Vector3 curScreenSpace = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenSpace.z);
//將當前鼠標的2維位置轉換成3維位置,再加上鼠標的移動量
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenSpace) + offset;
//curPosition就是物體應該的移動向量賦給transform的position屬性
transform.position = curPosition;
yield return new WaitForFixedUpdate(); //這個很重要,循環執行
}
}
4、鼠標左鍵遊戲對象使其旋轉
UnityEngine;
using System.Collections;
public class DragRotateWithSlider : MonoBehaviour {
private bool onDrag = false; //是否被拖拽
public float speed = 5f; //旋轉速度
private float tempSpeed; //阻尼速度
private float axisX; //鼠標沿水平方向移動的增量
private float axisY; //鼠標沿垂直方向移動的增量
private float cXY; //鼠標移動的距離
//接收鼠標按下的事件
void OnMouseDown ()
{
axisX = 0f; //爲移動的增量賦初值
axisY = 0f;
}
//鼠標拖拽時的操作
void OnMouseDrag()
{
onDrag = true; //被拖拽
axisX = Input.GetAxis("Mouse Y"); //獲得鼠標增量
axisY = -Input.GetAxis("Mouse X");
cXY = Mathf.Sqrt(axisX * axisX + axisY * axisY); //計算鼠標移動的長度
if (cXY == 0f)
{
cXY = 1f;
}
}
//Count TempSpeed
float Rigid() //計算阻尼速度
{
if (onDrag)
{
tempSpeed = speed;
}
else
{
if (tempSpeed > 0)
{
tempSpeed -= speed * 2 * Time.deltaTime / cXY;//通過除以鼠標移動長度實現拖拽越長速度減緩越慢
}
else
{
tempSpeed = 0;
}
}
return tempSpeed; //返回阻尼的值
}
void Update()
{
gameObject.transform.Rotate(new Vector3(axisX, axisY, 0) * Rigid(), Space.World);
if (!Input.GetMouseButton(0))
{
onDrag = false;
}
}
}