Draw a maze in Unity from 2D Arr - c#

I'm creating a game for a game jam and i make a static maze.
The problem is that the maze isn't made the same as in the maze.
{ 1, 0, 1, 1, 1, 1, 1},
{ 1, 0, 0, 0, 1, 0, 1},
{ 1, 1, 1, 0, 0, 0, 1},
{ 1, 0, 0, 0, 1, 0, 1},
{ 1, 0, 1, 1, 1, 0, 1},
{ 1, 0, 0, 0, 1, 0, 1},
{ 1, 1, 1, 0, 1, 1, 1}
And isn't drawn right
Labyrinth
I know that some of the walls are missing but i haven't inserted that code for now.
void CreateWalls() {
for (int i = 0; i < gridSize; i++) {
for (int j = 0; j < gridSize; j++) {
if (grid[i, j] == 1) {
Vector3 position = new Vector3(i * wallSize, -j * wallSize, 1);
Quaternion rotation = transform.rotation;
GameObject createdWall = null;
bool hasTopWall = false;
bool hasLeftWall = false;
bool hasRightWall = false;
bool hasBottomWall = false;
Debug.Log("i: " + i + ", j: " + j);
if (i - 1 >= 0) {
if (grid[i - 1, j] == 1) {
hasLeftWall = true;
}
}
if (i + 1 < gridSize) {
if (grid[i + 1, j] == 1) {
hasRightWall = true;
}
}
if (j - 1 >= 0) {
if (grid[i, j - 1] == 1) {
hasTopWall = true;
}
}
if (j + 1 < gridSize) {
if (grid[i, j + 1] == 1) {
hasBottomWall = true;
}
}
if (hasTopWall && !hasLeftWall && !hasRightWall && hasBottomWall) { // vertical
createdWall = Instantiate(verticalWallPrefab, position, rotation);
} else if (!hasTopWall && hasLeftWall && hasRightWall && !hasBottomWall) { // horizontal
createdWall = Instantiate(horizontalWallPrefab, position, rotation);
} else if (hasTopWall && hasLeftWall && hasRightWall && !hasBottomWall) { // top - left - right
createdWall = Instantiate(threeWayWallTopLeftRightPrefab, position, rotation);
} else if (!hasTopWall && hasLeftWall && hasRightWall && hasBottomWall) { // bottom - left - right
createdWall = Instantiate(threeWayWallBottomLeftRightPrefab, position, rotation);
} else if (hasTopWall && hasLeftWall && hasRightWall && !hasBottomWall) { // top - left - bottom
createdWall = Instantiate(threeWayWallBottomLeftRightPrefab, position, rotation);
} else if (hasTopWall && !hasLeftWall && hasRightWall && hasBottomWall) { // top - right - bottom
createdWall = Instantiate(threeWayWallTopBottomRightPrefab, position, rotation);
} else if (hasTopWall && hasLeftWall && hasRightWall && hasBottomWall) { // four way
createdWall = Instantiate(fourWayWall, position, rotation);
} else if (hasTopWall && hasLeftWall && !hasRightWall && !hasBottomWall) {
createdWall = Instantiate(cornerLeftBottomTop, position, rotation);
} else if (hasTopWall && !hasLeftWall && hasRightWall && !hasBottomWall) {
createdWall = Instantiate(cornerRightBottomTop, position, rotation);
} else if (!hasTopWall && !hasLeftWall && hasRightWall && hasBottomWall) {
createdWall = Instantiate(cornerTopRightBottom, position, rotation);
} else if (!hasTopWall && hasLeftWall && !hasRightWall && hasBottomWall) {
createdWall = Instantiate(cornerTopLeftBottom, position, rotation);
}
if(createdWall != null) {
createdWall.transform.parent = GameObject.Find("WallHolder").transform;
}
}
}
}
}
How do i flip the maze like i want it to?
And do you have a better way to get all the different walls (i need different walls because its a 2.5D Top Down game
I already tried to negate the y and also x coordinates
And I tried to change the rotation during the creation

Related

How to clear Tetris lines in Unity game?

I'm making a simple Tetris game in Unity. Everything works except I can't clear lines and I don't know why.
In my code I put in to check for completed lines. If it is a completed line it should clear the line and all the blocks should move one row down. Unfortunately, my code doesn't work.
public Vector3 rotationPoint;
private float previousTime;
public float fallTime = 0.8f;
public static int height = 20;
public static int width = 10;
private static Transform[,] grid = new Transform[width, height];
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.LeftArrow)) {
transform.position += new Vector3 (-1, 0, 0);
if (!ValidMove ())
transform.position -= new Vector3 (-1, 0, 0);
} else if (Input.GetKeyDown (KeyCode.RightArrow)) {
transform.position += new Vector3 (1, 0, 0);
if (!ValidMove ())
transform.position -= new Vector3 (1, 0, 0);
} else if (Input.GetKeyDown (KeyCode.UpArrow)) {
transform.RotateAround (transform.TransformPoint(rotationPoint), new Vector3 (0, 0, 1), 90);
if (!ValidMove ())
transform.RotateAround (transform.TransformPoint(rotationPoint), new Vector3 (0, 0, 1), -90);
}
if (Time.time - previousTime > (Input.GetKey (KeyCode.DownArrow) ? fallTime / 10 : fallTime)) {
transform.position += new Vector3 (0, -1, 0);
if (!ValidMove ()){
transform.position -= new Vector3 (0, -1, 0);
AddToGrid ();
this.enabled = false;
FindObjectOfType<SpawnTetromino> ().NewTetromino ();
}
previousTime = Time.time;
}
}
void CheckForLines(){
for (int i = height-1; i >= 0; i--){
if(HasLine(i)){
DeleteLine(i);
RowDown(i);
}
}
}
bool HasLine(int i){
for(int j = 0; j< width; j++){
if(grid[j, i] == null)
return false;
}
return true;
}
void DeleteLine(int i){
for (int j = 0; j < width; j++){
Destroy(grid[j, i].gameObject);
grid[j, i] = null;
}
}
void RowDown(int i){
for (int y = i; y < height; y++){
for (int j = 0; j < width; j++){
if(grid[j,y] != null){
grid[j, y - 1] = grid[j,y];
grid[j, y] = null;
grid[j, y - 1].transform.position -= new Vector3(0, 1, 0);
}
}
}
}
void AddToGrid(){
foreach (Transform children in transform) {
int roundedX = Mathf.RoundToInt (children.transform.position.x);
int roundedY = Mathf.RoundToInt (children.transform.position.y);
grid [roundedX, roundedY] = children;
}
}
bool ValidMove(){
foreach (Transform children in transform) {
int roundedX = Mathf.RoundToInt (children.transform.position.x);
int roundedY = Mathf.RoundToInt (children.transform.position.y);
if (roundedX < 0 || roundedX >= width || roundedY < 0 || roundedY >= height) {
return false;
}
if (grid [roundedX, roundedY] != null)
return false;
}
return true;
}
thanks in advance.
Add CheckForLines() to the end of Update method. Currently you are not using it

Unity: Using Mouse Position To Make Angles For My Camera To Rotate Around An Object

I am trying to make a mouse-oriented 3d Agar.io. Right now, I am working on the basic movement and camera readjustments. I want the camera to be able to rotate around an object, but here is the catch: I want to use only the mouse to move the object and camera, and rotate the camera. I, however, was able to configure the logic of this using the distance formula twice and the law of cosines. I based this on the origin point (0, 0). I am positive that there is nothing wrong with the process that I did; however, I know that the way it is being outputted is wrong.
Every time you move the mouse left or right, I want the camera to rotate immediately after. I tried using a FixedUpdate and LateUpdate event to try to make this happen but both did not succeed. The code I tried using for the camera to rotate is this:
void LateUpdate ()
{
// Move Camera
transform.position = new Vector3(camX, 10.5f, camZ);
//Rotate Camera
//transform.eulerAngles = getRotation;
//transform.eulerAngles = Vector3.MoveTowards(currentRotation, getRotation, speed * Time.deltaTime);
}
The full code I have (updated):
public class CameraController : MonoBehaviour {
public float speed;
public GameObject player;
private Ray ray;
private RaycastHit hit;
private Vector3 mousePos;
private float camX;
private float camZ;
private float getA;
private float getB;
private float getC;
private float getAngle;
private Vector3 currentRotation;
private Vector3 getRotation;
void Start ()
{
}
void FixedUpdate()
{
}
void LateUpdate ()
{
if (PlayerController.movedPlayer)
{
if (Physics.Raycast(ray, out hit))
{
mousePos = new Vector3(hit.point.x, 1.3f, hit.point.z);
}
// MouseZ
if (mousePos.z >= player.transform.position.z)
{
// Camera Follow Z+
if ((player.transform.position.z >= 0 && player.transform.position.x >= 0) &&
player.transform.position.z >= player.transform.position.x)
{
camZ = player.transform.position.z - 10;
camX = 0;
}
else if ((player.transform.position.z >= 0 && player.transform.position.x < 0) &&
player.transform.position.z >= -player.transform.position.x)
{
camZ = player.transform.position.z - 10;
camX = 0;
}
else if ((player.transform.position.z >= 0 && player.transform.position.x >= 0) &&
player.transform.position.z < player.transform.position.x)
{
camX = player.transform.position.x - 10;
camZ = 0;
}
else if ((player.transform.position.z >= 0 && player.transform.position.x < 0) &&
player.transform.position.z < -player.transform.position.x)
{
camX = player.transform.position.x + 10;
camZ = 0;
}
// Camera Follow Z-
if ((player.transform.position.z < 0 && player.transform.position.x >= 0) &&
-player.transform.position.z >= player.transform.position.x)
{
camZ = player.transform.position.z + 10;
camX = 0;
}
else if ((player.transform.position.z < 0 && player.transform.position.x < 0) &&
-player.transform.position.z >= -player.transform.position.x)
{
camZ = player.transform.position.z + 10;
camX = 0;
}
else if ((player.transform.position.z < 0 && player.transform.position.x >= 0) &&
-player.transform.position.z < player.transform.position.x)
{
camX = player.transform.position.x - 10;
camZ = 0;
}
else if ((player.transform.position.z < 0 && player.transform.position.x < 0) &&
-player.transform.position.z < -player.transform.position.x)
{
camX = player.transform.position.x + 10;
camZ = 0;
}
}
//PlayerZ
else if (mousePos.z < player.transform.position.z)
{
// Camera Follow Z+
if ((player.transform.position.z >= 0 && player.transform.position.x >= 0) &&
player.transform.position.z >= player.transform.position.x)
{
camZ = player.transform.position.z + 10;
camX = 0;
}
else if ((player.transform.position.z >= 0 && player.transform.position.x < 0) &&
player.transform.position.z >= -player.transform.position.x)
{
camZ = player.transform.position.z + 10;
camX = 0;
}
else if ((player.transform.position.z >= 0 && player.transform.position.x >= 0) &&
player.transform.position.z < player.transform.position.x)
{
camX = player.transform.position.x + 10;
camZ = 0;
}
else if ((player.transform.position.z >= 0 && player.transform.position.x < 0) &&
player.transform.position.z < -player.transform.position.x)
{
camX = player.transform.position.x - 10;
camZ = 0;
}
// Camera Follow Z-
if ((player.transform.position.z < 0 && player.transform.position.x >= 0) &&
-player.transform.position.z >= player.transform.position.x)
{
camZ = player.transform.position.z - 10;
camX = 0;
}
else if ((player.transform.position.z < 0 && player.transform.position.x < 0) &&
-player.transform.position.z >= -player.transform.position.x)
{
camZ = player.transform.position.z - 10;
camX = 0;
}
else if ((player.transform.position.z < 0 && player.transform.position.x >= 0) &&
-player.transform.position.z < player.transform.position.x)
{
camX = player.transform.position.x + 10;
camZ = 0;
}
else if ((player.transform.position.z < 0 && player.transform.position.x < 0) &&
-player.transform.position.z < -player.transform.position.x)
{
camX = player.transform.position.x - 10;
camZ = 0;
}
}
// Make Triangle
getA = mousePos.x - player.transform.position.x;
getB = mousePos.z - player.transform.position.z;
getC = Mathf.Sqrt(Mathf.Pow((getA), 2) + Mathf.Pow((getB), 2));
// Get Rotating Angle.
if (getA == 0 && getB >= 0)
{
getAngle = 0;
}
else if (getA == 0 && getB < 0)
{
getAngle = 180;
}
else if (getB == 0 && getA >= 0)
{
getAngle = 90;
}
else if (getB == 0 && getA < 0)
{
getAngle = 270;
}
else
{
if (getA > 0)
{
getAngle = Mathf.Acos((Mathf.Pow((getB), 2) + Mathf.Pow((getC), 2) - Mathf.Pow((getA), 2)) /
(2 * (getB) * (getC))) * Mathf.Rad2Deg;
}
else if (getA < 0)
{
getAngle = -Mathf.Acos((Mathf.Pow((getB), 2) + Mathf.Pow((getC), 2) - Mathf.Pow((getA), 2)) /
(2 * (getB) * (getC))) * Mathf.Rad2Deg;
}
//getAngle = Mathf.Asin(getA * Mathf.Sin(90) / getC);
//Debug.Log("A: " + getA + " B: " + getB + " C: " + getC + " Angle: " + getAngle);
}
// Set Rotating Angle
currentRotation = transform.eulerAngles;
getRotation = new Vector3(45f, getAngle, 0f);
}
// Move Camera
transform.position = new Vector3(camX, 10.5f, camZ);
//Rotate Camera
//transform.eulerAngles = getRotation;
transform.eulerAngles = Vector3.MoveTowards(currentRotation, getRotation, speed * Time.deltaTime);
}
}
Let me elaborate on the code. I wanted the camera to face toward the origin when the mouse was the closest to it and vise versa. I am having some trouble with this because I don't think I set it up right. My attempt at trying to fix this was using reverse logic (i.e. when the mouse is closer to the origin the camera is the furthest away from the origin [mouse -> object -> camera]. The opposite would be true if the mouse was the furthest from the origin [camera -> object -> mouse].)
Now that you know some of the issues I am having, here is what I need answered (I would prefer you write a solution and tell me how it fixes the problem.): How do I fix the camera rotation so it rotates immediately when the mouse moves, and how can I specify whether the mouse is closer, or not, to the origin?
Update: I moved all the code to LateUpdate because that's when you should do your camera algorithms. I also "tried" to simplify the mouse location part, so it isn't so redundant. I also have a link to a screen capture of my project: https://gyazo.com/2f5ee8f94c0bc55bbdeaeafa8a8b35f4
I found the solution:
transform.rotation = Quaternion.RotateTowards(transform.rotation, getRotation, speed * Time.deltaTime);

If there is a mirror laser ignores other objects

I can't see a problem in code but in game if there is mirror anywhere in front of the beam it ignores all other objects
Please can anyone help me, why this is happening.
I can't ahre this because "It's all code and stuff".
public class LaserBeam : MonoBehaviour {
LineRenderer lr;
public bool isOpen = true;
Vector3 s;
void Start () {
lr = GetComponent<LineRenderer>();
}
void Update () {
s = new Vector3(transform.position.x, transform.position.y + (2 / 5f), transform.position.z);
lr.SetPosition(0, s);
lr.SetWidth(0.3f, 0.3f);
if (isOpen)
{
RaycastHit[] Hit = Physics.RaycastAll(s, transform.forward, 100.0F);
//Debug.Log("isOpen W");
if (Hit.Length > 0)
{
for (int x = 0; x < Hit.Length; x++)
{
Debug.Log(Hit[x].collider.tag + " ID: " + x);
if (Hit[x].collider.tag == "Mirror" || !Hit[x].collider.isTrigger)
{
Debug.DrawLine(s, Hit[x].point, Color.blue);
lr.SetPosition(1, Hit[x].point);
// Debug.Log("loop W" + x);
if (Hit[x].collider.tag == "Mirror") Reflect(s, Hit[x], 0);
else lr.SetVertexCount(2);
break;
}
else if (x == Hit.Length - 1)
{
lr.SetVertexCount(2);
Debug.DrawLine(s, transform.forward * Int16.MaxValue, Color.blue);
lr.SetPosition(1, transform.forward * Int16.MaxValue);
break;
}
}
}
else
{
lr.SetVertexCount(2);
Debug.DrawLine(s, transform.forward * Int16.MaxValue, Color.blue);
lr.SetPosition(1, transform.forward * Int16.MaxValue);
}
}
else
{
lr.SetVertexCount(2);
lr.SetPosition(1, s);
}
}
public void Reflect(Vector3 start, RaycastHit hit, int id)
{
lr.SetVertexCount(id + 3);
Vector3 p = Vector3.Reflect(hit.point - start, hit.normal);
Debug.DrawRay(hit.point, hit.normal * 3);
Debug.DrawLine(hit.point, p + hit.point, Color.blue);
RaycastHit[] Hit1 = Physics.RaycastAll(hit.point, p, 100.0F);
if (Hit1.Length > 0)
{
for (int x = 0; x < Hit1.Length; x++)
{
if (Hit1[x].collider.tag == "Mirror" || !Hit1[x].collider.isTrigger)
{
Debug.DrawLine(hit.point, Hit1[x].point, Color.blue);
//Debug.DrawLine(hit.point, Hit[x].point, Color.blue);
//lr.SetPosition(id + 1,(hit.point + start) / 2);
//lr.SetPosition(id + 2, hit.point);
lr.SetPosition(id + 2, Hit1[x].point);
if (Hit1[x].collider.tag == "Mirror")
{
Reflect(hit.point, Hit1[x], (id + 1));
return;
}
else lr.SetVertexCount(id + 3);
return;
}
else if (x == Hit1.Length - 1)
{
lr.SetVertexCount(id + 3);
Debug.DrawLine(hit.point, Vector3.Normalize(p) * Int16.MaxValue, Color.blue);
//lr.SetPosition(id + 1, (hit.point + start) / 2);
//lr.SetPosition(id + 2, hit.point);
lr.SetPosition(id + 2, Vector3.Normalize(p) * Int16.MaxValue);
return;
}
return;
}
}
else
{
lr.SetVertexCount(id + 3);
Debug.DrawLine(hit.point, Vector3.Normalize(p) * Int16.MaxValue, Color.blue);
//lr.SetPosition(id + 1, (hit.point + start) / 2);
//lr.SetPosition(id + 2, hit.point);
lr.SetPosition(id + 2, Vector3.Normalize(p) * Int16.MaxValue);
return;
}
// Debug.Log(id);
}
}
I can't see a problem in code but in game if there is mirror anywhere in front of the beam it ignores all other objects
Please can anyone help me, why this is happening.
I can't ahre this because "It's all code and stuff".
I don't think you can get away with layers in this case, but you DO NEED to know how to use them, anyways, this should work:
if (Hit.Length > 0)
{
float firstHitDistance = 100000f; //or something ridiculously high
RaycastHit firstHit;
for (int x = 0; x < Hit.Length; x++)
{
if(Hit[x].distance < firstHitDistance){
firstHitDistance = Hit[x].distance;
firstHit = Hit[x];
}
}
if (firstHit.collider.tag == "Mirror" || !firstHit.collider.isTrigger)
{
Debug.DrawLine(s, firstHit.point, Color.blue);
lr.SetPosition(1, firstHit.point);
// Debug.Log("loop W" + x);
if (firstHit.collider.tag == "Mirror") Reflect(s, , 0);
else lr.SetVertexCount(2);
break;
}
}
sorry for bad formatting, I'm on windows :/

Double jump not working as expected

that's my code to handle double jump.But sometimes it doesn't work.
First time I touch the screen my character jumps up ( variable doublejump = true ) . When I touch the screen a second time my character jumps higher(double jump = false)(double jump worked).
But sometimes when I touch the screen for the first time(no need to touch the screen a second time) doublejump variable = false(double jump not working as I expected)
Help me fix that
My code:
void FixedUpdate ()
{
isGrounded = GroundedCheck ();
if (gm.gameState == GameManager.GameState.playing) {
foreach (GameObject g in gm.deadObjects) {
if (g.name == "P13") {
g.GetComponent<Mygroup> ().targetName = "P12";
} else if (g.name == "P12") {
g.GetComponent<Mygroup> ().targetName = "P11";
} else if (g.name == "P11") {
g.GetComponent<Mygroup> ().targetName = "P10";
} else if (g.name == "P10") {
g.GetComponent<Mygroup> ().targetName = "P9";
} else if (g.name == "P9") {
g.GetComponent<Mygroup> ().targetName = "P8";
} else if (g.name == "P8") {
g.GetComponent<Mygroup> ().targetName = "P7";
} else if (g.name == "P7") {
g.GetComponent<Mygroup> ().targetName = "P6";
} else if (g.name == "P6") {
g.GetComponent<Mygroup> ().targetName = "P5";
} else if (g.name == "P5") {
g.GetComponent<Mygroup> ().targetName = "P4";
} else if (g.name == "P4") {
g.GetComponent<Mygroup> ().targetName = "P3";
} else if (g.name == "P3") {
g.GetComponent<Mygroup> ().targetName = "P2";
} else if (g.name == "P2") {
g.GetComponent<Mygroup> ().targetName = "P1";
} else if (g.name == "P1") {
g.GetComponent<Mygroup> ().targetName = "P0";
} else if (g.name == "P0") {
g.GetComponent<Mygroup> ().targetName = null;
}
}
if (this.gameObject.name == "P0") {
targetName = "";
} else if (this.gameObject.name == "P1")
targetName = "P0";
else if (this.gameObject.name == "P2")
targetName = "P1";
else if (this.gameObject.name == "P3")
targetName = "P2";
else if (this.gameObject.name == "P4")
targetName = "P3";
else if (this.gameObject.name == "P5")
targetName = "P4";
else if (this.gameObject.name == "P6")
targetName = "P5";
else if (this.gameObject.name == "P7")
targetName = "P6";
else if (this.gameObject.name == "P8")
targetName = "P7";
else if (this.gameObject.name == "P9")
targetName = "P8";
else if (this.gameObject.name == "P10")
targetName = "P9";
else if (this.gameObject.name == "P11")
targetName = "P10";
else if (this.gameObject.name == "P12")
targetName = "P11";
target = GameObject.Find (targetName);
if (isGrounded) {
jumping = false;
doubleJump = false;
jumpAnim = false;
if (this.gameObject.name == "P0" && isGrounded && !doubleJump && !jumping) {
if (Input.GetMouseButtonDown (0) && this.gameObject.name == "P0") {
jumping = true;
rid.velocity = new Vector3 (0, 50f * jump, 0);
doubleJump = true;
}
}
if (this.gameObject.name == "P0" && !isGrounded && doubleJump && jumping) {
if (Input.GetMouseButtonDown (0) && this.gameObject.name == "P0") {
rid.velocity = new Vector3 (0, 60f * jump, 0);
doubleJump = false;
}
}
if (target != null && this.gameObject.name != "P0" && isGrounded && target.GetComponent<Mygroup> ().doubleJump && !target.GetComponent<Mygroup> ().isGrounded) {
StartCoroutine (Jump ());
}
if (target != null && this.gameObject.name != "P0" && !isGrounded && doubleJump && !target.GetComponent<Mygroup> ().isGrounded && !target.GetComponent<Mygroup> ().doubleJump) {
StartCoroutine (JumpDouble ());
}
}
IEnumerator Jump ()
{
yield return new WaitForSeconds (time);
if (gameObject.name == "P1")
rid.velocity = new Vector3 (0, 49.6f * jump, 0);
if (gameObject.name == "P2")
rid.velocity = new Vector3 (0, 49.4f * jump, 0);
if (gameObject.name == "P3")
rid.velocity = new Vector3 (0, 49.2f * jump, 0);
if (gameObject.name == "P4")
rid.velocity = new Vector3 (0, 49f * jump, 0);
if (gameObject.name == "P5")
rid.velocity = new Vector3 (0, 48.8f * jump, 0);
if (gameObject.name == "P6")
rid.velocity = new Vector3 (0, 48.6f * jump, 0);
if (gameObject.name == "P7")
rid.velocity = new Vector3 (0, 48.4f * jump, 0);
if (gameObject.name == "P8")
rid.velocity = new Vector3 (0, 48.2f * jump, 0);
if (gameObject.name == "P9")
rid.velocity = new Vector3 (0, 48f * jump, 0);
if (gameObject.name == "P10")
rid.velocity = new Vector3 (0, 47.8f * jump, 0);
if (gameObject.name == "P11")
rid.velocity = new Vector3 (0, 47.6f * jump, 0);
if (gameObject.name == "P12")
rid.velocity = new Vector3 (0, 47.4f * jump, 0);
jumping = true;
doubleJump = true;
}
IEnumerator JumpDouble ()
{
yield return new WaitForSeconds (time);
if (doubleJump) {
if (gameObject.name == "P1")
rid.velocity = new Vector3 (0, 59.8f * jump, 0);
if (gameObject.name == "P2")
rid.velocity = new Vector3 (0, 59.6f * jump, 0);
if (gameObject.name == "P3")
rid.velocity = new Vector3 (0, 59.4f * jump, 0);
if (gameObject.name == "P4")
rid.velocity = new Vector3 (0, 59.2f * jump, 0);
if (gameObject.name == "P5")
rid.velocity = new Vector3 (0, 59f * jump, 0);
if (gameObject.name == "P6")
rid.velocity = new Vector3 (0, 58.8f * jump, 0);
if (gameObject.name == "P7")
rid.velocity = new Vector3 (0, 58.6f * jump, 0);
if (gameObject.name == "P8")
rid.velocity = new Vector3 (0, 58.4f * jump, 0);
if (gameObject.name == "P9")
rid.velocity = new Vector3 (0, 58.2f * jump, 0);
if (gameObject.name == "P10")
rid.velocity = new Vector3 (0, 58f * jump, 0);
if (gameObject.name == "P11")
rid.velocity = new Vector3 (0, 57.8f * jump, 0);
if (gameObject.name == "P12")
rid.velocity = new Vector3 (0, 57.6f * jump, 0);
doubleJump = false;
jumping = false;
}
}
I think there is no need to use two variables 'doubleJump & jumping'
Might this work, check following one
if (this.gameObject.name == "P0" && isGrounded && !jumping) {
if (Input.GetMouseButtonDown (0)) {
jumping = true;
rid.velocity = new Vector3 (0, 50f * jump, 0);
}
}
if (this.gameObject.name == "P0" && !isGrounded && jumping) {
if (Input.GetMouseButtonDown (0)) {
rid.velocity = new Vector3 (0, 60f * jump, 0);
jumping = false;
}
}
if (this.gameObject.name == "P0" && !isGrounded && doubleJump && jumping) {
if (Input.GetMouseButtonDown (0) && this.gameObject.name == "P0") {
rid.velocity = new Vector3 (0, 60f * jump, 0);
doubleJump = false;
My suggestion is that you remove '&& jumping' from your if statement.
Hope it helps.
Cheers.

C# XNA - Ball & Brick Continuous Collision Detection

I've been having some trouble applying correct velocity changes to my ball when it hits bricks in my Breakout clone. In a previous question, I was advised to use continuous collision detection, as well as other methods such as finding the intersection between the ball and the brick when it hits a corner to determine which direction the ball should reflect. I've applied this to my code below, but there are still occasions when the ball will just completely plow through a collection of bricks. This is more noticeable when it hits moving bricks.
In Level.cs Update method:
Bricks.ForEach(brick => Balls.ForEach(ball => ball.Collide(brick)));
In Ball.cs:
public bool Touching(Brick brick)
{
var position = Position + (Velocity * Speed);
return position.Y + Size.Y >= brick.Position.Y &&
position.Y <= brick.Position.Y + brick.Size.Y &&
position.X + Size.X >= brick.Position.X &&
position.X <= brick.Position.X + brick.Size.X && brick.Health > 0 && brick.Lifespan == 1F;
}
public void Collide(Brick brick)
{
if (!Touching(brick)) return;
var position = Position + (Velocity * Speed);
var bounds = new Rectangle((int)position.X, (int)position.Y, Texture.Width, Texture.Height);
var nonCCDBounds = new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height);
if (bounds.Intersects(brick.Top) || bounds.Intersects(brick.Bottom))
{
var change = new Vector2(Velocity.X, -Velocity.Y);
if (bounds.Intersects(brick.Left) || bounds.Intersects(brick.Right))
{
var intersection = Rectangle.Intersect(bounds, brick.Texture.Bounds);
var nonCCDIntersection = Rectangle.Intersect(nonCCDBounds, brick.Texture.Bounds);
if (intersection.Width < intersection.Height || nonCCDIntersection.Width < nonCCDIntersection.Height){
change = new Vector2(-Velocity.X, Velocity.Y);
}
}
if (bounds.Intersects(brick.Top))
{
if (level.GetBrick(new Vector2(brick.GridPosition.X, brick.GridPosition.Y - 1)) != null)
change = new Vector2(-Velocity.X, Velocity.Y);
else if ((Position - Velocity).Y > brick.Position.Y)
change = new Vector2(-Velocity.X, Velocity.Y);
}
if (bounds.Intersects(brick.Bottom))
{
if (level.GetBrick(new Vector2(brick.GridPosition.X, brick.GridPosition.Y + 1)) != null)
change = new Vector2(-Velocity.X, Velocity.Y);
else if ((Position - Velocity).Y < brick.Position.Y + brick.Texture.Bounds.Height)
change = new Vector2(-Velocity.X, Velocity.Y);
}
ReflectBall(brick, change);
return;
}
if (bounds.Intersects(brick.Left) || bounds.Intersects(brick.Right))
{
var change = new Vector2(-Velocity.X, Velocity.Y);
if (bounds.Intersects(brick.Top) || bounds.Intersects(brick.Bottom))
{
var intersection = Rectangle.Intersect(bounds, brick.Texture.Bounds);
var nonCCDIntersection = Rectangle.Intersect(nonCCDBounds, brick.Texture.Bounds);
if (intersection.Width > intersection.Height || nonCCDIntersection.Width > nonCCDIntersection.Height)
{
change = new Vector2(Velocity.X, -Velocity.Y);
}
}
if (bounds.Intersects(brick.Left))
{
if (level.GetBrick(new Vector2(brick.GridPosition.X - 1, brick.GridPosition.Y)) != null)
change = new Vector2(Velocity.X, -Velocity.Y);
else if ((Position - Velocity).X > brick.Position.X)
change = new Vector2(Velocity.X, -Velocity.Y);
}
if (bounds.Intersects(brick.Right))
{
if (level.GetBrick(new Vector2(brick.GridPosition.X + 1, brick.GridPosition.Y)) != null)
change = new Vector2(Velocity.X, -Velocity.Y);
else if ((Position - Velocity).X < brick.Position.X + brick.Texture.Bounds.Width)
change = new Vector2(Velocity.X, -Velocity.Y);
}
ReflectBall(brick, change);
}
}
public void ReflectBall(Brick brick, Vector2 reflection)
{
Position = Position - Velocity;
Velocity = reflection;
if (brick.Health < 9)
{
brick.Health--;
brick.Life --;
}
if (brick.Health > 0 && brick.Life > 0)
{
brick.Texture = Assets.GetBrick(brick.TextureName, brick.Health);
}
}
It's a bit of a mess but it's the closest I've got to having decent collision. It would be much easier if there was a fast way of finding out collision points and applying correct velocity changes.

Categories

Resources