• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
白染
博客园    首页    新随笔    联系   管理    订阅  订阅

小球跳跃游戏

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;
        }
    }

}

 

莫说我穷的叮当响,大袖揽清风。 莫讥我困时无处眠,天地做床被。 莫笑我渴时无美酒,江湖来做壶。
posted @ 2018-09-08 15:03  白染  阅读(313)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3