The question is answered, right answer was accepted
[SOLVED] Gameobject wont move towards target with MoveTowards
Hello there!
[SOLVED]: Set Time.TimeScale at the end of the Spacelevel to 0, so that the Player can appreciate the text that gets displayed. Didn't think the timeScale would persist between levels, but it did. Thanks to all of you guys, for helping me keep the motivation up to find the issue!
So, I got a small galaxy scene setup, where I instantiate the players spaceship and on a rightclick, it moves towards the position of the mouse, until it is there.
For that, i use the following script in the gameobject:
void Update(){
if (Input.GetMouseButtonDown(1)) {
Camera mainCam = FindObjectOfType<Camera>();
target = mainCam.ScreenToWorldPoint(Input.mousePosition);
target.z = -1f;
}
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
}
Which works fine. However, once I am at a galaxy system, I can enter the fight space there (which works fine to) and after that, I have a button pop in, which reloads the space scene.
Alas, once I got back into the space scene, the ship wont move. Everything in the setup seems to work. It correctly detects the player location, it detects where the mouse is and sets the target accordingly. However, my gameobject doesn't move anymore, even though, according to everything I see, it should.
Also, changing the value of float speed doesn't change anything either.
I am really stumped on this one. Please rush to my aid, fellow Scripters.
If I can provide any useful intel, just tell me.
Best Regards
EDIT: More Info. Using unity personal 5.3.1f1 and Application.LoadLevel. Also , there is a GUI Element which I move via rigidbody2D and add velocity. Strangely, this doesn't work after loading, either. On printing the MoveTowards, it return always the transform.position of the player, even if the target is different.
If all scripts with $$anonymous$$oveTowards not working may be a Unity bug.Report to Unity.you can use Vector3.Lerp ins$$anonymous$$d and check if its working.
Tried that, doesn't work either. Also, that doesn't explain the non-functioning of the GUI velocity, as that works with AddForce()...
Are you applying your scripts after the level loads? If your scripts are working but then stop working after the scene loads that seems to me like your not applying your scripts or something. Are they part of your prefab that you instantiate on scene load or are the scripts applied to already static objects part of the scene?
Hey wesleywh,
thanks for the help! The scripts are part of instances. Also, the scripts are working. Example: I got that button which adds force to a menu on the side, therefore sliding it in. After pressing play, that works. The fightscreen works to, then the level back to the galaxy map where said slider is, which has worked right after pressing play. If I print the force, it gets added to the rigidbody, but it doesn't move one bit.
Best Regards