- Home /
Moving an instantiate object with buttons
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);
}
}
Do the functions ever get called? Do you get the Debug messages that you set up?
Thats the first step. First, get that confirmation
Yes, i received the debug log in the button function "SpostaSx()/SpostaDx()" so i don't know why it don't work... the object must move at left or right while is moving down...
Answer by SkyAngel_AOG · Apr 10, 2018 at 10:28 AM
Hi, @Fasertio41!
Try this C# script
using UnityEngine; using UnityEngine.UI;
public class PlayerController : MonoBehaviour {
#region Declaration
[Header("Button right")]
public Button rightButton;
[Header("Button left")]
public Button leftButton;
private Rigidbody2D rb2d;
private float vel = 5f;
private float velrotazione = 5f;
[Header ("transform position vector")]
public Vector3 sx = new Vector3(2.4f, 8.26f, -2.75f);
#endregion
void Start()
{
rightButton.onClick.AddListener(rightButtonEvent);
leftButton.onClick.AddListener(leftButtonEvent);
rb2d = GetComponent<Rigidbody2D>();
}
void Update () {
rb2d.transform.Translate(Vector3.down * vel * Time.deltaTime, Space.World);
transform.Rotate(Vector3.forward * velrotazione);
}
#region movement buttons events
/* if you don`t use ui - rename private to public and apply to your object*/
private void rightButtonEvent()
{
transform.position = Vector3.MoveTowards(transform.position, transform.position - sx, 10f);
}
private void leftButtonEvent()
{
transform.position = Vector3.MoveTowards(transform.position, transform.position + sx, 10f);
}
#endregion
}
Thanks for the answer ^_^,the logic work but the problem is that the object is instatiante so i can't drag and drop in the inspector the button to plug it on the script when is a prefab =(
add these lines at the beginning start()
rightButton = GameObject.Find("name button right").GetComponent(); leftButton = GameObject.Find("name left button").GetComponent();
you should get
void Start() { rightButton = GameObject.Find("name button right").GetComponent(); leftButton = GameObject.Find("name left button").GetComponent(); rightButton.onClick.AddListener(rightButtonEvent); leftButton.onClick.AddListener(leftButtonEvent); rb2d = GetComponent(); }