障碍物和背景不同时滚动
Obstacles and Background not scrolling at the same time
我和一个大学的朋友有一个游戏创意,将障碍物集中在一个对象池中,然后以与背景滚动(从右到左)相同的速度在屏幕上滚动(从右到左),从而给出玩家角色的幻觉是 运行。这到目前为止有效。
但是,我们希望让背景和障碍物的速度随着时间的推移逐渐增加,同时彼此保持相同的速度,从而使游戏对用户来说更难。
我们在实现这个方面遇到了很多麻烦。最初我们尝试为两种基本速度添加速度调节器,但这导致速度大相径庭。我相信这与我们使用纹理偏移来移动背景但对于障碍物我们使用速度这一事实有关。如果有帮助,一切(除了背景、相机、EventSystem 和 GameController)都在 canvas 内,因为那是我们当时知道如何缩放的唯一方法。
那么,我们做错了什么?
背景滚动代码:
using UnityEngine;
public class Background : MonoBehaviour
{
/*
*
* A script that's sole purpose is to create a scrolling, infinite background.
* Attached to: Brick
* Scene: Level0
*
*/
public float scrollSpeed;
private Vector2 savedOffset;
private Renderer rndrer;
private float conversionFactor = 0.1f;
public static Background instance;
private void Start()
{
rndrer = GetComponent<Renderer>();
savedOffset = rndrer.material.GetTextureOffset("_MainTex");
}
private void FixedUpdate()
{
this.rndrer.material.SetTextureOffset("_MainTex", new Vector2((GameControl.speed * conversionFactor), 0));
}
private void OnDisable()
{
rndrer.sharedMaterial.SetTextureOffset("_MainTex", savedOffset);
}
}
障碍滚动代码:
using UnityEngine;
public class ObstacleScrolling : MonoBehaviour
{
/*
*
* A script which controls the movement of the Obstacles to make it look like the player is running towards them.
* Attached to: [All Obstacles in the Prefabs folder]
* Scene: Level0
*
*/
private Rigidbody2D rb2d;
public float scrollSpeed;
public float test;
GameObject canvasObj;
Canvas canvas;
// Use this for initialization
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
canvasObj = GameObject.Find("Canvas");
canvas = canvasObj.GetComponent<Canvas>();
test = -1280 * canvas.scaleFactor;
scrollSpeed = scrollSpeed * canvas.scaleFactor; //+ -(Time.timeSinceLevelLoad / 100);
}
// Update is called once per frame
void FixedUpdate()
{
this.transform.Translate(Vector3.right * GameControl.speed);
if (GameControl.instance.gameOver == true)
{
rb2d.velocity = Vector2.zero;
}
}
}
使用这个:
public class GameControl : MonoBehaviour
{
private static float speedFactor = 1.0f; // you can set this to canvas scale
private static float speed = 1.0f;
public static float ScaledSpeed
{
get
{
return speed * speedFactor;
}
}
void FixedUpdate()
{
speed += 0.01f * Time.deltaTime;
}
}
还有这个:
public class Background : MonoBehaviour
{
private Vector2 savedOffset;
private Vector2 offset;
private Renderer rndrer;
private float conversionFactor = 0.1f;
private void Start()
{
rndrer = GetComponent<Renderer>();
savedOffset = rndrer.material.GetTextureOffset("_MainTex");
offset = savedOffset;
}
private void FixedUpdate()
{
offset += new Vector2(GameControl.ScaledSpeed * conversionFactor, 0);
this.rndrer.material.SetTextureOffset("_MainTex", offset);
}
}
还有这个:
public class ObstacleScrolling : MonoBehaviour
{
void FixedUpdate()
{
this.transform.Translate(Vector3.right * GameControl.ScaledSpeed);
}
}
我和一个大学的朋友有一个游戏创意,将障碍物集中在一个对象池中,然后以与背景滚动(从右到左)相同的速度在屏幕上滚动(从右到左),从而给出玩家角色的幻觉是 运行。这到目前为止有效。
但是,我们希望让背景和障碍物的速度随着时间的推移逐渐增加,同时彼此保持相同的速度,从而使游戏对用户来说更难。
我们在实现这个方面遇到了很多麻烦。最初我们尝试为两种基本速度添加速度调节器,但这导致速度大相径庭。我相信这与我们使用纹理偏移来移动背景但对于障碍物我们使用速度这一事实有关。如果有帮助,一切(除了背景、相机、EventSystem 和 GameController)都在 canvas 内,因为那是我们当时知道如何缩放的唯一方法。
那么,我们做错了什么?
背景滚动代码:
using UnityEngine;
public class Background : MonoBehaviour
{
/*
*
* A script that's sole purpose is to create a scrolling, infinite background.
* Attached to: Brick
* Scene: Level0
*
*/
public float scrollSpeed;
private Vector2 savedOffset;
private Renderer rndrer;
private float conversionFactor = 0.1f;
public static Background instance;
private void Start()
{
rndrer = GetComponent<Renderer>();
savedOffset = rndrer.material.GetTextureOffset("_MainTex");
}
private void FixedUpdate()
{
this.rndrer.material.SetTextureOffset("_MainTex", new Vector2((GameControl.speed * conversionFactor), 0));
}
private void OnDisable()
{
rndrer.sharedMaterial.SetTextureOffset("_MainTex", savedOffset);
}
}
障碍滚动代码:
using UnityEngine;
public class ObstacleScrolling : MonoBehaviour
{
/*
*
* A script which controls the movement of the Obstacles to make it look like the player is running towards them.
* Attached to: [All Obstacles in the Prefabs folder]
* Scene: Level0
*
*/
private Rigidbody2D rb2d;
public float scrollSpeed;
public float test;
GameObject canvasObj;
Canvas canvas;
// Use this for initialization
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
canvasObj = GameObject.Find("Canvas");
canvas = canvasObj.GetComponent<Canvas>();
test = -1280 * canvas.scaleFactor;
scrollSpeed = scrollSpeed * canvas.scaleFactor; //+ -(Time.timeSinceLevelLoad / 100);
}
// Update is called once per frame
void FixedUpdate()
{
this.transform.Translate(Vector3.right * GameControl.speed);
if (GameControl.instance.gameOver == true)
{
rb2d.velocity = Vector2.zero;
}
}
}
使用这个:
public class GameControl : MonoBehaviour
{
private static float speedFactor = 1.0f; // you can set this to canvas scale
private static float speed = 1.0f;
public static float ScaledSpeed
{
get
{
return speed * speedFactor;
}
}
void FixedUpdate()
{
speed += 0.01f * Time.deltaTime;
}
}
还有这个:
public class Background : MonoBehaviour
{
private Vector2 savedOffset;
private Vector2 offset;
private Renderer rndrer;
private float conversionFactor = 0.1f;
private void Start()
{
rndrer = GetComponent<Renderer>();
savedOffset = rndrer.material.GetTextureOffset("_MainTex");
offset = savedOffset;
}
private void FixedUpdate()
{
offset += new Vector2(GameControl.ScaledSpeed * conversionFactor, 0);
this.rndrer.material.SetTextureOffset("_MainTex", offset);
}
}
还有这个:
public class ObstacleScrolling : MonoBehaviour
{
void FixedUpdate()
{
this.transform.Translate(Vector3.right * GameControl.ScaledSpeed);
}
}