Unity3D Speed Down

Speed Down

1.AnimationBG

using UnityEngine;

public class AnimationBG : MonoBehaviour
{
    Material material;
    Vector2 movement;
    public Vector2 speed;

    void Start()
    {
        material = GetComponent<Renderer>().material;
    }

    void Update()
    {
        movement += speed * Time.deltaTime;
        material.mainTextureOffset = movement;
    }
}

2.FanPlatform

using UnityEngine;

public class FanPlatform : MonoBehaviour
{
    Animator animator;
    
    void Start()
    {
        animator = GetComponent<Animator>();
    }

    void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            animator.Play("Fan_run");
        }
    }
}

3.GameManager

using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class GameManager : MonoBehaviour
{
    static GameManager instance;
    public Text timeScore;

    public GameObject gameOverUI;
    
    void Awake()
    {
        if (instance != null)
        {
            Destroy(this);
        }

        instance = this;
        instance.gameOverUI.SetActive(false);
    }
    
    void Update()
    {
        //timeScore.text = Time.time.ToString("00");
        //每一个场景加载的时候,重新计算时间
        timeScore.text = Time.timeSinceLevelLoad.ToString("00");
    }

    public void RestartGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        Time.timeScale = 1f;
    }

    public void Quit()
    {
        Application.Quit();
    }

    public static void GameOver(bool dead)
    {
        if (dead)
        {
            instance.gameOverUI.SetActive(true);
            Time.timeScale = 0f;
        }
    }
}

4.LineRender

using UnityEngine;

public class LineRender : MonoBehaviour
{
    LineRenderer line;

    public Transform startPoint;

    public Transform endPoint;
    
    void Start()
    {
        line = GetComponent<LineRenderer>();
    }
    
    void Update()
    {
        line.SetPosition(0, startPoint.position);
        line.SetPosition(1, endPoint.position);
    }
}

5.Platform

using UnityEngine;

public class Platform : MonoBehaviour
{
    GameObject topLine;
    Vector3 movement;

    public float speed;
    
    void Start()
    {
        movement.y = speed;
        topLine = GameObject.Find("TopLine");
    }
    
    void Update()
    {
        MovePlatform();
    }

    void MovePlatform()
    {
        transform.position += movement * Time.deltaTime;
        if (transform.position.y >= topLine.transform.position.y)
        {
            Destroy(gameObject);
        }
    }
}

6.PlayerController

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    Rigidbody2D rb;
    Animator anim;
    public float speed;


    public bool isOnGround;
    public float checkRadius;
    public LayerMask platform;
    public GameObject groundCheck;

    float xVelocity;
    
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        anim = GetComponent<Animator>();
    }
    
    void Update()
    {
        isOnGround = Physics2D.OverlapCircle(groundCheck.transform.position, checkRadius, platform);
        //掉落的动画
        anim.SetBool("isOnGround", isOnGround);
        Movement();
    }


    void Movement()
    {
        //没有触摸  
        if (Input.touchCount <= 0)
        {
            return;
        }

        //单点触摸, 水平上下移动
        if (Input.touchCount == 1 && Input.GetTouch(0).phase == TouchPhase.Moved)
        {
            //EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)为true表示点到了UI
            // if (!UnityEngine.EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId))
            // {
            xVelocity = Input.GetTouch(0).deltaPosition.x;
/*                if (deltaposition > 0)
                {
                    xVelocity = 1;
                }
                else if (deltaposition < 0)
                {
                    xVelocity = -1;
                }
                else {
                    xVelocity = 0;
                }*/
            // transform.Translate(-deltaposition.x * 0.1f, 0f, -deltaposition.y * 0.1f);
            // }
        }


        //返回-1,0,1
        //xVelocity = Input.GetAxisRaw("Horizontal");
        rb.velocity = new Vector2(xVelocity * speed, rb.velocity.y);

        //跑到的动画
        anim.SetFloat("speed", Mathf.Abs(rb.velocity.x));


        if (xVelocity != 0)
        {
            if (xVelocity > 0)
            {
                xVelocity = 1;
            }
            else if (xVelocity < 0)
            {
                xVelocity = -1;
            }
            else
            {
                xVelocity = 0;
            }

            transform.localScale = new Vector3(xVelocity, 1, 1);
        }
    }

    bool playerDead;

    void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.CompareTag("Spike"))
        {
            anim.SetTrigger("dead");
        }
    }

    void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("Fan"))
        {
            rb.velocity = new Vector2(rb.velocity.x, 6f);
        }
    }


    public void PlayerDead()
    {
        playerDead = true;
        Destroy(gameObject);
        GameManager.GameOver(playerDead);
    }

    private void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.blue;
        Gizmos.DrawWireSphere(groundCheck.transform.position, checkRadius);
    }
}

7.Spawner

using System.Collections.Generic;
using UnityEngine;

public class Spawner : MonoBehaviour
{
    public List<GameObject> platforms = new List<GameObject>();

    public float spawnTime;
    private float countTime;

    private Vector3 spawnPosition;
    
    void Update()
    {
        SpawnPlatform();
    }

    public void SpawnPlatform()
    {
        countTime += Time.deltaTime;
        spawnPosition = transform.position;
        spawnPosition.x = Random.Range(-3.5f, 3.5f);
        if (countTime >= spawnTime)
        {
            CreatePlatform();
            countTime = 0;
        }
    }

    public void CreatePlatform()
    {
        int index = Random.Range(0, platforms.Count);
        int spikeNum = 0;
        if (index == 4)
        {
            spikeNum++;
        }

        if (spikeNum > 1)
        {
            spikeNum = 0;
            countTime = spawnTime;
            return;
        }

        GameObject newPlatform = Instantiate(platforms[index], spawnPosition, Quaternion.identity);
        newPlatform.transform.SetParent(this.gameObject.transform);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值