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 WantToMakeASimpleGame · Dec 28, 2014 at 10:51 AM · c#cameracamera-movementpoint-a-to-b

Camera smooth movement from ponint A to B

Hello, I have a problem with my code. I want to make smooth movement from PointA to PointB (comments in the code shows which is which) and I don't know how to do this.

using UnityEngine; using System.Collections;

public class CameraMovement : MonoBehaviour {

 public Transform projectile; //followed object
 public Transform farLeft; //camera border marker
 public Transform farRight; //camera border marker



 void Start () {
 Vector3 StartPosition = transform.position;
 }

 //In Update Camera is following object from point A (StartPosition) to point B.
 void Update () {

                 Vector3 newPosition = transform.position;
                 newPosition.x = projectile.position.x;
                 newPosition.x = Mathf.Clamp (newPosition.x, farLeft.position.x, farRight.position.x);
                 transform.position = newPosition;
 //It stops on farRight point
 }

 //Function used from other script
 public void CameraChanger(){

 //I want this function to slowly and smoothly take my camera 
 //from point B (where we are now) to point A (StartPosition)
 //But I don't know how to do this

 }
Comment
Add comment · Show 5
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 Scribe · Dec 28, 2014 at 11:38 AM 0
Share

make 'StartPosition' a global variable as at the moment it is only accessable from Start (move it to the top with your public Transform variables). The you can use Vector3.Lerp or Vector3.$$anonymous$$oveTowards to move from transform.position to StartPosition

avatar image WantToMakeASimpleGame · Dec 28, 2014 at 09:45 PM 0
Share

It doesn't work properly :/

It works but all unity freezes, console too and then, when transform.position reaches StartPoint (I know it happens because i have Debug.Log) everything get back to normal, unity unfreees.

public class Camera$$anonymous$$ovement : $$anonymous$$onoBehaviour {

 public Transform projectile; //followed object
 public Transform farLeft; //camera border marker
 public Transform farRight; //camera border marker
 private float speed = 0.001f;
 private float lerp$$anonymous$$oving = 0.0f;
 private Vector3 StartPosition;


 void Start () {
 Vector3 StartPosition = transform.position;
 }

 //In Update Camera is following object from point A (StartPosition) to point B.
 void Update () {

                 Vector3 newPosition = transform.position;
                 newPosition.x = projectile.position.x;
                 newPosition.x = $$anonymous$$athf.Clamp (newPosition.x, farLeft.position.x, farRight.position.x);
                 transform.position = newPosition;
 //It stops on farRight point
 }

 //Function used from other script
 public void CameraChanger(){
 while (transform.position != StartPosition) {

     lerp$$anonymous$$oving += Time.deltaTime; 
     transform.position = Vector3.$$anonymous$$oveTowards (transform.position, StartPosition, speed*lerp$$anonymous$$oving);
     Debug.Log ("Backtrack: " + transform.position);
 }

Debug works well, but unity just freezes

avatar image Scribe · Dec 28, 2014 at 10:05 PM 0
Share

The reason is that your while loop does not allow your frame to progress until it has finished e.g. when transform.position == StartPosition, so nothing will happen, and then you will see everything happen at once once the while has finished. You should use a coroutine so that you can see the progress at each stage of the while loop. Another problem you might find with your current code is that you should ~never~ really compare floating point numbers and hence you should not directly compare to Vector3's as you are doing with (transform.position != StartPosition) I think a better approach might be:

 public IEnumerator CameraChanger(){
     float progress = 0;
     while (progress <= 1) {
         transform.position = Vector3.Lerp(transform.position, StartPosition, progress);
         progress += speed*Time.deltaTime; 
         Debug.Log ("Backtrack: " + transform.position);
         yield return null;
     }
     transform.position = StartPosition;
 }


And you should call it as:

 StartCoroutine(CameraChanger());

Hope that helps!

Scribe

avatar image WantToMakeASimpleGame · Dec 28, 2014 at 10:15 PM 0
Share

How to call Coroutine from other script? With normal functions it looks like:

 Camera$$anonymous$$oveScript.CameraChanger();

but how should it looks like with Coroutine?

avatar image WantToMakeASimpleGame · Dec 28, 2014 at 10:21 PM 0
Share

got it by

     public void CallCameraChanger() {
         StartCoroutine(CameraChanger());
     }


It works! Awesome!! Thank you very much!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Enable the OrbitCam script how can I make it will continue from the current camera view and position ? 0 Answers

Issue with my camera controls 0 Answers

Pan RTS camera controller without changing elevation/zoom 0 Answers

Regarding transform.position in the roll a ball tutorial 1 Answer

Camera not being moved when dragged by mouse 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