Skip to main content
2 of 4
edited tags
Vaillancourt
  • 16.4k
  • 17
  • 55
  • 61

Instantiate an object by translating it randomly

My goal through this code in C # is with each support of a key to create a cube, to a fixed position given by another object, but taking a random direction translation

support 1> translation 1

support2> translation 2

support 3> translation 3

etc......

here is my correct code but it does not work.

Why?

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class spawn_random_trajet : MonoBehaviour {

public GameObject Spawn, Spawn2;
public Transform origineSpawn;
public float moveSpeed=5f;


// Use this for initialization
void Start () {


    
}

// Update is called once per frame
void Update () {

    if (Input.GetKeyDown (KeyCode.Q)) {

        int random_trajet_x = Random.Range (-10,10);
        int random_trajet_y = Random.Range (-10,10);
        int random_trajet_z = Random.Range (-10,10);


        Spawn2 =Instantiate (Spawn, origineSpawn.position, origineSpawn.rotation) as GameObject;
        transform.Translate (random_trajet_x*Time.deltaTime*moveSpeed,random_trajet_y*Time.deltaTime*moveSpeed,random_trajet_z*Time.deltaTime*moveSpeed);

    
}

} }

thanks