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 /
  • Help Room /
avatar image
0
Question by jonahsrocket · Jul 21, 2016 at 08:53 PM · vector3movement scriptmove an objectgameobject.findclick objects

Move gameobject when clicking on another gameobject

Hi there,

I want to move one object (called 'Manlooptdeftrans_0') horizontal to a certain position when another object is clicked. I attached the code (below) to the object that is clicked. It triggers an animation of the other object and with GameObject.find I try to also make it move. But I get an error about the vector3 element and can't figure out what I should do to solve this.

Hope someone can help me out. Thanks in advance!!!!

Cheers!

Jonah

 using UnityEngine;
 using System.Collections;
 
 public class ManLoopt : MonoBehaviour
 {
 
     public Animator anim; //anim kan ook een andre naam zijn, is willekurig
 
         
     
 
     // Use this for initialization
     void Start()
     {
 
     }
 
     // Update is called once per frame
     void Update()
     {
 
             
     }
     void OnMouseDown()
     {
         anim.SetTrigger("Lopen");
         GameObject.Find("Manlooptdeftrans_0").transform.position = Vector3(2, 0, 0);
 
 
     }
 }
Comment
Add comment · Show 1
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 Robxd0 · Jul 21, 2016 at 09:15 PM 0
Share

GameObject.Find() is pretty expensive in terms of performance and shouldn't be used very often. You can try to make a public GameObject field and assign the it the GameObject that you wish to move.

However, if this doesn't work, it would be useful to also attach the error that you are getting.

1 Reply

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

Answer by nathanvj · Jul 22, 2016 at 01:33 AM

Hi fellow Nederlander @jonahsrocket!

I made this script for you. In the script you can read further explanations. You can delete all the comments after you understand the script.

 using UnityEngine;
 using System.Collections;
 
 public class ManLoopt : MonoBehaviour
 {
     public Transform doel;
     public float snelheid = 3;
 
     Animator anim;
     GameObject man;
     bool clicked;
 
     /* 1. Maak een EMPTY GAMEOBJECT aan, en plaats deze op de positie waar de man heen moet lopen.
      * 2. In je spel doe je deze script het object dat geklikt moet worden.
      * 3. In de inspector sleep je bij 'Doel: (Transform)' het EMPTY GAMEOBJECT (met de positie waar de man heen moet lopen).
      * 4. Zet de snelheid zo snel jij het wilt. */
 
     void Start()
     {
         // Als je vergeten bent om een doel of snelheid te assignen in de inspector
         // In case you forgot to assign a target or speed in the inspector
         if (doel == null || snelheid == 0) {
             Debug.LogError ("Je bent vergeten je doel of snelheid in te stellen!");
         }
 
         // Go is altijd False bij opstarten
         // Set Go to false by default
         clicked = false;
 
         // Find the man GameObject
         man = GameObject.Find ("Manlooptdeftrans_0");
 
         // Ik neem aan dat de Animator component van de Man is? Zoja dan kan je dit doen:
         // If the Animator is a component of the men you can do this:
         anim = man.GetComponent<Animator>();
     }
 
     void Update () {
 
         // Als er op het ene object geklikt is, en dus clicked = true
         // If the object has been clicked so that clicked = true
         if (clicked) {
             float step = snelheid * Time.deltaTime;
             man.transform.position = Vector3.MoveTowards (man.transform.position, doel.position, step);
         }
 
     }
 
     void OnMouseDown()
     {
         // Set Animatie Trigger lopen en zet ook clicked = true
         // Set Animation Trigger Lopen en set clicked to be true
         anim.SetTrigger("Lopen");
         clicked = true;
     }
 }
Comment
Add comment · Show 4 · 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 jonahsrocket · Jul 22, 2016 at 07:40 AM 0
Share

He @zunor. Helemaal top deze reactie. Ik ga hem eens bestuderen. Ben net begonnen met Unity, dus deze hulp is zeer welkom!

Hey @Zunor. Thanks a lot. I will study the code. Just started Unity so this explanation is very welcome!

avatar image nathanvj · Jul 23, 2016 at 12:23 AM 0
Share

Glad you like it. Let me know if you encounter any problems! ;-)

avatar image jonahsrocket nathanvj · Jul 26, 2016 at 07:00 PM 0
Share

@Zunor It works (jeehaa) but I don't want the gameobject to go up and down, only horizontal. Is there a way to prevent the gameobject from going up or down?

avatar image nathanvj jonahsrocket · Jul 27, 2016 at 12:47 AM 0
Share

Hey Jonah, glad that worked!

Yeah, that's rather easy. Just place the Target GameObject on the same Y-axis as the $$anonymous$$an. So if the $$anonymous$$ans' Y-position is 5, then you would change the Target GameObject Y-position to 5.

Another option is to use Vector2.$$anonymous$$oveTowards ins$$anonymous$$d.

You would replace:

  man.transform.position = Vector3.$$anonymous$$oveTowards (man.transform.position, doel.position, step);

With:

 man.transform.position = Vector2.$$anonymous$$oveTowards (man.transform.position, doel.position, step);

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

69 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

Related Questions

how to make your object move from point A to point B? 1 Answer

how do i fix the compiler errors on unity 5? i have tried everything there is to try. 0 Answers

NPC Movement 0 Answers

Why does my object's position change when I rotate it? 0 Answers

SImple, but how do I check a game object's position in an If statement? 1 Answer


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