Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fasertio41 · Apr 09, 2018 at 05:15 PM · movementbuttonvector3

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);
     }
 
 }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Captain_Pineapple · Apr 09, 2018 at 06:26 PM 2
Share

Do the functions ever get called? Do you get the Debug messages that you set up?

avatar image tormentoarmagedoom Captain_Pineapple · Apr 09, 2018 at 06:38 PM 0
Share

Thats the first step. First, get that confirmation

avatar image Fasertio41 · Apr 10, 2018 at 09:11 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

}

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fasertio41 · Apr 10, 2018 at 12:45 PM 0
Share

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 =(

avatar image SkyAngel_AOG · Apr 10, 2018 at 02:00 PM 0
Share

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(); }

avatar image Fasertio41 SkyAngel_AOG · Apr 10, 2018 at 04:50 PM 0
Share

It works thank you very much!!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

140 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Vector Movement,Vectorel Movement with Buttons 2 Answers

Stopping Vector3.Lerp 3 Answers

Camera wil not move between given points, stays at first given point 1 Answer

Choppy y-axis crane movement when grabbing food, not dropping food on target 0 Answers

Plotting particle movement Time/keyframe issue 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges