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 DevMerlin · Jun 03, 2013 at 12:12 PM · androidcameratouchcoroutinebuttons

How can I fix this Coroutine Camera?

I've built a very simple coroutine with a emulated axis for my camera to move for a 2D-based game, however while this works, it's not entirely what I want. It should fire until the main buttons are released, which are emulated axis in order to allow touch screen control. It only fires once, then waits for another button press.

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour {
     
     public Transform _target;
     
     Vector3 firstTouchPosistion;
     
     private float horizontalControl;
     
     private bool moving = false;
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
 
 
         
 
         
     }
     
     
     public void axisUpdate(string dir)
     {
         switch(dir)
         {
             case "left":
                 horizontalControl = -1;
                 break;
             case "right":
                 horizontalControl = 1;
                 break;
             case "release":
                 horizontalControl = 0;
             break;
         }
         
         
         
         if (!moving) {
             StartCoroutine("moveCamera");
         }
     }
     
     IEnumerator moveCamera()
     {
         moving = true;
         
         float i = 0;
         
         Vector3 camPosistion = transform.position;
         
         Vector3 movePosistion = new Vector3(horizontalControl, 0, 0);
             
         Vector3 destination = camPosistion + movePosistion;
         
         while (i < 1)
         {
             i += Time.deltaTime * 5;
             Debug.Log(i);
             transform.position = Vector3.Lerp(camPosistion, destination, i);
             yield return 0;
         }
         
         moving = false;
     }
 }
 
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 SubatomicHero · Jun 03, 2013 at 02:52 PM 0
Share

I'm sorry but I dont understand what exactly you are asking.

avatar image DevMerlin · Jun 03, 2013 at 02:59 PM 0
Share

The coroutine -should- repeat until the variable horizontalControl changes. However, it only fires once. The button itself fires twice, once when it's pressed which changes horizontalControl to the proper direction, and once when it's released changing it back to zero.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by robertbu · Jun 03, 2013 at 03:54 PM

Even with your comment, I have to guess a bit on what you want to do. If I'm right, I'd remove the coroutine and do something like:

 using UnityEngine;
 using System.Collections;
  
 public class CameraController : MonoBehaviour {
  
     public Transform _target;
     Vector3 firstTouchPosistion;
     private float horizontalControl;
     private Vector3 destination;    
     private float speed = 0.5f;
  
     void Start () {
          destination = transform.position;
     }
  
     // Update is called once per frame
     void Update () {
          transform.position = Vector3.MoveTowards(transform.position, destination, speed * Time.deltaTime);
     }
  
     public void axisUpdate(string dir)
     {
         switch(dir)
         {
          case "left":
           horizontalControl = -1.0f;
           break;
          case "right":
           horizontalControl = 1.0f;
           break;
          case "release":
           horizontalControl = 0.0f;
          break;
         }
         
         destination = transform.position + new Vector3(horizontalControl, 0.0f, 0.0f);
     }
 }
Comment
Add comment · Show 2 · Share
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 DevMerlin · Jun 03, 2013 at 04:04 PM 0
Share

NICE! That works and produces FAR smoother results in camera movement then IEnumerator. Still a bit jerky, but far better then transform.position += or transform.Translate() on Android. I think I can ignore this now, aside from telling it to occasionally point at the character and follow them ins$$anonymous$$d of under player control.

Basically, I've been trying to solve the issue of a jerky 2D-limited camera on the Android platform that seems to be occuring for almost everyone.

avatar image robertbu · Jun 03, 2013 at 05:46 PM 0
Share

If you replace Vector3.$$anonymous$$oveTowards() with Vector3.Lerp() you will get an eased movement. You might also try changing Update() to LateUpdate().

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

15 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

Related Questions

Control the camera with a half of the touch screen 0 Answers

Button isn't working 1 Answer

How can I rotate a camera useing 2 touches? 0 Answers

Move to touch position 1 Answer

Dragging Camera based on Touch 0 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