1、實(shí)現(xiàn)吊機(jī)吊物體的功能
個(gè)人見解:
//掛在吊機(jī)的腳本
public class diaoji_ctrl : MonoBehaviour
{
public float move_speed;
public float gouzi_speed;
GameObject shengZi;
GameObject gouZi;
void Start()
{
shengZi = GameObject.Find("shengzi");
gouZi = GameObject.Find("gouzi");
}
void Update()
{
float h = Input.GetAxis("Horizontal") * move_speed * Time.deltaTime;
float v = Input.GetAxis("Vertical") * move_speed * Time.deltaTime;
transform.Translate(h, 0, v); //吊機(jī)的移動(dòng)
//限制上拉與下放的極限距離
float dis = Vector3.Distance(gameObject.transform.position, gouZi.transform.position);
if (Input.GetKey(KeyCode.J) && dis < 2.85f)
{
GouziMove(gouzi_speed);
}
if (Input.GetKey(KeyCode.K) && dis > 0.8f)
{
GouziMove(-gouzi_speed);
}
}
void GouziMove(float speed)
{
shengZi.transform.localScale = shengZi.transform.localScale + new Vector3(0, speed, 0) * Time.deltaTime;
shengZi.transform.Translate(0, -speed * Time.deltaTime, 0);
gouZi.transform.Translate(0, -2 * speed * Time.deltaTime, 0);
}
}
//掛在吊鉤的腳本
public class gouzi_ctrl : MonoBehaviour {
private void Update()
{
if (Input.GetKeyDown(KeyCode.L))
{
transform.GetChild(0).gameObject.GetComponent<Rigidbody>().isKinematic = false;//恢復(fù)
transform.DetachChildren(); //解除自身所有子物體
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "goods")
{
collision.gameObject.GetComponent<Rigidbody>().isKinematic = true;// 讓物體僅受代碼控制
collision.gameObject.transform.SetParent(gameObject.transform);
}
}
}
2谁尸、寫一個(gè)計(jì)時(shí)器工具舅踪,從整點(diǎn)開始計(jì)時(shí),格式為:00:00:00
//掛在隨便一個(gè)text物體上
//空格暫停與開始良蛮;小鍵盤0清零
public class jishiqi : MonoBehaviour {
private float timer = 0f;
private int h = 0;
private int m = 0;
private int s = 0;
private string timeStr = string.Empty;
Text time_text;
bool pause = true;
private void Start()
{
time_text = gameObject.GetComponent<Text>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
pause = !pause;
}
timer += Time.deltaTime;
if (!pause)
{
if (timer >= 1f)
{
s++;
timer = 0;
}
if (s >= 60)
{
m++;
s = 0;
}
if (m >= 60)
{
h++;
m = 0;
}
if (h >= 99)
{
h = 0;
}
}
if (Input.GetKeyDown(KeyCode.Keypad0))
{
s = 0; m = 0; h = 0;
}
timeStr = string.Format("{0:D2}:{1:D2}:{2:D2}", h, m, s);
time_text.text = timeStr;
}
}
3抽碌、用鼠標(biāo)實(shí)現(xiàn)在場景中拖動(dòng)物體,用鼠標(biāo)滾輪實(shí)現(xiàn)縮放
//鼠標(biāo)拖拽决瞳,注:被拖拽物體不能與場景內(nèi)其他物體在default layer上
public class dragobj : MonoBehaviour
{
private void OnMouseEnter()
{
gameObject.transform.localScale += new Vector3(0.1f, 0.1f, 0.1f);
}
private void OnMouseExit()
{
gameObject.transform.localScale -= new Vector3(0.1f, 0.1f, 0.1f);
}
private void OnMouseDrag()
{
Ray camRay = Camera.main.ScreenPointToRay(Input.mousePosition);//從屏幕鼠標(biāo)點(diǎn)擊的位置向場景內(nèi)發(fā)出一條射線
RaycastHit floorHit;
if (Physics.Raycast(camRay, out floorHit, 1000f, 1))//1為過濾層
{
transform.position = new Vector3(floorHit.point.x, floorHit.point.y + 0.25f, floorHit.point.z);
}
}
}
//滾輪縮放
public class view_ctrl : MonoBehaviour
{
public float wheelspeed;
void Update()
{
transform.Translate(0, 0, Input.GetAxis("Mouse ScrollWheel") * wheelspeed * Time.deltaTime, Space.Self);
}
}
4货徙、鼠標(biāo)左鍵游戲?qū)ο笫蛊湫D(zhuǎn)
public class RollObj : MonoBehaviour
{
public float roll_speed;
void Update()
{
if (Input.GetMouseButton(0))
{
gameObject.transform.eulerAngles += new Vector3(0, -Input.GetAxis("Mouse X") * roll_speed * Time.deltaTime, 0);
}
}
}