- Home /
Change the direction of the Cube instantiate
Hi , How can I change the direction of where the cubes are instantiated ? with this script just goes in the direction (up) and (down) the cube instantiate , I want that must go only (forward) the cube Instantiate
using UnityEngine;
using System.Collections;
public class InfiniteCubes: MonoBehaviour
{
#region Emission System
public GameObject cube ;
public float life = 1 ;
public float spawnSpeed = 1 ;
private float timer ;
#endregion
#region Start
public void Start()
{
if (cube == null)
{
var generateCube = GameObject.CreatePrimitive (PrimitiveType.Cube);
generateCube.GetComponent<Renderer>().material = new Material (Shader.Find("Diffuse"));
cube = generateCube;
}
}
#endregion
#region Update
public void Update()
{
timer += 1 * Time.deltaTime;
if(timer >= spawnSpeed)
{
GameObject cubeClone;
cubeClone = Instantiate(cube, transform.position, transform.rotation) as GameObject;
cube.transform.parent = this.transform;
Destroy(cubeClone, life);
timer = 0;
}
}
#endregion
}
Do you mean you want change the position along a certain rotation that the clones instantiate?
It's just you're saying forward so it's not clear if you want just the rotation to change or you want to move along a certain rotation and instantiate 1 after another, like this but in a single line not a square of them:
the position of the instantiated cubes and than i do not want the rotation
Answer by tanoshimi · Jan 11, 2015 at 07:55 PM
Rotation is the third parameter you pass to Instantiate.
e.g.
cubeClone = Instantiate(cube, transform.position, Quaternion.identity) as GameObject;
I want that the cubes instantiated , must go only in the position (forward) and not (up) and (down)
I want to instantiate the cubes as if they were the bullets of a gun
I want to create an automatic cannon that shoots cubes , in the forward position
Answer by Mmmpies · Jan 11, 2015 at 08:45 PM
OK this is the script that I used for instantiating those, it creates an array but you just need something more simple.
using UnityEngine;
using System.Collections;
public class testScript : MonoBehaviour {
public GameObject Prefab;
public float timer = 0f;
public float time_of_spawn = 0f;
public int Xn;
public int Zn;
private bool spawn = true;
void Update() {
//timer += Time.deltaTime;
if (spawn)
{
spawn = false;
for (int x = 0; x < Xn; x++)
{
for (int z = 0; z < Zn; z++)
{
Instantiate(Prefab, new Vector3 (100 + x, 0.5f, 100 + z), Quaternion.identity);
//timer = 0f;
}
}
}
}
}
It's based on a question someone else asked so there are some bit's you don't need.
Look at the "new Vector3" I create in the Instantiate command, you only need x or z if you only need a line of Objects.
If you struggling let me know and I'll try and fit it in with your script.
EDIT:
I've re-read this question. It's still not obvious if this is a correct answer. Do you want a line of instantiated objects or do you want to instantiate and move the object forward? Just answer line or move.
Oh and stop posting random text to keep this at the top of the list, it's more likely to make people ignore the post.
EDIT 2:
Then ignore all that. You want to add a rigidbody to the bullet prefab and apply force to it, I do this for arrows:
rigidbody.AddForce(transform.TransformDirection (Vector3.right) * (_force / 5));
just play with the (_force / 5) bit, change that so it's just a number, a low number will fall to the floor quickly so just increase it till it works for your game. Depending on the way your bullet is you may want Vector3.right or Vector3.left or Vector3.forward, well you get the point.
Also search for instantiate bullet on here there will be lots and lots of examples.
I want to instantiate the cubes as if they were the bullets of a gun , in the forward position
Do you want the bullets to not fall to the ground?
Or do you want the bullets not to be affected by gravity?
I think there's a bit of language issue here, that's O$$anonymous$$ what language is do you speak normally?
PS very late where I am so may not get back to you until tomorrow.
$$anonymous$$ust not fall to the ground , and Yes I want that must be affected by gravity
I want to create an automatic cannon that shoots cubes , in the forward position
Your answer
![](https://koobas.hobune.stream/wayback/20220613180121im_/https://answers.unity.com/themes/thub/images/avi.jpg)