Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Apr 20, 2016 at 09:13 AM by Internetman.
avatar image
0
Question by Internetman · Apr 04, 2016 at 02:53 PM · c#physicsinstantiatepositiondirection

Make projectile move in certain direction depending on position for base object (C#)

Hello once again my fellow Unites! I'm developing a 2D mobile survival game and I have a little problem, I've created an exploding enemy, that is supposed to shoot out 4 projectiles in a certain direction depending on which position that then enemy has chosen.

I have managed to instantiate the 4 projectiles once the exploding enemy has "exploded", but here's where the problem begins, the projectiles just flys off into a random direction, how can I fix this? I would like to set the direction for the projectiles, depending on which position the "exploding enemy" went to. So that the projectiles does not just fly off in a random.range direction.

Here's what it looks like now (The "big ball" is the exploding enemy and it can go to certain position on the screen, the "small balls" are the projectiles):

alt text

Here's an image for how I would like it to be: alt text

and here's the code for the "exploding enemy": (Note that the Transform moveToPoints is where the exploding enemy is supposed to go"

 using UnityEngine;
  using System.Collections;
  
  public class EnemyExplodeScript : MonoBehaviour {
      
      public Transform[] moveToPoints;
  
  
      public Transform explodingEnemyTransform;
  
      public GameObject instantiatedEnemy1;
      public GameObject instantiatedEnemy2;
      public GameObject instantiatedEnemy3;
      public GameObject instantiatedEnemy4;
  
      int movePoint = 0; 
      public float speed;
      public float explodeTimer = 3.0f;
  
      public Rigidbody2D enemyMove;
  
      public ParticleSystem preExplosion;
      public ParticleSystem explosion;
      
  
      void Start () 
      {
          //Find the which points the enemy should follow in the array
          moveToPoints[0] = GameObject.Find("MoveRightUp").transform;
          moveToPoints[1] = GameObject.Find("MoveLeftDown").transform;
          moveToPoints[2] = GameObject.Find("MoveRightDown").transform;
          moveToPoints[3] = GameObject.Find("MoveRightUp").transform;
          moveToPoints[4] = GameObject.Find("MoveMiddle").transform;
  
          //Set where the enemy should go with the array
          movePoint = Random.Range(0, moveToPoints.Length);
      }
  
  
      void Update () 
      {
          // Speed and time 
          float step = speed * Time.deltaTime;
  
          //Get the enemy moving
          transform.Translate (Vector3.MoveTowards (transform.position, moveToPoints[movePoint].position, step) - transform.position);
      
  
      }
  
      void ExplosionSpawnEnemy()
      {
          // Get the transform ahead of time for readability
          //Transform explodingEnemyTransform = GameObject.Find("Enemy_explode").transform;
          
          // Instantiate the four projectiles
          GameObject explodingEnemyBall1 = Instantiate(instantiatedEnemy1, explodingEnemyTransform.position, explodingEnemyTransform.rotation) as GameObject;
          GameObject explodingEnemyBall2 = Instantiate(instantiatedEnemy1, explodingEnemyTransform.position, explodingEnemyTransform.rotation) as GameObject;
          GameObject explodingEnemyBall3 = Instantiate(instantiatedEnemy1, explodingEnemyTransform.position, explodingEnemyTransform.rotation) as GameObject;
          GameObject explodingEnemyBall4 = Instantiate(instantiatedEnemy1, explodingEnemyTransform.position, explodingEnemyTransform.rotation) as GameObject;
      }


and last but not least here's the code for the instantiated projectiles, note that they right now just go off in a random range direction. I want to set the direction that they fly off to depending on the position on which the exploding enemy is in:

 using UnityEngine;
  using System.Collections;
  
  public class EnemyExplodeBallScript : MonoBehaviour 
  {
      Rigidbody2D bouncing;
      public float speed = 30;
  
      public float timer = 3.0f;
      
      Vector3 velocity;
      
      void Start() 
      {
          //transform.position = Vector3.zero;
          transform.rotation = Quaternion.identity;
          
          **// Random range where the ball is supposed to fly
          float angle = Random.Range (0, 180);**
          
          **// Calculate velocity for ball
          velocity = new Vector2 (Mathf.Cos (angle), Mathf.Sin(angle));**
          
          // Get the ball moving! 
          this.GetComponent<Rigidbody2D> ().velocity = velocity * speed;
  
  
          
      }

You guys are just great, I'm grateful for any help that I can get! Thank you!

exploding-ball-problem4.png (12.8 kB)
exploding-ball-problem3.png (17.7 kB)
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

  • Sort: 
avatar image
0

Answer by Brocccoli · Apr 04, 2016 at 08:48 PM

Wouldn't you want 4 different angles for each ball?

0, 90, 180, 270? This would be for a cross, so I suppose your angles would be 45, 135, 225, 315.

I would encapsulate the idea of the "explosion with 4 balls" into one Component Script, so that on death of the gameObject, the balls are instantiated and fly in the proper directions

Not sure if this is a big help or not.

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

Follow this Question

Answers Answers and Comments

137 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

Related Questions

Game Object doesn't instantiate at Mouse Position 3 Answers

Photon Player instantiate position 1 Answer

how do i make player die when falling certain height? 3 Answers

How can I change position of instantiate objects (clones)? 1 Answer

Wrong position (always 0,0,0) when Instantiate prefab. 1 Answer


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