mardi 10 mai 2022

unity2d move a game object towards a random position

i have a satellite which i let spawn randomly in a different script by just using the method Satellite() and i want it move towards the bottom of the screen but not towards the middle but again a random position of the ground. the problem is that satellitedirection is always 0. also, if i somehow would manage to get the problem solved, wouldn´t every satellite on the screen move towards the same position?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SatelliteBehaviour : MonoBehaviour
{
    [SerializeField] Rigidbody2D satelliteRB;
    [SerializeField] Transform satelliteFirePoint;
    [SerializeField] GameObject satellitePrefab;
    float satellitedirection;

    void Start()
    {
        

    }

    
    void Update()
    {
        transform.Rotate(0,0,80*Time.deltaTime);
        //satelliteRB.velocity = -transform.up * 5;
        transform.position = Vector2.MoveTowards(transform.position, new Vector2(satellitedirection, -15), 1 * Time.deltaTime);

        Debug.Log(satellitedirection);
        
    }

    public void Satellite()
    {
        Instantiate(satellitePrefab, new Vector2(Random.Range(-18,18),10), Quaternion.identity);
        satellitedirection = Random.Range(-18, 18);
    }
}



Aucun commentaire:

Enregistrer un commentaire