Hi guys, I’ve finally figured out how to get a bunch of projectiles to fire in a circle, however they always instantiate in the middle of the screen, instead of the gameobjects position. Anyone have any ideas?
using UnityEngine;
using System.Collections;
using System;
public class MedEnemyProjectile : MonoBehaviour {
public GameObject enemybullet;
private double radius=0.1;
private double pointx,pointy;
private double angle,angle1,angle2;
private Vector3 projposition;
private float speed=10;
private Transform myTransform;
private float amtToMove;
private bool shoot=false;
// Use this for initialization
void Start () {
angle=0;
myTransform=transform;
StartCoroutine("drawcircle");
}
// Update is called once per frame
void Update () {
}
private double DegreeToRadian(double a)
{
double answer;
answer = Math.PI * (a/180.0);
return answer;
}
IEnumerator drawcircle()
{
for(int i=0;i<360;i+=18)
{
angle1=Math.Cos(DegreeToRadian(angle+i));
angle2=Math.Sin(DegreeToRadian(angle+i));
pointx=radius*angle1;
pointy=radius*angle2;
projposition=new Vector3((float)pointx,(float)pointy,0);
Instantiate(enemybullet,projposition,Quaternion.identity);
}
yield return new WaitForSeconds(5f);
}
}
It is the point of the projectile on the circumference of the circle, each iteration of the loop moves a point along a certain number of degrees specified, in this case 18 degrees every cycle. I
Yup I’ve figured that much out, but trying to implement it into the code, if I replace projposition with transform.position the bullets just all go in a straight line right.
Add projPosition to the position of the nozzle? If the position of the nozzle (or where you want to fire from) is myTransform.position. (myTransform being your cached transform) Another thing, in Start you need to cache the transform first, before you start the coroutine.
Why does it work this way? You’ve found out where a the point on the circle is, from a position of 0,0,0. So now you want to make it relative to the enemy’s position, adding it to the enemy’s coordinates.
Hmm tried that, and when I do I get a null pointer exception. NullReferenceException
UnityEngine.Transform.get_position () (at C:/BuildAgent/work/842f9557127e852/Runtime/ExportGenerated/Editor/UnityEngineTransform.cs:19)
MedEnemyProjectile+c__Iterator2.MoveNext () (at Assets/Scripts/MedEnemyProjectile.cs:55)
UnityEngine.MonoBehaviour:StartCoroutine(String)
MedEnemyProjectile:Start() (at Assets/Scripts/MedEnemyProjectile.cs:20)
Did you fix your Start() function like I said? To make sure ( myTransform = transform; )is done before you call the coroutine?
If you don’t know what is happening when you cache locally like that just use transform.position. But remember that’s a really bad way to do it if you want speed.
I did, but had it in the wrong place in the line, I moved it, but when I do myTransform.position+projposition, it just shoots all the bullets in 1 direction and not in a circle pattern anymore.
But does it at least shoot from the enemy’s position? Need some more info here. Do they still start in a circle pattern, but shoot in a line? Or do they all start at one spot and go in a line?
How do your bullets themselves know what direction to fly? When you instantiate they only know the starting position, not the direction to move it.
Were you actually using the starting position as the velocity too? You’ll need to separate that. The bullets will need two types of information.
The start location: myTransform.position + projposition
The direction to move in: projposition
I think the only reason it worked correctly was the position and the velocity happened to be the same.
But I can’t be sure without more code or more information about how things are going wrong.
Ah that helps a bit, so I made the radius bigger, and yes they appear to be starting in a circle and going right one at a time, the script I linked about is the one that is on the enemy, the bullets have this on them to move them.
using UnityEngine;
using System.Collections;
public class testbullet : MonoBehaviour{
public float speed;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate(transform.position * speed * Time.deltaTime);
}
}
They don’t instantiate on the enemy, always in the middle of the screen at 0,0,0. Anyways I’ve gotten them to finally instantiate on the enemy, however now they don’t spread out and expand the circle, they just sit in a circle position and move towards the assigned vector.
using UnityEngine;
using System.Collections;
using System;
public class MedEnemyProjectile : MonoBehaviour {
public GameObject enemybullet;
private double radius=5;
private double pointx,pointy;
private double angle,angle1,angle2;
private Vector3 projposition;
private float speed=3;
private float amtToMove;
private Transform myTransform;
// Use this for initialization
void Start () {
angle=0;
amtToMove=speed*Time.deltaTime;
myTransform=transform;
StartCoroutine("drawcircle");
}
// Update is called once per frame
void Update () {
//myTransform.Translate(Vector3.left * amtToMove);
}
private double DegreeToRadian(double a)
{
double answer;
answer = Math.PI * (a/180.0);
return answer;
}
IEnumerator drawcircle()
{
for(int i=0;i<360;i+=18)
{
angle1=Math.Cos(DegreeToRadian(angle+i));
angle2=Math.Sin(DegreeToRadian(angle+i));
pointx=radius*angle1+myTransform.position.x;
pointy=radius*angle2+myTransform.position.y;
projposition=new Vector3((float)(pointx),(float)(pointy),0);
Instantiate(enemybullet,projposition,Quaternion.identity);
}
yield return new WaitForSeconds(0.35f);
}
}
I’d actually like them to stay in the place they instantiate and spread out from that point, any ideas?
your code, should work. It seems that the only suggestion I have is that the position of the vector, where you were instantiating was 0,0,0. So I would think that just changing this to enemy position would do the trick.