Here’s the script I don’t see anything wrong with it, maybe you guys can find out.
public GameObject enemyMissile;
public Transform player;
public int maxpos = 1.5f;
public float missileDelay = 0.5f;
public float missilecooldownTimer = 0;
void Start ()
{
if (GameObject.Find ("Player"))
player = GameObject.Find ("Player").transform;
}
void Update ()
{
missilecooldownTimer -= Time.deltaTime;
if (missilecooldownTimer <=0 && player)
{
Vector3 maxpos = new Vector3(Random.Range(1.5f, -1.5f), transform.position.y, transform.position.z);
missilecooldownTimer = missileDelay;
Instantiate(enemyMissile, maxpos, transform.rotation);
}
}
}
thanks in advance.
What exactly is the problem?
You have your Random.Range values backwards, you have maximum before minimum.
Change it to this:
Random.Range(-1.5f, 1.5f)
The script has no errors and the problem is the missile is lunched only from the center of the enemy, not in random position
GroZZleR:
You have your Random.Range values backwards, you have maximum before minimum.
Change it to this:
Random.Range(-1.5f, 1.5f)
Vector3 maxpos = new Vector3(Random.Range(-1.5f, 1.5f), transform.position.y, transform.position.z);
I did and still didn’t change anything.
I notice you have
“public int maxpos = 1.5f;”
Are you showing us you doing this
“Vector3 maxpos = new Vector3 (Random .Range (-1.5f, 1.5f), transform.position .y, transform.position .z);”
but in reality you are actually doing this?
“Vector3 someName = new Vector3 (Random .Range (-maxpos, maxpos), transform.position .y, transform.position .z);”
Maybe your int maxpos, although is set to 1.5f in the script, is actually still 0 in the inspector?
Though, I dont think it would even let you do that since your vector3 is also named maxpos, so if you plugged maxpos, it would think you are putting in a vector3. This may not be the issue, but it might be a possibility that the code you are showing us isnt completely what you have.
Well I removed the int in max pos, and this is the whole script I attched, and problem still going on.
When I run this code
using UnityEngine;
public class TestRandomRange : MonoBehaviour
{
public GameObject prefab;
void Update()
{
Vector3 maxpos = new Vector3(Random.Range(1.5f, -1.5f), transform.position.y, transform.position.z);
Instantiate(prefab, maxpos, transform.rotation);
}
}
Everything works as expected.
Make a new scene with just your script and do a test just to make sure you don’t have anything causing problems.
Post your project if you want me to look at it.
Try making the numbers larger. If your scene is big you wont notice a difference.
I tried it didn’t do anything, but I have the spawning script for enemies, and it works fine with the same set up it’s really so weird!
post an example project of the problem so we can look at it ourselves.
Maybe you didnt put the script on the gameobject and instead still have an old script there. Or maybe some other reasons.