Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Juliaw02 · Feb 14 at 10:02 AM · collisionarrayvector3teleportteleporting

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);
         }
     }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

273 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Teleport through walls even with colliders and rigidbody 1 Answer

How do I make a secret area in my game 0 Answers

Teleport camera to where is pointing at 3 Answers

Spawning a player to a new place in the same level 1 Answer

need help teleporting an object 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges