Unity
Speed Down小游戏
添加背景,使背景运动,添加尖刺
背景图X:Y比例为9:16
背景动画设计代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimationBackGround : MonoBehaviour
{
Material material;
public Vector2 movement;
public Vector2 speed;
void Start()
{
material= GetComponent<Renderer>().material;
}
// Update is called once per frame
void Update()
{
//deltaTime一秒钟运行50次左右
movement += speed * Time.deltaTime;
material.mainTextureOffset = movement;
}
}由此可以得知,通过脚本可以获得所有组件及其操作
泛型:一个方法,适用于所有类型,且类型安全
T GetComponent<T>() where T : Component
{
return (T)foundComponent;
}
//泛型的使用
Renderer r = MyMethod<Renderer>(); // T 被替换为 Renderer
AudioSource a = MyMethod<AudioSource>(); // T 被替换为 AudioSource由此可知,若要得到Materials,需要先得到Renderer

mainTextureOffset:
mainTextureOffset这是材质的主纹理(Main Texture/Albedo)的UV偏移量
每一帧都让背景偏移一段,用以制造循环效果
代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
static GameManager instance;
public Text timeScore;
public GameObject gameoverUI;
private void Awake()
{
if (instance != null)
{
Destroy(gameObject);
}
instance = this;
}
void Update()
{
timeScore.text=Time.timeSinceLevelLoad.ToString("00");
}
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
Time.timeScale = 1;
}
public void Quit()
{
Application.Quit();
}
public static void GameOver(bool dead)
{
if (dead)
{
instance.gameoverUI.SetActive(true);
Time.timeScale = 0f;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StartLine : MonoBehaviour
{
public List<GameObject> platforms = new List<GameObject>();
public float spwanTime;
private float countTime;
private Vector3 spwanPosition;
int spikeNum = 0;
//随机生成函数
void Update()
{
SpawnPlatform();
}
public void SpawnPlatform()
{
countTime += Time.deltaTime;
spwanPosition = transform.position;
spwanPosition.x = Random.Range(-3.5f, 3.5f);
if (countTime >= spwanTime)
{
CreatePlatform();
countTime = 0;
}
}
public void CreatePlatform()
{
//platforms[];
int index = Random.Range(0, platforms.Count);
if (index == 5)
{
spikeNum++;
}
if (spikeNum > 1)
{
spikeNum = 0;
countTime = spwanTime;
return;
}
GameObject newPlatform=Instantiate(platforms[index], spwanPosition, Quaternion.identity);
newPlatform.transform.SetParent(this.gameObject.transform);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LineRender : MonoBehaviour
{
LineRenderer line;
public Transform startPoint;
public Transform endPoint;
void Start()
{
line = GetComponent<LineRenderer>();
}
// Update is called once per frame
void Update()
{
line.SetPosition(0, startPoint.position);
line.SetPosition(1, endPoint.position);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions.Must;
public class PlayerController : MonoBehaviour
{
Rigidbody2D rb;
Animator animator;
public float speed;
float xVlocity;
float yVlocity;
public bool isOnGround;
public float checkRadius;
public LayerMask platform;
public GameObject groundCheck;
bool playerdead;
void Start()//通常用于获取组件
{
rb = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
isOnGround = Physics2D.OverlapCircle(groundCheck.transform.position, checkRadius, platform);
animator.SetBool("isonground",isOnGround);
Movement();
}
void Movement()
{
xVlocity = Input.GetAxisRaw("Horizontal");//水平横轴移动
rb.velocity = new Vector2(xVlocity*speed,rb.velocity.y);
animator.SetFloat("speed", Mathf.Abs(rb.velocity.x));//跑动动画
if (xVlocity != 0)
{
transform.localScale = new Vector3(xVlocity,1,1);
}
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Fan"))
{
rb.velocity = new Vector2(rb.velocity.x, 10f);
}
else if (other.gameObject.CompareTag("Jump"))
{
rb.velocity = new Vector2(rb.velocity.x, 10f);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Spike"))
{
animator.SetTrigger("Hit");
}
}
public void PlayerDead()
{
playerdead = true;
GameManager.GameOver(playerdead);
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(groundCheck.transform.position,checkRadius);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class JumpPlatform : MonoBehaviour
{
Animator Animator;
void Start()
{
Animator=GetComponent<Animator>();
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Player"))
{
Animator.Play("JumpOn");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FanPlatform : MonoBehaviour
{
Animator Animator;
void Start()
{
Animator=GetComponent<Animator>();
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Player"))
{
Animator.Play("Fan_on");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Platform : MonoBehaviour
{
Vector3 movement;
public float speed;
GameObject topLine;
// Start is called before the first frame update
void Start()
{
movement.y = speed;
topLine = GameObject.Find("Topline");
}
// Update is called once per frame
void Update()
{
MovePlatform();
}
void MovePlatform()
{
transform.position += movement * Time.deltaTime;
if(transform.position.y >= topLine.transform.position.y)
{
Destroy(gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FanPlatform : MonoBehaviour
{
Animator Animator;
void Start()
{
Animator=GetComponent<Animator>();
}
private void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.CompareTag("Player"))
{
Animator.Play("Fan_on");
}
}
}