上海杨浦大连路地铁站单机版电子沙盘
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

102 lines
2.6 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//Author:ZCG
//CreatTime:12/1/2017
/// <summary>
///物体伸缩(梯子,举臂等)
/// </summary>
public class ObjStretch: MonoBehaviour
{
private void Start()
{
Init();
initY = objUp[0].localPosition.y;//初始化第一节高度
}
private void Update()
{
InputControl();
}
/// <summary>
/// 初始化
/// </summary>
protected virtual void Init()
{
}
/// <summary>
/// 输入控制
/// </summary>
protected virtual void InputControl()
{
if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W))
Stretch();
if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.S))
Shorten();
}
/// <summary>
/// 根据物体名字关键字获取可伸展的所有物体
/// </summary>
/// <returns></returns>
protected Transform[] GetObj(string keyword)
{
List<Transform> childs = new List<Transform>();
for (int i = 0; i < transform.childCount; i++)
{
var child = transform.GetChild(i);
if (child.name.Contains(keyword))
{
childs.Add(child);
}
}
return childs.ToArray();
}
[Tooltip("可以伸展的所有物体(不包括固定的那一节)")]
/// <summary>
/// 可以伸展的所有梯子(不包括固定的那一节)
/// </summary>
public Transform[] objUp;
private Vector3 speedVec = new Vector3(0, 1, 0);
[Tooltip("每一节米数")]
public float nodeLength;
[Tooltip("速度")]
public float speed;
private float initY;
//第几节索引
private int index = 0;
/// <summary>
/// 延伸
/// </summary>
protected virtual void Stretch()
{
//根据高度计算操作的第几节
if (objUp[index].localPosition.y < (index+1) * nodeLength)
{
for (int i = index; i < objUp.Length; i++)//所有节一起移动
objUp[i].localPosition += speed * Time.deltaTime * speedVec;
}
else if(index < objUp.Length - 1)
{
index++;
Stretch();
}
}
/// <summary>
/// 收回
/// </summary>
protected virtual void Shorten()
{
//当前节是否已经伸展
if (objUp[index].localPosition.y > initY)
{
for (int i = index; i < objUp.Length; i++)//移动每一节阶梯
objUp[i].localPosition -= speed * Time.deltaTime * speedVec;
}
//如果这一节移动完开始移动下一节
if (index > 0 && objUp[index].localPosition.y <= objUp[index - 1].localPosition.y)
{
index--;
Shorten();
}
}
}