How to teleport an object to 1 of 8 possible locations on collision?
I'm pretty new to unity and I don't really know c# very well (I've mostly just been figuring stuff out from my limited knowledge of javascript). I'm making my own version of Brick Breaker and I'm trying to make a type of brick (that resembles a mask) where, after being hit once, it'll teleport to 1 of 8 possible locations in an array. I'm not sure if I've been making and/or using the array wrong or if I shouldn't even be using an array for this. I would appreciate any and all suggestions, thank you! Here is the script I've been using for my bricks:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyBrick : MonoBehaviour
{
public int numberOfHits = 0;
public int maxHits;
public Sprite noHit;
public Sprite oneHit;
public SpriteRenderer brickSprite;
public int brickValue;
public Transform powerup;
public GameMaster gameMaster;
public Vector3[] maskLocations;
void Start()
{
brickSprite = GetComponent<SpriteRenderer>();
gameMaster.GetComponent<GameMaster>();
maskLocations = new Vector3[8];
maskLocations[0] = new Vector3(-7.5f, 1.75f, 0f);
maskLocations[1] = new Vector3(-4.5f, 0.75f, 0f);
maskLocations[2] = new Vector3(-1.5f, -0.25f, 0f);
maskLocations[3] = new Vector3(1.5f, -0.25f, 0f);
maskLocations[4] = new Vector3(7.5f, -0.25f, 0f);
maskLocations[5] = new Vector3(-4.5f, -0.25f, 0f);
maskLocations[6] = new Vector3(-7.5f, 0.75f, 0f);
maskLocations[7] = new Vector3(-7.5f, 3.75f, 0f);
}
void Update()
{
if (this.CompareTag("Mask") && numberOfHits >= 1 && numberOfHits < maxHits)
{
int i = Random.Range(0, maskLocations.Length);
transform.position = maskLocations[i].position;
}
}
private void OnCollisionEnter2D(Collision2D other)
{
numberOfHits++;
transform.GetComponent<SpriteRenderer>().sprite = oneHit;
if (this.transform.CompareTag("Bricks"))
{
int randomChance = Random.Range(1, 101);
if (randomChance < 30)
{
Instantiate(powerup, this.transform.position, other.transform.rotation);
}
}
if (numberOfHits >= maxHits)
{
gameMaster.UpdateScore(+brickValue);
Destroy(this.gameObject);
}
}
}
Answer by Supertang · Feb 15 at 09:02 AM
I believe you can just declare your Vector3-array like this:
maskLocations = new Vector3[] {
new Vector3(-7.5f, 1.75f, 0f),
new Vector3(-4.5f, 0.75f, 0f),
new Vector3(-1.5f, -0.25f, 0f),
new Vector3(1.5f, -0.25f, 0f),
new Vector3(7.5f, -0.25f, 0f),
new Vector3(-4.5f, -0.25f, 0f),
new Vector3(-7.5f, 0.75f, 0f),
new Vector3(-7.5f, 3.75f, 0f)
};
not that it makes a difference. Other than that I don't know what you're asking. Are you getting an error, what is not working for you?
Edit: On line 40 you shouldn't say .position after the array, a position is just a Vector3, which it already is.
Your answer
Follow this Question
Related Questions
Teleport through walls even with colliders and rigidbody 1 Answer
Field value changes after SceneManager.LoadScene been called 0 Answers
Creating a new vector3 2 Answers
How to transform position of one object to the exact position of another object? 0 Answers
To know what is in a circle, use circle collider or Vector3.Distance? 2 Answers