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);
}
}
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.
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;
}
}
Glad you like it. Let me know if you encounter any problems! ;-)
@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?
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
Follow this Question
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