using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class SphereControll : MonoBehaviour { bool b = false; public float sphereSpeed=10;//以监视面板赋值为准,如果外界改值调用外界的值 public float sphereJump=20; public Image image; // Use this for initialization void Start () { image = image.GetComponent<Image>(); image.gameObject.SetActive(false); //GetComponent<BoxCollider>().enabled = false;//控制组件或者挂载的脚本的激活失活 } // Update is called once per frame void Update () { if (Input.GetKey(KeyCode.A)|| Input.GetKey(KeyCode.D)) { transform.rotation = Quaternion.identity;//相当于无旋转 } if (b) { if (Input.GetKey(KeyCode.A)) { GetComponent<Rigidbody>().AddForce(-transform.right* sphereSpeed * Time.deltaTime);//移动 } if (Input.GetKey(KeyCode.D)) { GetComponent<Rigidbody>().AddForce(transform.right* sphereSpeed * Time.deltaTime); } if (Input.GetKey(KeyCode.Space)) { GetComponent<Rigidbody>().AddForce(transform.up* sphereJump*Time.deltaTime); } } } private void OnCollisionEnter(Collision collision) { if (collision.gameObject.tag.Equals( "Player")) { b = true; // transform.parent = collision.gameObject.transform; } if (collision.gameObject.name=="Cube") { image.gameObject.SetActive(true); image.CrossFadeAlpha(0,3,true);//ui界面淡入淡出 GetComponent<Rigidbody>().Sleep(); transform.position = new Vector3(-1,-1,0); GetComponent<Rigidbody>().WakeUp(); Invoke("ImageFalse",3);//延迟调用该方法 } } public void ImageFalse() { image.color = Color.red; image.gameObject.SetActive(false); } private void OnCollisionStay(Collision collision) { if (collision.gameObject.tag.Equals("Player")) { b = true; } } private void OnCollisionExit(Collision collision) { if (collision.gameObject.tag.Equals("Player")) { b = false; transform.parent = null; } } }