Hello everyone! I’m trying to create a game where one object at time fall from the sky and i can control them with two button (for movement) the problem is: when I instantiate the object i can’t using Vector3.MoveForward for moving my object when they fall. I’ve setted two function in the object script and in the OnClick() function of the buttons i’ve setted these functions but don’t work…some ideas? These is my code: (For the object)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Oggetto : MonoBehaviour {
private Rigidbody2D rb2d;
//private BoxCollider2D bx2d;
private float vel = 5f;
private float velrotazione = 5f;
public Vector3 sx = new Vector3(-2.4f, 8.26f, -2.75f);
public Vector3 dx = new Vector3(2.4f, 8.6f, -2.75f);
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D>();
//bx2d = GetComponent<BoxCollider2D>();
}
// Update is called once per frame
void Update () {
rb2d.transform.Translate(Vector3.down * vel * Time.deltaTime, Space.World);
transform.Rotate(Vector3.forward * velrotazione);
}
public void SpostaSx()
{
Debug.Log("Premuto sx");
transform.position = Vector3.MoveTowards(transform.position, sx, 10f);
}
public void SpostaDx()
{
Debug.Log("Premuto dx");
transform.position = Vector3.MoveTowards(transform.position, dx, 10f);
}
}