Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Code formatting, clarifying
Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

Instantiate an object by translatingand translate it randomly

My goal through this code in C # is withthat each support of a key totime the player presses the Q I create a cube, to at a fixed position given by another object, but takingthen begin translating it in a random direction translation

support 1> translation 1

support2> translation 2

support 3> translation 3

etc..... 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

Here is myI don't understand why the code, but it 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;
 
    // Use this for initialization
    void StartUpdate () {
        
    }
    
    // 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= 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
            );
         }
    }
}

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 code, but it 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;
 
    // 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);
         }
    }
}

Instantiate an object and translate it randomly

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
            );
        }
    }
}
Corrected code indentation.
Source Link

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......

hereHere 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 {

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

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

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 code, but it 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;

    // 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);
         }
    }
}
edited tags
Link
Vaillancourt
  • 16.4k
  • 17
  • 55
  • 61
Source Link
Loading