I know this question is very specific to those familiar with both tiled and monogame, so I'll try to be a thorough as possible. I've been working on this issue for 15+ hours and am desperate.
I'm trying to draw sprites to the screen using Tiled's object layer. I've created a new object layer, drew some rectangles to it, and added a custom property which I reference in my LoadContent method.
Here is my LoadContent Method:
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
mymap = Content.Load<TiledMap>("Misc/newmap");
enemyShip = Content.Load<Texture2D>("Enemies/enemyship");
//TiledMapObject[] islands = mymap.GetLayer<TiledMapObjectLayer>("Islands").Objects;
ship_sprite = Content.Load<Texture2D>("character/wooden_ship");
Texture2D texture = Content.Load<Texture2D>("character/anima2");
animatedSprite = new AnimatedSprite(texture, 1, 2);
font = Content.Load<SpriteFont>("Misc/londrinasolid");
Song song = Content.Load<Song>("Music/Chad_Crouch_-_Stellars_Jay");
MediaPlayer.Volume = 0.7F;
MediaPlayer.Play(song);
TiledMapObject[] littleBoats = mymap.GetLayer<TiledMapObjectLayer>("SmallBoats").Objects;
foreach (var en in littleBoats)
{
string type;
en.Properties.TryGetValue("type", out type);
if (type == "smallboat")
{
Enemy.enemies.Add(new SmallBoat(en.Position));
}
}
// TODO: use this.Content to load your game content here
}
I've created a TiledMapObject Array which takes the items from the tiledmap object layer. It also checks the custom property name. If it it's "type", which you can see labeled in the bottom lefthand corner of the tiledmap picture, it takes that object and adds it to the array at the position on the tiledmap (at least it's supposed to).
In the draw method, I create a temporary Texture2D variable which references the aforementioned enemy list (which is now filled with two elements from the tiled map). If type Smallboat, it then draws the sprite I want to draw, which it does, just at the wrong position. Here is the relevant draw method:
foreach (Enemy en in Enemy.enemies)
{
Texture2D spritetoDraw;
int rad;
if (en.GetType() == typeof(SmallBoat))
{
rad = 16;
spritetoDraw = enemyShip;
}
else
{
rad = 30;
spritetoDraw = ship_sprite;
}
spriteBatch.Draw(spritetoDraw, new Vector2(en.Position.X - rad, en.Position.Y - rad), Color.White);
}
And here is the starting position of the enemy boat once I run, it's slightly offset from the origin 0,0 because in the draw method I subtract the radius:
Here is my Enemy class, with the SmallBoat child class at the bottom. I've created a new list which should contain the objects from the tiled layer once they are added in the LoadContent method in my main file. I've checked that the objects are being added by using Enemy.Remove(), and they are, so I know that at least the program recognizes the objects in the tiled file.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace my_first_game
{
class Enemy
{
private Vector2 position;
protected int radius;
float circle = MathHelper.Pi * 2;
public float rotationAngle;
protected int health;
public static List<Enemy> enemies = new List<Enemy>();
public int Radius { get { return radius; } }
public Vector2 Position { get { return position; }}
public int Health { get { return health; } set { value = health; } }
public Enemy(Vector2 newpos)
{
newpos = position;
}
public void Update(GameTime gametime, Vector2 playerPos)
{
rotationAngle = circle % 2;
double dt = gametime.ElapsedGameTime.TotalSeconds;
Vector2 direction = new Vector2((float)Math.Cos(rotationAngle), (float)Math.Sin(rotationAngle));
Vector2 movedir = Position - playerPos;
movedir.Normalize();
position -= movedir;
}
}
class SmallBoat : Enemy
{
public SmallBoat(Vector2 newpos) : base(newpos)
{
//radius = 50;
}
}
}
What I've tried:
1). commented out all update methods and ran, nothing changes except the ship's don't move.
2). Drawn new rectangles, erased old ones, deleted and created object layers.
3). if I remove a rectangle from the object layer, there are less boats drawn to the screen once the game is running. Therefore I know that the enemies are being drawn to the screen, just not at the right position.
4). Changed the coordinates to a hardcoded fixed number in the draw method instead of en.position.X and en.position.Y. This changes the starting position to the desired location, but it's hardcoded and doesn't solve the problem.
5). Rearranged elements in the loadcontent method and draw method, to no avail.
It seems that the list is being properly referenced and that the issue is that for some reason the coordinates on the tiled map aren't transfering over. Much of my code is based off of a Udemy Monogame tutorial, and the loadcontent method and draw method are basically the same.
Any and all help is warmly welcomed. Please let me know if there's any other information I can provide.
Edit: Here is the entire draw method.
protected override void Draw(GameTime gameTime)
{
controller.controller_update(gameTime);
spriteBatch.Begin();
if (controller.ingame == false)
{
spriteBatch.DrawString(font, "Welcome to Thallasaphobia. \n press Enter to Begin", new Vector2(100, 100), Color.White);
}
spriteBatch.End();
if (controller.ingame == true)
{
GraphicsDevice.Clear(Color.Black);
mapRenderer.Draw(mymap, cam.GetViewMatrix());
spriteBatch.Begin(transformMatrix: cam.GetViewMatrix());
animatedSprite.Draw(spriteBatch, new Vector2(480, 350));
//create a new rectangle around the ship_sprite
Rectangle ship_rectangle = new Rectangle(0, 0, ship_sprite.Width, ship_sprite.Height);
Vector2 origin = new Vector2(ship_sprite.Width / 2, ship_sprite.Height + 15);
foreach (Enemy en in Enemy.enemies)
{
Texture2D spritetoDraw;
int rad;
if (en.GetType() == typeof(SmallBoat))
{
rad = 16;
spritetoDraw = enemyShip;
}
else
{
rad = 30;
spritetoDraw = ship_sprite;
}
spriteBatch.Draw(spritetoDraw, new Vector2(en.Position.X - rad, en.Position.Y - rad), Color.White);
}
spriteBatch.Draw(ship_sprite, ship.Position, ship_rectangle, Color.White, ship.rotationAngle + MathHelper.Pi/2, origin, 1.0f, SpriteEffects.None, 1);
}
spriteBatch.End();
base.Draw(gameTime);
}
Although I don't really have a means of testing this, I believe the problem is in your constructor. You currently have this code:
public Enemy(Vector2 newpos)
{
newpos = position;
}
You should actually have:
public Enemy(Vector2 newpos)
{
position = newpos;
}
Since "position" is the property of the Enemy class, THAT is the value we want updated, not newpos, which is just a parameter. So that would explain why your enemy is starting at the origin, because its position is never being initially set.
Related
Ive been working for a few days on this problem, trying to create a square grid of tiles, using Unity Tilemaps seems to be the most efficient way, and it has a lot built in already.
I want to highlight the tiles around the player so that he will see what the legal moves are.
I use a second Tilemap for this, on top of the basemap(ground/grass).
The second Tilemap, the highlightsMap is made up of invisible Tiles, which will turn into highlighted Tiles when a Physics.SphereOverlap occurs, or a Physics2D.CircleAll
To detect every Tile, I have added a box collider on an empty object and I made a grid of these colliders on top of the Tilemap grid, so that:
Box Collider at Position (2,4,0) is exactly on top of Tile at Position(2,4,0)
This should be the most straightforward way to handle this, as you can change a Tile, using Tilemap.SetTile(Vector3Int Pos, Tile tile)
The problem is very strange however. The colliders have the correct positional values to be able to reference the tiles exactly underneath them, just through that position data. As explained above, and I have double checked this, the collider empties have the exact same position as the tiles underneath them, no conversion is needed.
The problem is that the tiles are not highlighting around the player as expected, instead a few next to the player are highlighted and others arent, the Physics.OverlapSphere I am using only works on 3D Colliders, which is why I added them as another grid on top of everything.
Using Physics2D.CircleAll, unfortunately does not detect any 2D colliders on the tiles themselves(Sprite, or Grid), not sure if that is intended to work like that anyway.
Collision Grid Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class scr_ColliderGrid : MonoBehaviour
{
public GameObject eCollider;
public Tile invisibleTile;
public Tile highlightTile;
public Tilemap highlightMap;
public int xSize, ySize;
private Vector3Int[] gridArray; // Tilemaps use Vector3Int BUT only use (X, Y, 0) !!!!!!!
private int xC = 0, yC = 0, i = 0;
private Tile[] tiles;
private Vector3Int previous;
private void Start()
{
gridArray = new Vector3Int[xSize * ySize];
tiles = new Tile[xSize * ySize];
GenerateCollisionGrid();
}
private void GenerateCollisionGrid()
{
for (xC = 0; xC < xSize; xC++)
{
for (yC = 0; yC < ySize; yC++)
{
Vector3Int newPos = new Vector3Int(xC, yC, 0); // 2, 4, 0
Vector3Int newColPos = new Vector3Int(xC, yC, 0); // 2, 4, 0 //This used to be different values, but now they are exactly the same.
if (invisibleTile != null)
{
Tile tile = Instantiate(invisibleTile, newPos, Quaternion.identity, transform);
tiles[i] = tile;
GameObject col = Instantiate(eCollider, newColPos, Quaternion.identity, transform);
}
gridArray[i] = newPos;
i++;
}
}
highlightMap.SetTiles(gridArray, tiles);
highlightMap.SetTile(new Vector3Int(2,4,0), highlightTile); // 2,4,0 //Test to see if positions are the same. (collider and tiles)
}
Player Highlight Legal Moves Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Tilemaps;
public class scr_TankMoves : MonoBehaviour
{
public Tile highlightTile;
public TileBase[] highlightTiles;
public Tilemap highlightMap;
public int maxMoveTiles;
public bool highlight;
private Vector3Int previous, previousLeft, previousRight, previousForward, previousAft, previousVect;
private Vector3Int[] previousVectors;
void Start()
{
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
transform.localPosition = new Vector3(transform.localPosition.x,
transform.localPosition.y + 1,
transform.localPosition.z );
}
if (Input.GetKeyDown(KeyCode.S))
{
transform.localPosition = new Vector3(transform.localPosition.x,
transform.localPosition.y - 1,
transform.localPosition.z );
}
if (Input.GetKeyDown(KeyCode.D))
{
transform.localPosition = new Vector3(transform.localPosition.x + 1,
transform.localPosition.y,
transform.localPosition.z);
}
if (Input.GetKeyDown(KeyCode.A))
{
transform.localPosition = new Vector3(transform.localPosition.x - 1,
transform.localPosition.y,
transform.localPosition.z);
}
}
void LateUpdate()
{
if (highlight)
HighlightMoves();
else EraseHighlights();
}
private void HighlightMoves()
{
previousVectors = new Vector3Int[1];
highlightMap.SetTiles(previousVectors, null);
int tMax = 0;
Collider[] legalTiles = Physics.OverlapSphere(transform.position, maxMoveTiles/2);
previousVectors = new Vector3Int[legalTiles.Length];
foreach (Collider col in legalTiles)
{
//Vector3 conversionVector = new Vector3(col.transform.localPosition.x, col.transform.localPosition.y, col.transform.localPosition.z);
Vector3Int tileVector = Vector3Int.FloorToInt(col.transform.position);
previousVectors[tMax] = tileVector;
tMax++;
}
highlightMap.SetTiles(previousVectors, highlightTiles);
}
private void EraseHighlights()
{
//highlightMap.SetTile(previousForward, null);
//highlightMap.SetTile(previousAft, null);
//highlightMap.SetTile(previous, null);
//highlightMap.SetTile(previousRight, null);
//highlightMap.SetTile(previousLeft, null);
highlightMap.SetTiles(previousVectors, null);
}
private void OnDrawGizmos()
{
Gizmos.DrawWireSphere(transform.position, maxMoveTiles/2);
}
}
}
If you open a new 3D Project in Unity and setup a grid with a Tilemap, called highlightMap in my code example, you should be able to recreate this exactly, using the 2 scripts.
Everything is oriented in 2D, this means x+ is Right y+ is forward and z+ is up/dephth (unused by tilemaps).
The empty Collider prefab I have is an empty GO with pos 0,0,0. it has another empty GO Child, which has the Box Collider Component, and the transform value of this child is 0.5,0.5,0.5, so that the Collider is centered on top of each tile.
This is after 0 moves, so just pressed Play:
This is after 1 move forward, (y+1):
Instead of going by colliders at all afaik you could also just use GridLayout.WorldToCell like e.g.
Tilemap yourTileMap;
// see https://docs.unity3d.com/ScriptReference/Tilemaps.Tilemap-layoutGrid.html
var grid = yourTileMap.layoutGrid;
// The "Grid" implements "GridLayout"
// see https://docs.unity3d.com/ScriptReference/Grid.html
var currentCellPos = grid.WorldToCell(transform.position);
var currentCell = yourTileMap.GetTile(currentCellPos);
Then if you need to get multiple tiles I would probably go the "lazy/stupid" way and just do something like e.g.
var currentCellPos = grid.WorldToCell(transform.position);
var currentCell = yourTileMap.GetTile(currentCellPos);
var leftCellPos = grid.WorldToCell(transform.position - transform.right * someRange);
var leftCell = yourTileMap.GetTile(leftCellPos);
var rightCellPos = grid.WorldToCell(transform.position + transform.right * someRange);
var rightCell = yourTileMap.GetTile(rightCellPos);
var frontCellPos = grid.WorldToCell(transform.position + transform.forward * someRange);
var frontCell = yourTileMap.GetTile(frontCellPos);
var backCellPos = grid.WorldToCell(transform.position - transform.forward * someRange);
var backCell = yourTileMap.GetTile(backCellPos);
highlightTiles[tMax] = Instantiate(highlightTile, col.transform.parent.position, Quaternion.identity, transform);
I dont know why, but apparently I didnt instantiate the tiles themselves into an Array of tiles, so everytime I was ''placing'' them on the grid, it was actually placing an empty array.
Now its fixed! Just need to find a nice way to erase all after a move and im set!
I've looked through what seems like every question and resource there is for Triangle.NET trying to find an answer to how to insert a vertex into an existing triangulation. The closest I've gotten was in the discussion archives for Traingle.Net where someone asked a similar question (discussion id 632458) but unfortunately, the answer was not what I was looking for.
My goal here is to make a destructible wall in Unity where, when the player shoots the wall, it will create a hole in the wall (like in Rainbow Six Siege).
Here's what I did for my original implementation:
Create initial triangulation using the four corners of the wall.
When the player shoots, perform a raycast, if the raycast intersects with the wall then add the point of intersection to the polygon variable and re-triangulate the entire mesh using that variable.
Draw new triangulation on the wall as a texture to visualise what's happening.
Repeat.
As you can see, step 2 is the problem.
Because I re-triangulate the entire mesh every time the player hits the wall, the more times the player hits the wall the slower the triangulation gets as the number of vertices rises. This could be fine I guess, but I want destructible walls to play a major role in my game so this is not acceptable.
So, digging through the Triangle.Net source code, I find an internal method called InsertVertex. The summary for this method states:
Insert a vertex into a Delaunay triangulation, performing flips as necessary to maintain the Delaunay property.
This would mean I wouldn't have to re-triangulate every time the player shoots!
So I get to implementing this method, and...it doesn't work. I get an error like the one below:
NullReferenceException: Object reference not set to an instance of an object
TriangleNet.TriangleLocator.PreciseLocate (TriangleNet.Geometry.Point searchpoint, TriangleNet.Topology.Otri& searchtri, System.Boolean stopatsubsegment) (at Assets/Triangle.NET/TriangleLocator.cs:146)
I have been stuck on this problem for days and I cannot solve it for the life of me! If anyone who is knowledgeable enough with the Triangle.NET library would be willing to help me I would be so grateful! Along with that, if there is a better alternative to either the implementation or library I'm using (for my purpose which I outlined above) that would also be awesome!
Currently, how I've set up the scene is really simple, I just have a quad which I scaled up and added the script below to it as a component. I then linked that component to a shoot raycast script attached to the Main Camera:
How the scene is setup.
What it looks like in Play Mode.
The exact Triangle.Net repo I cloned is this one.
My code is posted below:
using UnityEngine;
using TriangleNet.Geometry;
using TriangleNet.Topology;
using TriangleNet.Meshing;
public class Delaunay : MonoBehaviour
{
[SerializeField]
private int randomPoints = 150;
[SerializeField]
private int width = 512;
[SerializeField]
private int height = 512;
private TriangleNet.Mesh mesh;
Polygon polygon = new Polygon();
Otri otri = default(Otri);
Osub osub = default(Osub);
ConstraintOptions constraintOptions = new ConstraintOptions() { ConformingDelaunay = true };
QualityOptions qualityOptions = new QualityOptions() { MinimumAngle = 25 };
void Start()
{
osub.seg = null;
Mesh objMesh = GetComponent<MeshFilter>().mesh;
// Add four corners of wall (quad in this case) to polygon.
//foreach (Vector3 vert in objMesh.vertices)
//{
// Vector2 temp = new Vector2();
// temp.x = map(vert.x, -0.5f, 0.5f, 0, 512);
// temp.y = map(vert.y, -0.5f, 0.5f, 0, 512);
// polygon.Add(new Vertex(temp.x, temp.y));
//}
// Generate random points and add to polygon.
for (int i = 0; i < randomPoints; i++)
{
polygon.Add(new Vertex(Random.Range(0.0f, width), Random.Range(0.0f, height)));
}
// Triangulate polygon.
delaunayTriangulation();
}
// When left click is pressed, a raycast is sent out. If that raycast hits the wall, updatePoints() is called and is passed in the location of the hit (hit.point).
public void updatePoints(Vector3 pos)
{
// Convert pos to local coords of wall.
pos = transform.InverseTransformPoint(pos);
Vertex newVert = new Vertex(pos.x, pos.y);
//// Give new vertex a unique id.
//if (mesh != null)
//{
// newVert.id = mesh.NumberOfInputPoints;
//}
// Insert new vertex into existing triangulation.
otri.tri = mesh.dummytri;
mesh.InsertVertex(newVert, ref otri, ref osub, false, false);
// Draw result as a texture onto the wall so to visualise what is happening.
draw();
}
private void delaunayTriangulation()
{
mesh = (TriangleNet.Mesh)polygon.Triangulate(constraintOptions, qualityOptions);
draw();
}
void draw()
{
Texture2D tx = new Texture2D(width, height);
// Draw triangulation.
if (mesh.Edges != null)
{
foreach (Edge edge in mesh.Edges)
{
Vertex v0 = mesh.vertices[edge.P0];
Vertex v1 = mesh.vertices[edge.P1];
DrawLine(new Vector2((float)v0.x, (float)v0.y), new Vector2((float)v1.x, (float)v1.y), tx, Color.black);
}
}
tx.Apply();
this.GetComponent<Renderer>().sharedMaterial.mainTexture = tx;
}
// Bresenham line algorithm
private void DrawLine(Vector2 p0, Vector2 p1, Texture2D tx, Color c, int offset = 0)
{
int x0 = (int)p0.x;
int y0 = (int)p0.y;
int x1 = (int)p1.x;
int y1 = (int)p1.y;
int dx = Mathf.Abs(x1 - x0);
int dy = Mathf.Abs(y1 - y0);
int sx = x0 < x1 ? 1 : -1;
int sy = y0 < y1 ? 1 : -1;
int err = dx - dy;
while (true)
{
tx.SetPixel(x0 + offset, y0 + offset, c);
if (x0 == x1 && y0 == y1) break;
int e2 = 2 * err;
if (e2 > -dy)
{
err -= dy;
x0 += sx;
}
if (e2 < dx)
{
err += dx;
y0 += sy;
}
}
}
private float map(float from, float fromMin, float fromMax, float toMin, float toMax)
{
float fromAbs = from - fromMin;
float fromMaxAbs = fromMax - fromMin;
float normal = fromAbs / fromMaxAbs;
float toMaxAbs = toMax - toMin;
float toAbs = toMaxAbs * normal;
float to = toAbs + toMin;
return to;
}
}
Great news! I've managed to fix the issue. InsertVertex() doesn't actually add the new vertex to the list of vertices! So this means that when it tried to triangulate, it was trying to point to the new vertex but it couldn't (because that vertex wasn't in the list). So, to solve this, I just manually add my new vertex to the list of vertices in the mesh, before calling InsertVertex(). Note: When you do this, you also need to manually set the vertex's id. I set the id to the size of the list of vertices because I was adding all new vertices to the end of the list.
// When left click is pressed, a raycast is sent out. If that raycast hits the wall, updatePoints() is called and is passed in the location of the hit (hit.point).
public void updatePoints(Vector3 pos)
{
// Convert pos to local coords of wall. You don't need to do this, i do it because of my draw() method where i map everything out onto a texture and display it.
pos = transform.InverseTransformPoint(pos);
pos.x = map(pos.x, -0.5f, 0.5f, 0, 512);
pos.y = map(pos.y, -0.5f, 0.5f, 0, 512);
Vertex newVert = new Vertex(pos.x, pos.y);
// Manually add new vertex to list of vertices.
newVert.id = mesh.vertices.Count;
mesh.vertices.Add(newVert.id, newVert);
//Doing just the first line gave me a null pointer exception. Adding the two extra lines below it fixed it for me.
otri.tri = mesh.dummytri;
otri.orient = 0;
otri.Sym();
// Insert new vertex into existing triangulation.
mesh.InsertVertex(newVert, ref otri, ref osub, false, false);
// Draw result as a texture onto the wall so to visualise what is happening.
draw();
}
Hope this will help someone done the road!
I'm new to C# and Monogame, and I'm trying to create an effect of a 'psychic' ability, the user will press a key and a circular aura blast will emit from their position. I'm just trying to get the actual blast working before setting up the positions and all that, but my problem is when trying to get all the particles to move out from the origin of a circle to the outside.
I've never worked with circles yet and only with Rectangles so my knowledge of this is very basic. You'll probably recognise the code from a youtube channel and that's because I've been trying to learn from wherever I can, most of the time that leads me to youtube since a video demonstration works best but I digress.
This is my code for the particle generator so far.
namespace Particles
{
class ParticleGenerator
{
Texture2D texture;
float spawnWidth;
float density;
List<Particles> particles = new List<Particles>();
float timer;
public ParticleGenerator(Texture2D newTexture, float newSpawnWidth, float newDensity)
{
texture = newTexture;
spawnWidth = newSpawnWidth;
density = newDensity;
}
public void createParticle(GraphicsDevice graphics)
{
particles.Add(new Particles(texture, new Vector2(graphics.Viewport.Width / 2 , graphics.Viewport.Height /2), new Vector2(5, 1)));
}
public void Update(GameTime gameTime, GraphicsDevice graphics)
{
timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
while (timer > 0)
{
timer -= 1f / density;
createParticle(graphics);
}
for (int i = 0; i < particles.Count; i++)
{
particles[i].Update();
if (particles[i].Position.Y > graphics.Viewport.Height)
{
particles.RemoveAt(i);
i--;
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
foreach (Particles particle in particles)
{
particle.Draw(spriteBatch);
}
}
}
}
Many thanks for any help.
I'm working on a 2D Unity app and I'm encountering some weird behavior.
This code works just fine.
Debug.DrawLine(button1.transform.position, button2.transform.position, Color.green);
When I run the app, I see a green line in the Scene view.
But nothing appears in the Game view when I have the following line.
Physics2D.Linecast(button1.transform.position, button2.transform.position);
I'm confused as to how Unity is able to draw a line between these two buttons, but for some reason, it's just not doing it in the Game view.
Any idea how I'd troubleshoot this?
Just line Serlite said, Physics2D.Linecast is not used to draw a line but to detect if there is an object in the middle of two objects with raycast. It has nothing to do with drawing lines.
As you already know, Debug.DrawLine will only work in the Scene view unless Gizmos is enabled. You can just LineRenderer or the GL functions to draw lines which will work without enabling Gizmos and will also work in a build.
Here is a helper class for drawing line in the Game and Scene view.
public struct LineDrawer
{
private LineRenderer lineRenderer;
private float lineSize;
public LineDrawer(float lineSize = 0.2f)
{
GameObject lineObj = new GameObject("LineObj");
lineRenderer = lineObj.AddComponent<LineRenderer>();
//Particles/Additive
lineRenderer.material = new Material(Shader.Find("Hidden/Internal-Colored"));
this.lineSize = lineSize;
}
private void init(float lineSize = 0.2f)
{
if (lineRenderer == null)
{
GameObject lineObj = new GameObject("LineObj");
lineRenderer = lineObj.AddComponent<LineRenderer>();
//Particles/Additive
lineRenderer.material = new Material(Shader.Find("Hidden/Internal-Colored"));
this.lineSize = lineSize;
}
}
//Draws lines through the provided vertices
public void DrawLineInGameView(Vector3 start, Vector3 end, Color color)
{
if (lineRenderer == null)
{
init(0.2f);
}
//Set color
lineRenderer.startColor = color;
lineRenderer.endColor = color;
//Set width
lineRenderer.startWidth = lineSize;
lineRenderer.endWidth = lineSize;
//Set line count which is 2
lineRenderer.positionCount = 2;
//Set the postion of both two lines
lineRenderer.SetPosition(0, start);
lineRenderer.SetPosition(1, end);
}
public void Destroy()
{
if (lineRenderer != null)
{
UnityEngine.Object.Destroy(lineRenderer.gameObject);
}
}
}
Usage:
LineDrawer lineDrawer;
void Start()
{
lineDrawer = new LineDrawer();
}
void Update()
{
lineDrawer.DrawLineInGameView(Vector3.zero, new Vector3(0, 40, 0f), Color.blue);
}
When done, you can call lineDrawer.Destroy();.
Debug.DrawLine renders a line gizmo into the Scene View.
If you want a line rendered into the Game View, use a Line Renderer Component.
Line Renderer Docs
I am using a tile map where if a moving platform hits a tile it should go another direction. I am using a list for both the tilemap and platform, this helps keep things neat.
The image below shows the platform, it is constantly in motion and when it collides with one of the black circles it should change directions and head the opposite way it was going.
Unfortunately I am having a problem where I need to find the correct platform so it will go the other direction. I created the list in the main class and do not know how to call upon the list inside a another class besides the main.
Question:
How would I call upon a list that is holding multiple objects going in different directions, in different position, and is created in the main class, so that I can know if the platform is colliding with a tile?
Simply put, how do I make the platform collide with a tile?
Here is the process that the tile map must go through to be used and called:
To make the tile map two classes are used, Block class and Game1 class (the main class).
Inside the Block class the Texture, Position, and BlockState (which decides what thing it will do)
public Block(Texture2D Texture, Vector2 Position, int BlockState)
{
this.Texture = Texture;
this.Position = Position;
this.BlockState = BlockState;
}
Then the Block class will use a method that uses the Player class.
public Player BlockCollision(Player player)
{
Rectangle top = new Rectangle((int)Position.X + 5, (int)Position.Y - 10, Texture.Width - 10, 10);
Rectangle bottom = new Rectangle((int)Position.X + 5, (int)Position.Y + Texture.Height, Texture.Width - 10, 10);
Rectangle left = new Rectangle((int)Position.X - 10, (int)Position.Y + 5, 10, Texture.Height - 10);
Rectangle right = new Rectangle((int)Position.X + Texture.Width, (int)Position.Y + 5, 10, Texture.Height - 10);
if (BlockState == 1 || (BlockState == 2 && !player.goingUp))
{
if (top.Intersects(new Rectangle((int)player.Position.X, (int)player.Position.Y, player.Texture.Width, player.Texture.Height)))
{
if (player.Position.Y + player.Texture.Height > Position.Y && player.Position.Y + player.Texture.Height < Position.Y + Texture.Height / 2)
{
player.Position.Y = player.ground = Position.Y - player.Texture.Height;
player.Velocity.Y = 0;
player.isJumping = false;
player.Time = 0;
player.botCollision = true;
}
}
}
return player;
}
Then the Draw method.
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(Texture, new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height), Color.White);
}
We then move over to the main class Game1.
I create the Lists for both Blocks and the Platforms.
List<Block> Blocks;
List<MovingPlatform> Platforms;
Using a char system I create the map.
List<char[,]> Levels = new List<char[,]>();
public int tileWidth, tileHeight;
Then in the Initialize method I call on Lists and create the map.
protected override void Initialize()
{
Blocks = new List<Block>();
Platforms = new List<MovingPlatform>();
char[,] Level1 = {{'.','.','#'},
{'.','.','#'},
{'.','.','#'}};
Levels.Add(Level1);
base.Initialize();
}
Then a void LoadLevel is called on.
void LoadLevel(int level)
{
Blocks.Clear();
Platforms.Clear();
player.Position = Vector2.Zero;
tileWidth = Levels[level].GetLength(1);
tileHeight = Levels[level].GetLength(0);
Texture2D blockSpriteA = Content.Load<Texture2D>("blockA2");
for (int x = 0; x < tileWidth; x++)
{
for (int y = 0; y < tileHeight; y++)
{
//Background
Blocks.Add(new Block(background, new Vector2(x * 50, y * 50), 0));
//Impassable Blocks
if (Levels[level][y, x] == '#')
{
Blocks.Add(new Block(blockSpriteA, new Vector2(x * 50, y * 50), 1));
}
//Vertical Moving Platform
if (Levels[level][y, x] == '=')
{
Platforms.Add(new MovingPlatform(platform, new Vector2(x * 50, y * 50), 3.0f, true));
}
Then In the Update I call on the lists.
foreach (Block b in Blocks)
{
player = b.BlockCollision(player);
}
foreach (MovingPlatform m in Platforms)
{
player = m.BlockCollision(player);
m.Update(gameTime);
}
Finally the Draw method calls on them.
foreach (Block b in Blocks)
{
b.Draw(spriteBatch);
}
foreach (MovingPlatform m in Platforms)
{
m.Draw(spriteBatch);
}
I think I understand your question, I am not sure, but I am going to hazard an answer anyway.
I think what you need to do is to move stuff out of the main Game class. You have a concept of a map and a bunch of stuff that belongs to a map (platforms, blocks) so it's a good idea to encapsulate this in its own class, eg:
class Map
{
public List<Block> Blocks;
public List<MovingPlatform> Platforms;
void LoadLevel(int level)
{
///
}
}
Now it makes the code cleaner, but also you now have a Map object you can pass around.
So for example to enable a MovingPlatform to have access to everything on a map, you just need to pass a reference to a map as a parameter to the MovingPlatform constructor which saves it away to a private field. Eg:
class MovingPlatform
{
Map _map;
public MovingPlatform(Map map, Vector2 position, ...)
{
_map = map;
}
public void Update(GameTime gameTime)
{
//move platform
//detect collision
//have access to all the blocks and other platforms on map
//_map.Blocks;
//_map.Platforms;
}
}
So in the map class LoadLevel() method you'd pass in 'this' as the first parameter of the MovingPlatform constructor:
class Map
{
void LoadLevel(int level)
{
//Vertical Moving Platform
if (Levels[level][y, x] == '=')
{
Platforms.Add(new MovingPlatform(this, ...));
}
}
}
You can add other map specific things (even a reference to the player?) to the Map class and the MovingPlatforms will automatically have access to them.
This is called Dependency Injection. Your MovingPlatform class has a dependency on the map and you are injecting that dependency in through the constructor.
Edit:
For collision you could add a Bounds property to Tile and MovingPlatform class. That makes it easier to get the current rectangle bounds of them. Also an IntersectsTile method in MovingPlatform that will return true if the platform intersects a specified tile.
class Tile
{
Rectangle Bounds
{
get
{
return new Rectangle((int)Position.X, (int)Position.Y, tileWidth, tileHeight);
}
}
}
class MovingPlatform
{
Rectangle Bounds
{
get
{
return new Rectangle((int)Position.X, (int)Position.Y, platformWidth, platformHeight);
}
}
//returns true if this platform intersects the specified tile
bool IntersectsTile(Tile tile)
{
return Bounds.Intersects(tile.Bounds);
}
}
Then in the MovingPlatfom class in the Update method where the platform is moved each frame, check if there is a collision after the movement. If there is a collision then back out the movement and reverse the platform direction, so next frame it will move the opposite way.
class MovingPlatform
{
void Update(Tile[] collidableTiles)
{
Position += direction;
//test collision
bool isCollision = CollisionTest(collidableTiles);
if (isCollision)
{
//undo the movement and change direction
Position -= direction;
direction = newDirection;
}
}
//returns true if this platform intersects with any of the specified tiles
bool CollisionTest(Tile[] tiles)
{
foreach (Tile tile in tiles)
if (IntersectsTile(tile))
return true;
return false;
}
}
For the above to work you need to pass in a list of collidable tiles on the map to the MovingPlatform update. Call each MovingPlatform in the map class, passing it the list of collidable tiles.
class Map
{
List<Tile> _collidableTiles;
void Update(GameTime gameTime)
{
foreach (MovingPlatform platform in MovingPlatforms)
{
platform.Update(_collidableTiles);
}
}
}
That's one way of doing it. A better way though would be instead of passing in collidable tiles, get the MovingPlatform to grab tiles around it and test those.
class MovingPlatform
{
void Update()
{
Position += direction;
//test collision
Tile[] tiles = _map.GetNearbyTiles(Position);
foreach (Tile tile in tiles)
if (tile.IsSolid)
if (IntersectsTile(tile))
{
//undo the movement and change direction
Position -= direction;
direction = newDirection;
break;
}
}
}
So that way all tiles have an IsSolid property and any that are set to true will cause moving platforms to collide with them.
class Tile
{
bool IsSolid;
}