一堆小游戏制作的总结

1.鼠标点击移动

    public NavMeshAgent agent;
    public Animator anim;
    
    // Update is called once per frame
    void Update () {
        if (Input.GetMouseButtonDown(0)) {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if(Physics.Raycast(ray, out hit)) {
                //hit.point
                //print(hit.point);
                agent.SetDestination(hit.point);
            }
        }
        anim.SetFloat("speed", agent.velocity.magnitude);
    }

2.

UI0.01说过了 100像素相当于1米

3.   transform.Rotate(new Vector3(0, 0, -speed * Time.deltaTime));//试试?

4.

    IEnumerator GameOverAnimation()
    {
        while (true)
        {
            mainCamera.backgroundColor = Color.Lerp(mainCamera.backgroundColor, Color.red, speed * Time.deltaTime);
            mainCamera.orthographicSize = Mathf.Lerp(mainCamera.orthographicSize, 4, speed * Time.deltaTime);
            if( Mathf.Abs( mainCamera.orthographicSize-4 )<0.01f)
            {
                break;
            }
            yield return 0;
        }
        yield return new WaitForSeconds(0.2f);
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }

5.

        if ( Input.GetMouseButtonDown(0) )
        {
            GameObject b = GameObject.Instantiate(bullet, transform.position, transform.rotation);
            Rigidbody rgd = b.GetComponent<Rigidbody>();
            rgd.velocity = transform.forward * speed;
        }

6.

    void Update () {
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        //Debug.Log(h);
        transform.Translate(new Vector3(h, v, 0) * Time.deltaTime * speed );
    }

7.

    public float speed =1;
    public float mouseSpeed =60;
    // Update is called once per frame
    void Update () {
        float h = Input.GetAxis("Horizontal");
        float v =Input.GetAxis("Vertical");
        float mouse = Input.GetAxis("Mouse ScrollWheel");
        transform.Translate(new Vector3(h,mouse*mouseSpeed,v)*Time.deltaTime*speed,Space.World);
    }

8.显示在面板

[System.Serializable]
public class Wave  {
    public GameObject enemyPrefab;
    public int count;
    public float rate;
}

9.暂停 yield return 0帧;

10.GD_Geek了解一下

11.2d赛车运动

    public Rigidbody2D m_Rigidbody;
    public WheelJoint2D m_LeftWheel;
    public WheelJoint2D m_RightWheel;

    public float m_Speed;

    JointMotor2D jmL, jmR;

    // Use this for initialization
    void Start () {
        jmL = m_LeftWheel.motor;
        jmR = m_RightWheel.motor;
    }
    
    // Update is called once per frame
    void Update () {
        Move();
    }
  
    void Move()
    {
        float ax = Input.GetAxis("Horizontal");
        float ay = Input.GetAxis("Vertical");
      
        jmL.motorSpeed = ax * m_Speed;
        jmR.motorSpeed = ax * m_Speed;
        Debug.LogError(jmL.motorSpeed);
        m_LeftWheel.motor = jmL;
        m_RightWheel.motor = jmR;
    }

缓动分

//是变化分数,缓动增加分数
    IEnumerator AddScoreIE(float p_tonum)
    {
        while (m_CurrentScore< p_tonum)
        {
            //插值运算
            m_CurrentScore = Mathf.Lerp(m_CurrentScore, p_tonum, m_changeSpeed * Time.deltaTime);

            m_Score_Text.text = ((int)m_CurrentScore).ToString();

            yield return 0;
        }
        
    }

真运动

using UnityEngine;
using System.Collections;

public class CarControll2 : MonoBehaviour {

    public float m_phSpeed = 5.0f;

    //速度
    public float Speed = 5f;
    //两个轮子
    public WheelJoint2D m_LeftWhell;
    public WheelJoint2D m_RightWhell;

    public AudioClip IdelClip;
    public AudioClip RunClip;

    public AudioSource m_AudioSource;

    //JointMotor2D,给轮子加动力
    JointMotor2D jmL, jmR;

    private Transform m_Transform;

    void Awake()
    {
        m_Transform = this.transform;
    }

    // Use this for initialization
    void Start () {
        //获取jmL, jmR;
        jmL = m_LeftWhell.motor;
        jmR = m_RightWhell.motor;

    }
    
    // Update is called once per frame
    void Update () {

        Move();


    }

    void Move()
    {
        //获取输入
        float ax = Input.GetAxis("Horizontal");
        float ay = Input.GetAxis("Vertical");

        //输入速度
        jmL.motorSpeed = ax * Speed;
        jmR.motorSpeed = ax * Speed;
        //赋值给轮子
        m_LeftWhell.motor = jmL;
        m_RightWhell.motor = jmR;
        if (ax!=0)
        {
            // AudioSource.PlayClipAtPoint(RunClip,m_Transform.position);
            m_AudioSource.clip = RunClip;
            if (!m_AudioSource.isPlaying)
            {
                m_AudioSource.Play();
            }
          
        }
        else
        {
            // AudioSource.PlayClipAtPoint(IdelClip, m_Transform.position);
            //m_AudioSource.clip = IdelClip;
            //if (!m_AudioSource.isPlaying)
            //{
            //    m_AudioSource.Play();
            //}

            m_AudioSource.Stop();

        }
        if (ay!=0)
        {
            //旋转赛车,
            m_Transform.Rotate(m_Transform.forward, ay * m_phSpeed);

        }

    }
}

猜你喜欢

转载自www.cnblogs.com/xiaomao21/p/9193741.html