Hi, I want to move 3 different objects around the scene, after some time an object would be choosen and that object would receive a message to tell them to move, unfortunatly I am not being able to make this. These are my scripts:
The script that chooses objects: using UnityEngine;using System.Collections;public class PlanetsScript : Mo - Pastebin.com
And the script of the object that shall move: using UnityEngine;using System.Collections;public class Planet : MonoBehav - Pastebin.com
Sorry I can’t post the code here, my unity answers is extremely bugged(I also can’t visit my profile). the function on the object that receives the function is being triggered, but it’s not moving. Can anyone help me fix this?
Thanks in advance. (God, I can’t put tags, this is hella bugged, I mean I can write there, but it doesn’t show the tag “label”)
Hi MadJonny,
It’s not moving because nothing in Update() like functions, so movement it’s applied like for one frame only.
Try something like this:
using UnityEngine;
using System.Collections;
public class Planet : MonoBehaviour {
public GameObject target;
public float speed = 1;
Vector3 oPosition;
public bool moving = false;
void Start () {
oPosition = transform.position;
}
void Update() {
if(moving) {
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, step);
if (transform.position == target.transform.position) {
Debug.Log ("trajectory done");
transform.position = oPosition;
moving = false;
}
}
}
void Move () {
moving = true;
Debug.Log ("Moving " + gameObject.name);
}
}
(sorry for unformatted code, yes, looks unity answers is bugged today)
By the time you answered I had already figgured it out :P, thanks anyway, I can’t reply to you and actually my code is almost the same as yours ahah, anyway, I can’t accept your answer, the button isn’t there, I’m probably doing something that I shouldn’t by replying to you via answer… anyway it’s not my fault.