XNA C# 2D Scrolling Background 3 or More - c#

I've trying to create a scrolling background with 3 background, but everything When the 3rd start to come out. It creates a giant blue screen(Default background of the game) infront of the 3rd and after the 3RD it doesn't show any background. I have no idea how fix this, and I already know it something simple. I've got the 3RD one to work, but .
Code that I'm trying to use.
public void Update(GameTime gameTime)
{
bgPos0.Y += speed;
bgPos1.Y += speed;
bgPos2.Y += speed;
if (bgPos0.Y >= 950)
{
bgPos1.Y = -950;
if (bgPos1.Y >= 950) // Doesn't go fully down.
{
bgPos2.Y = -950;
if (bgPos2.Y >= 950)
{
bgPos0.Y = 0; //(Try to change it to -950 still doesn't work. I guest it due to that bgPos0 is set to 0, 0)
}
}
}
}
And the Vector2 code for the pos are
bgPos0 = new Vector2(0, 0);
bgPos1 = new Vector2(0, -950);
bgPos2 = new Vector2(0, -1900); // Could be the large -1900 number that destroying the code. To make it not work.
So how do I fix this? I wish I could fix it right now, but I can't for some reason.

I don't think you want to have the if-statements nested. The code you posted constantly moves the second background to -950 for as long as the first is past 950. Since the first is constantly moved back to -950 it shouldn't ever manage to move past 950, and so it never gets into the last one. I think what you probably want to do is something more like:
public void Update(GameTime gameTime)
{
bgPos0.Y += speed;
if(bgPos0.Y > 950) {
bgPos0.Y = -950;
}
bgPos1.Y += speed;
if(bgPos1.Y > 950) {
bgPos1.Y = -950;
}
bgPos2.Y += speed;
if(bgPos1.Y > 950) {
bgPos1.Y = -950;
}
}
[EDIT]: As an aside, the number in question isn't nearly large enough to cause problems. XNA's Vector2 class stores the x and y components as floats, and the maximum value for a float in C# is somewhere around 3.4e38 or so, and accurate to 7 digits according to MSDN.

Related

Multi target orthographic camera in unity not working

I'm trying to make a camera track two targets in a 2D game on unity but I can't quite get it to work.
This is the code I currently have, but it's changing the rotation in transform instead of position. the ortho size is changing but it is not properly tracking the center point between the characters. is there anyway I can fix this?
using System.Collections.Generic;
using UnityEngine;
public class CameraZoom : MonoBehaviour
{
private Camera cameraRef;
private GameObject[] playerPos;
void Start()
{
cameraRef = GetComponent<Camera>();
playerPos = GameObject.FindGameObjectsWithTag("Player");
Debug.Log(playerPos[0].transform.position);
Debug.Log(playerPos[1].transform.position);
Debug.Log(cameraRef.tag);
StartCoroutine(ZoomInOut());
}
// Update is called once per frame
void Update()
{
}
IEnumerator ZoomInOut()
{
while (true)
{
if (playerPos[0] != null && playerPos[1] != null)
{
Vector3 lookPoint = Vector3.Lerp(playerPos[0].transform.position, playerPos[1].transform.position, 0.5f);
cameraRef.transform.LookAt(lookPoint);
float distance = Vector3.Distance(playerPos[0].transform.position, playerPos[1].transform.position);
if (distance > (cameraRef.orthographicSize * 2))
{
cameraRef.orthographicSize += 0.05f;
// if (distance < (cameraRef.orthographicSize * 2))
//{
// cameraRef.orthographicSize -= 0.1f;
//}
}
else
if (distance < (cameraRef.orthographicSize))
{
cameraRef.orthographicSize -= 0.05f;
}
yield return new WaitForSeconds(0.02f);
}
}
}
}
Well cameraRef.transform.LookAt does exactly what you described
Rotates the transform so the forward vector points at worldPosition.
Since your game is 2D anyway what you rather want to do is set the camera to exactly the same XY position
var lookPoint = (playerPos[0].transform.position, playerPos[1].transform.position) * 0.5f;
//Maintain the Z depth position
lookPoint.z = cameraRef.transform.position.z;
cameraRef.transform.position = lookPoint;
Also instead of
yield return new WaitForSeconds(0.02f);
rather use
yield return null;
The latter makes it run every frame. But anyway assuming 60FPS a frame takes about 0.017seconds so waiting0.02` will most probably mean you wait two frames since this value is not the exact time but rather the minimum time to wait/skip to the next frame.
And move this OUT of the if block!!
If one of your objects is missing => endless loop -> Freeze/Crash of Unity and your app! You definitely want to wait one frame for every iteration of your while loop!
Actually you could as well do the thing in Update without the loop at all :)
And then instead of
cameraRef.orthographicSize -= 0.05f;
I would rather either use
// Take differences in the frame rate into account (jitter)
cameraRef.orthographicSize -= 3 * Time.deltaTime;
or directly interpolate
// Smooth interpolate -> if the target and current value are far apart
// this zooms faster and then becomes slower when already very close
// Adjust that "5" according to your needs
cameraRef.orthographicSize = Mathf.Lerp(cameraRef.orthographicSize, distance, 5 * Time.deltaTime);
without using your conditions at all.
Typed on the phone but I hope the idea gets clear

Zoom In & Zoom Out in Unity

So, Hey guys. Im new to Unity and Im trying a very simple code that allows the player to Zoom-in & Zoom-Out. Here's the code of it:
[Header("Vertical Zooming")]
[SerializeField] private float zoomSpeed;
private void Update()
{
VerticalZooming();
}
private void VerticalZooming()
{
Vector3 initialPosition = new Vector3();
// bool canMove = false;
if (Input.GetMouseButtonDown(0))
{
initialPosition = mCamRef.ScreenToViewportPoint(Input.mousePosition);
Debug.Log(initialPosition.y);
}
if (Input.GetMouseButton(0))
{
Vector3 newPosition = mCamRef.ScreenToViewportPoint(Input.mousePosition);
Debug.Log("newPosition" + newPosition.y);
if (newPosition.y < initialPosition.y)
{
Zoom((zoomSpeed * Time.deltaTime) * -1f);
}
if (newPosition.y > initialPosition.y)
{
Zoom(zoomSpeed * Time.deltaTime);
}
intialPosition = newPosition;
}
}
private void Zoom(float inZoomSpeed)
{
transform.Translate(mCamRef.transform.position.z * inZoomSpeed * transform.forward);
}
So, if you see the code above. On mousebuttondown we note down the initial position and on holding the mouse button we keep track of new position constantly.. What i want is pretty simple. With reference to my initial mouse click im checking if my new position is greater in y is yes i need to zoom out. If new position is lower than initial position in y-axis. Them, i need to zoom in. Which I did, But for some reason its not working.
Two problems here:
Though if there is no change to newposition i.e., even if both my newposition variable and initial position variable are in same spot my camera moves(Zoom-out in my case) which shouldn't happen. It should only happen if my newPosition.y variable is greater that that of initialposition.y. I'm not able to figure out why this is happening. I tried using bool conditions to do this, but failed.
If i play test this in my mobile.. Its only going on one direction. in my case its only zooming out continously though my zoom-in if-condition meets. But when playtesting in unity game window. The zoom in works when my mouse is out of the game-screen. Because its only working outside the game screen.This is the reason its not working in mobile as there is no space outside mobile display. Which i dont know why this is happening or dont know how to overcome this.
Let me know why! Im stuck at it for a long time and not getting any idea. Thanks <3
If Any doubt in question let me know. Sorry for a huge explanation. Just wanted to be very precise on the question.
I have seen people suggesting zoom in zoom out through using FOV. But i wanna do it using translate or move the camera itself.
So your problem here is on taking count on the screenToViewPort point.
Instead just try this..
if (Input.GetMouseButtonDown(0))
{
mInitialPosition = Input.mousePosition.y;
}
if (Input.GetMouseButton(0))
{
mChangedPosition = Input.mousePosition.y;
if (mChangedPosition == mInitialPosition)
{
return;
}
if (mChangedPosition < mInitialPosition)
{
Zoom((mZoomSpeed * -1f) * Time.deltaTime);
}
if (mChangedPosition > mInitialPosition)
{
Zoom(mZoomSpeed * Time.deltaTime);
}
}
private void Zoom(float inZoomSpeed)
{
transform.Translate(mCamRef.transform.position.z * inZoomSpeed * transform.forward);
}
Not sure how ScreenToViewport point works... But I guess this code should do the magic for you... Here I'm just straight away taking the mouse.y value on click and hold and did the same functions as you did and also telling it not to do anything when both the y values are same and return the function... So meaning that you already had everything in your code itself... Have a nice day

XNA Smooth Downward Swoop Motion

(Before you read, I'm very nervous about posting here since my previous question got a lot of negative responses... Please try and be nice. I am a student and can't write "Perfect" code yet)
I'm currently trying to figure out how to make a mosquito (Texture2D) swoop downwards and then come back up to it's original position.
Currently the mosquitoes simply move left and right within the screen bounds, with no Y movement.
I've stepped it through a debugger and observed it's Y coordinate. My mosquitoes are indeed swooping down and then back upwards again... However, they do it so quickly that it isn't visible to the human eye.
Therefore I need a way to smoothly swoop them down so that it's actually visible.
I am currently unable to embed images into my posts, however I have made a GIF demonstrating the effect that I want.
Here's a link to my GIF
Things I've tried:
Copied the first line of my movement code (seen below) and changed it so that it would only affect the Y coordinates.
Result: Mosquitoes were still swooping too fast to see.
Changing my mosquitoe's velocity.Y to include a Y that isn't 0, so that my movement code will also change the Y position instead of just the X position.
Result: Game was stuck in an infinite loop since my movement code is found in the Update() function, and the code never got out of the loop so that it could update the position...
Here's the movement code I have in my Update.
The mosquitoes move all the way to the right, then all the way to the left.
internal void Update(GameTime GameTime)
{
position += velocity * (float)GameTime.ElapsedGameTime.TotalSeconds;
// Reverses the direction of the mosquito if it hits the bounds
if (position.X <= gameBoundingBox.Left ||
position.X + animationSequence.CelWidth > gameBoundingBox.Right)
{
velocity.X *= -1;
}
}
And here's the actual Swoop() code that I have...
internal void Swoop(GameTime gameTime)
{
float originalYPosition = position.Y;
bool currentlySwooping = true;
while (currentlySwooping)
{
position.Y++;
if (this.BoundingBox.Bottom >= gameBoundingBox.Bottom)
{
currentlySwooping = false;
}
}
while (!currentlySwooping && position.Y > originalYPosition)
{
position.Y--;
}
}
I don't ask questions on here too often, so I'm sorry if I'm using an incorrect format or if I've given too little information.
If you need to know anything else then please let me know.
There are many ways of implementing this, but this should do it.
float originalYPosition = position.Y;
int swoopDirection = 0;
internal void Update(GameTime GameTime)
{
/* Same update code */
if (swoopDirection != 0)
{
position.Y += swoopDirection;
if (swoopDirection == 1 && this.BoundingBox.Bottom >= gameBoundingBox.Bottom)
{
swoopDirection = -1;
}
else if (swoopDirection == -1 && position.Y <= originalYPosition)
{
swoopDirection = 0;
}
}
}
internal void Swoop()
{
swoopDirection = 1;
}
Basically, we have a variable swoopDirection that changes depending on the state of the swoop (1 when going down and -1 when going up). In the update method we check that swoopDirection != 0 (we are swooping). If we are swooping we add the direction to the Y axis and check that we aren't out of bounds. If we touch the bottom we set swoopDirection to -1 so we go up. If we are at the original position swoopDirection is set to 0 and we stop swooping.

Unity Lerping between multiple matierals

I have a need to lerp through 10 different materials attached to one of my game objects but for some reason, the code I have written doesn't work. I have spent the past hour trying to workout why and I'm pretty burnt out.
Could someone with fresher eyes please take a look and see if I'm doing some stupid?
public class LerpMaterials : MonoBehaviour
{
public List<Material> materials = new List<Material>();
public float lerpSpeed;
int currentMaterialNo;
Material currentMaterial;
Material targetMaterial;
bool lerpingMaterial;
float lerp;
void Start ()
{
if (materials.Count < 2) return;
currentMaterialNo = 0;
currentMaterial = materials[currentMaterialNo];
targetMaterial = materials[currentMaterialNo+1];
}
void Update ()
{
if (materials.Count < 2) return;
lerp += lerpSpeed;
renderer.material.Lerp(currentMaterial, targetMaterial, lerp);
if (lerp >= 1)
SwitchMaterial();
}
void SwitchMaterial()
{
if ( currentMaterialNo >= (materials.Count - 1) )
currentMaterialNo = 0;
else
currentMaterialNo++;
currentMaterial = materials[currentMaterialNo];
targetMaterial = materials[currentMaterialNo++];
lerp = 0;
}
}
My list holds every single material and my mesh renderer also holds the required materials as well. But nothing happens other than an instance of the first material appearing in the material renderer. No other movement.
You probably need to set your lerpSpeed.
If lerpSpeed = 0, then no change will happen.
If lerpSpeed > 1, then once per frame, it will change the material.
Since the first material is the only thing showing up and not looping through every material rapidly, lerpSpeed is probably 0. However, you will end up with a problem with this function in that it will change every frame or extremely rapidly. The issue being with how lerp is being incremented.
Instead of:
lerp += lerpSpeed;
Use this instead:
lerp += lerpSpeed * Time.deltaTime;
What this will do is instead of incrementing lerp by lerpSpeed once per frame (which is dependent on the computer's performance), instead, it will increment lerpSpeed by lerpSpeed every second, which will ensure a consistent effect across every machine.

sprite moves too fast after I unlimited the frame rate

I unlimited the frames per second in my game by doing.
graphics.SynchronizeWithVerticalRetrace = false;
IsFixedTimeStep = false;
But now my sprite/player moves WAY faster then it did before. I don't know why it does this, and I am not sure how to fix it.
if (keyboard.IsKeyDown(Keys.W) || keyboard.IsKeyDown(Keys.Up))
{
position.Y -= spd;
}
if (keyboard.IsKeyDown(Keys.A) || keyboard.IsKeyDown(Keys.Left))
{
position.X -= spd;
}
if (keyboard.IsKeyDown(Keys.S) || keyboard.IsKeyDown(Keys.Down))
{
position.Y += spd;
}
if (keyboard.IsKeyDown(Keys.D) || keyboard.IsKeyDown(Keys.Right))
{
position.X += spd;
}
That is currently how I am getting the sprite to move. spd = 4 at the moment. It worked perfectly fine, but now it seems as if it is moving like 2000 times faster. Just taping one of the keys takes him off screen.
Any and all help will be appreciated.
The game loop in XNA is based around update and draw. Fixed time step also refers to update, so by setting that to false, you are telling update to be called as often as possible. As your code is in the update function, its being called more than the default, fixed 60 times per second.
rather than just using spd, change it to
spd * (gameTime.ElapsedGameTime.Milliseconds / 16);
That will change it so that the spd is scaled by the elapsed time. The 16 is the number of milliseconds an update takes at 60 fps (approx) which is what your spd value is currently working at.
EDIT: For not part of Game.cs:
Have an update on the class you are interested in moving (Im going to call it Ship, but it could be anything you want)
class Ship
{
public void Update(GameTime gameTime)
{
...
position.Y += spd * (gameTime.ElapsedGameTime.Milliseconds / 16);
...
}
..
}
Then in the Game.cs file:
public override Update(GameTime gameTime)
{
myShip.Update(gameTime);
...
}
myShip being the variable for the class of the sprite you want to move. Note update is no longer overriding a base method, so the call to base.Update is also gone
When you turn off a fixed timestep, you need to take the time delta into the calculation to have any control over movement. For example:
if (keyboard.IsKeyDown(Keys.W) || keyboard.IsKeyDown(Keys.Up))
{
position.Y -= spd * gameTime.ElapsedGameTime.TotalSeconds;
}
...
In this case, you would set spd to the distance per second, not per frame.

Categories