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 /
avatar image
0
Question by FawkingAwesome · May 16, 2017 at 04:43 PM · unity 5

Unity 5 coding objects to fly towards you

this code is suppose to generate randomly placed planes in the distance that will fly towards you while the objective is to dodge them, however it just spawns random planes that don't move. It just gradually spawns them closer to your plane and keeps spawning them even if it goes past it, while the ones previously spawned stay in the same spot, so it's just a trail of non-moving planes, how would I code it so they approach me? Anything helps, thanks!

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class CloudGen : MonoBehaviour {

 public GameObject cloud;
 public GameObject enemy;

 int firstRand;
 int secondRand;
 int distance = 12;
 int time = 0;


 float x;
 float y;
 float z;

 Vector3 intPos;


 void Update () {
     if (Input.GetButtonDown ("left") || Input.GetButtonDown ("right")) 
     {
         firstRand = Random.Range (1, 3);
         if(firstRand == 1)
         {
             secondRand = Random.Range (1, 8);
             GameObject cloudInt = Instantiate (cloud) as GameObject;
             for (int i = 0; i < secondRand; i++) 
             {
                 x = Random.Range (-8, 16);
                 y = 0;
                 z = 287;
                 intPos = new Vector3 (x, y, z);
                 distance -= 1;
                 cloudInt.transform.position = intPos;
             }
         }
         if (firstRand == 2) 
         {
             secondRand = Random.Range (1, 8);

             for (int i = 0; i < secondRand; i++) 
             {
                 intPos = new Vector3 (Random.RandomRange(1, 4), 0, distance);                    }
                 GameObject enemyInt = Instantiate (enemy) as GameObject;
                 enemyInt.transform.position = intPos;
                 enemyInt.transform.Translate (distance, 0, 0);
                 distance -= 1;
             }
         }
     }
 }
Comment
Add comment · Show 1
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
avatar image toddisarockstar · May 16, 2017 at 07:30 PM 0
Share

anytime you want to get a direction between two points you simply subtract the two vector3s. if you want to move something in that direction you just add the answer to the position.

so you simply subtract the players postition from the planes position. and add the answer every frame to the plane. (you would pry want to normalize and multiply by a speed first though)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Piyush_Pandey · May 17, 2017 at 06:58 AM

This is not working because the enemyInt.transform.Translate (distance, 0, 0); is called only when you press the your keys down. This is only once per frame. Hence it will call this stuff

 firstRand = Random.Range (1, 3);
          if(firstRand == 1)
          {
              secondRand = Random.Range (1, 8);
              GameObject cloudInt = Instantiate (cloud) as GameObject;
              for (int i = 0; i < secondRand; i++) 
              {
                  x = Random.Range (-8, 16);
                  y = 0;
                  z = 287;
                  intPos = new Vector3 (x, y, z);
                  distance -= 1;
                  cloudInt.transform.position = intPos;
              }
          }
          if (firstRand == 2) 
          {
              secondRand = Random.Range (1, 8);
              for (int i = 0; i < secondRand; i++) 
              {
                  intPos = new Vector3 (Random.RandomRange(1, 4), 0, distance);                    }
                  GameObject enemyInt = Instantiate (enemy) as GameObject;
                  enemyInt.transform.position = intPos;
                  enemyInt.transform.Translate (distance, 0, 0);
                  distance -= 1;
              }
          }

only on the frame you pressed your key. For translate to work, it should call on every frame . If you want to translate your enemies towards camera, you can add a script to the enemies which will translate it. I wrote a simple short script for you which you can try.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MoveEnemy : MonoBehaviour 
 {
     private Transform thisTrans;
     public float speed = 1;
     void Start()
     {
         thisTrans = transform;
     }
     
     void Update ()
     {
         thisTrans.Translate(0,0,-speed*Time.deltaTime);
     }
 }
 

Just attach this script to the enemy prefab and the moment it is spawned, it will start moving. Please adjust the translate direction the way you want

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to run a game built in Unity (executable for linux) in a Ubuntu 16.04 VM on Google Cloud Platform? 3 Answers

Animation Event is won't be called 0 Answers

Animation Played when key pressed Help 1 Answer

How to draw a line in ARcore cloud anchor? 0 Answers

converting IntPoint to Vector3 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