Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nemeczek · May 09, 2014 at 03:11 PM · c#coroutine

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...

http://pastebin.com/LXWE7PFg

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;
         }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Nemeczek · May 12, 2014 at 05:35 PM 0
Share
 Any1?
avatar image snarf · May 12, 2014 at 05:50 PM 0
Share

What seems to be the problem?/what are you trying to achieve? :)

could you elaborate on 'going wild' ?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a fill take exactly n seconds to complete 2 Answers

Unexpected Co-Routine Behavior 1 Answer

Why is my Mathf.Lerp not lasting for the intended duration, even though I'm not using Time.time as the t variable? 1 Answer

Coroutine For Loop Will Not End 1 Answer

Distribute terrain in zones 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges