My goal through this code in C # is that each time the player presses the Q I create a cube at a fixed position given by another object, then begin translating it in a random direction. ie.
- first press: a cube is created at the spawn point and moves away in direction 1
- second press: a cube is created at the spawn point and moves away in direction 2
- third press: a cube is created at the spawn point and moves away in direction 3
I don't understand why the code below does not work:
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;
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
);
}
}
}