- Home /
Help in solving bug in my code :)
Ive wrote a simple code for example,im clicking at the spot with this code and transfrom cube is moving to spot, it works, but with a bug: when im clicking again at another spot2, while cube is moving to spot1,cube stops.Think problem is in logic of a code. Thanks for any help.
using UnityEngine; using System.Collections;
public class PlanetMoveTest : MonoBehaviour
{
public Transform Cube;
public float speed = 10f;
bool isMoving = false;
// Use this for initialization
void Start ()
{
}
void OnMouseDown()
{
isMoving = true;
}
// Update is called once per frame
void Update ()
{
if(isMoving)
{
Cube.transform.position = Vector3.MoveTowards(Cube.transform.position,transform.position,Time.deltaTime * speed);
}
if(Cube.transform.position == transform.position)
{
isMoving = false;
}
}
}
There are a number of issues and puzzles here, and I'm not sure which (if any) account for the behavior you describe. First On$$anonymous$$ouseDown() will only be called if you click on the object you have this script attached to. So if you are clicking on anything other than the object this script is attached to, this script is not being called.
Or you might have this script attached to multiple objects (likely from your description) and expect it to move to the next object when a new object is clicked? Then you will have multiple objects fighting to make the cube move with the $$anonymous$$oveTowards(). That is both scripts will attempt to move the cube each frame. The fight might result in a stalemate and thus no movement. Note typically an object moves itself rather than being moved from outside. If the object moves itself, there would be no fighting, just a reassignment of the destination.
What i think could be the problem is that it never is in the exact position as the one it is moving towards. I had similar problems a year ago and the solution is not to have it the exact same position.
Don't think so. Put a Debug.Log() statement at line 35. I'm pretty sure it will fire as long as you don't click on a second object.
Answer by robertbu · May 10, 2013 at 01:51 AM
As mentioned above, I'd restructure the code so that the follower moved itself. I know nothing about the nature of your game, but here are a couple of simple scripts as proof of concept. The first goes on the follower and it must be named "Follower" (or you can change the name in the code):
public class Follower : MonoBehaviour {
public Vector3 v3Dest;
public float speed = 5.0f;
void Start () {
v3Dest = transform.position;
}
void Update () {
transform.position = Vector3.MoveTowards(transform.position,v3Dest,Time.deltaTime * speed);
transform.LookAt(v3Dest);
}
}
And this script goes on any leaders (i.e. the things you click on to change the destination):
public class Leaders : MonoBehaviour {
private Follower follower;
void Start () {
follower = GameObject.Find ("Follower").GetComponent< Follower >();
}
void OnMouseDown() {
follower.v3Dest = transform.position;
}
}
I think logic of your code is ok, but it gave me two errors,this when im starting game : NullReferenceException at follower = GameObject.Find ("Follower").GetComponent< Follower >(); and second when im clicking at object,NullReferenceException: Object reference not set to an instance of an object at follower.v3Dest = transform.position;
Forgot to rename my object to "Follower" on the scene; Problem solved.Question may be deleted.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Ride across a tree? 1 Answer
Trying to make gameobject follow mouse cursor. It disappears instead. 2 Answers
Line Renderer Positioning Problem 1 Answer