- Home /
Move to point script - bad OOP and coroutines
I am trying to make a games where you are preparing a list of orders. Like pick a apple. Then go to trash adn drop. But after first pickup my character goes wild. I think it is a problem with my movement script. Honestly I have not experience in unity scripting and that coroutine are working weird.
Like the function which i starts with StartCoroutine is working in background. I tried to change that but... My terrain is not plain. So I need to be sure that my character is not drowning or flying.
It is any sense of using coroutines? My character is performin ONE task per time. Here is pastepin link to code and code belowe itself.
Maybe someone could explain to me how replace that coroutines or show how that code should look...
using UnityEngine; using System.Collections;
[RequireComponent (typeof(CharacterController))]
public class JasioController : MonoBehaviour
{
public Animatio animatio;
public float moveSpeed, gravity, flySpeed;
private GameObject player;
private CharacterController characterController;
private float flyTempSpeed, flyTempTime;
private Vector3 direction;
private bool isFlying;
public float speed = 5.0f;
void Start ()
{
player = GameObject.FindGameObjectWithTag ("Player");
characterController = player.GetComponent<CharacterController> ();
isFlying = false;
flyTempSpeed = 0;
}
// Update is called once per frame
void Update ()
{
}
// void OnMouseDown ()
// {
// //hero.rigidbody.velocity = new Vector3 ( hero.transform.position.x, hero.transform.position.y,hero.transform.position.z+20);
// if (i == 0) {
// StartCoroutine (doMoveToPoint (GameObject.FindGameObjectWithTag ("Jablko")));
// i++;
// } else
// StartCoroutine (doMoveToPoint (GameObject.FindGameObjectWithTag ("Kompostownik")));
// }
// void OnMouseDown ()
// {
// //hero.rigidbody.velocity = new Vector3 ( hero.transform.position.x, hero.transform.position.y,hero.transform.position.z+20);
//
// StartCoroutine (doMoveToPoint (GameObject.FindGameObjectWithTag ("Respawn")));
//
// }
private void move (Vector3 targetPoint)
{
if (!isFlying) {
direction = new Vector3 (0, 0, 0);
direction += targetPoint * Time.deltaTime;
}
if (characterController.isGrounded && isFlying) {
flyTempTime = 0;
isFlying = false;
}
if (!characterController.isGrounded) {
flyTempSpeed -= flyTempTime / gravity;
flyTempTime += Time.deltaTime;
}
direction.y = flyTempSpeed * Time.deltaTime;
Vector3 ldirection = transform.TransformDirection (direction );
characterController.Move (ldirection);
}
public IEnumerator doMoveToPoint (GameObject targetObject)
{
JasioManager.Busy = true;
animatio.run ();
GameObject.FindGameObjectWithTag ("Model").transform.LookAt (targetObject.transform.position);
//player.transform.LookAt (targetObject.transform.position);
yield return StartCoroutine (MoveObject (player.transform, player.transform.position, targetObject.transform.position, 9.0f));
animatio.rest ();
JasioManager.Busy = false;
print ("Statyczna" + JasioManager.Busy);
}
IEnumerator MoveObject (Transform thisTransform, Vector3 startPos, Vector3 endPos, float time)
{
float i = 0.0f;
float rate = 1.0f / time;
while (i < 1.0f) {
i += Time.deltaTime * rate;
if (Vector3.Distance (player.transform.position, endPos) < 2) {
break;
}
if (!isFlying) {
direction = new Vector3 (0, 0, 0);
direction += (endPos - startPos) * Time.deltaTime;
}
if (characterController.isGrounded && isFlying) {
flyTempTime = 0;
isFlying = false;
}
if (!characterController.isGrounded) {
flyTempSpeed -= flyTempTime / gravity;
flyTempTime += Time.deltaTime;
}
direction.y = flyTempSpeed * Time.deltaTime;
Vector3 ldirection = transform.TransformDirection (direction);
characterController.Move (ldirection);
//Time.timeScale = 0.1f;
yield return null;
}
yield return null;
}
}
What seems to be the problem?/what are you trying to achieve? :)
could you elaborate on 'going wild' ?
Your answer
Follow this Question
Related Questions
Making a fill take exactly n seconds to complete 2 Answers
Unexpected Co-Routine Behavior 1 Answer
Coroutine For Loop Will Not End 1 Answer
Distribute terrain in zones 3 Answers