- Home /
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;
}
}
}
}
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)
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
Your answer
Follow this Question
Related Questions
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