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