Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Jan 25, 2017 at 12:07 PM by Dobryakov for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Dobryakov · Jan 25, 2017 at 01:04 PM · c#cameracamera-lookcamera movement

Smooth camera movement and lookAt [solved]

Got stucked with camera movement in my Unity3D C# project.

What i have:

some objects on the scene a camera, which would fly from any object's poition or from it's own current position What i need:

smooth rotare camera to one of the object's origin fly to the spot near the object (there is an empty, so i fly to empty's coords) Algorithm: rotare to the object's origin -> when rotation finishes, start to fly to the empty's position. while flying, look at object's origin.

The problem is that it's not smooth, the camera "jumps" at the end of the movement.

My code: c#

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class testMove : MonoBehaviour
 {
     public GameObject startObj;
     public GameObject endObj;
     public float speed = 1.0F;
     private float startTime;
     private float journeyLength;
     private string endObjName;
     private GameObject endObjLookAt;
     void Start()
     {
         startTime = Time.time;
         if (startObj)
         {
         }
         else
         {
             startObj = this.gameObject;
         }
         journeyLength = Vector3.Distance(startObj.transform.position, endObj.transform.position);
         endObjName = endObj.name;
         endObjLookAt = GameObject.Find(endObjName + "LookAt");
     }
 
     void Update()
     {
         if (endObj)
         {
             float distCovered = (Time.time - startTime) * speed;
             float fracJourney = distCovered / journeyLength;
             tweenLook(endObjLookAt, fracJourney);
             float angle = Quaternion.Angle(transform.rotation, Quaternion.LookRotation(endObjLookAt.transform.position - transform.position));
             if (angle <= 0.0001)
             {
                 Debug.Log("rotation finished");
                 tweenPos(startObj, endObj, fracJourney);
                 transform.LookAt(endObjLookAt.transform.position);
             }
         }
     }
 
     private void tweenPos(GameObject startObj, GameObject endObj, float fracJourney)
     {
         Vector3 newposition = Vector3.Lerp(startObj.transform.position, endObj.transform.position, fracJourney);
         transform.position = newposition;
     }
 
     private void tweenLook(GameObject endObjLookAt, float fracJourney)
     {
         Quaternion newrotation = Quaternion.LookRotation(endObjLookAt.transform.position - transform.position);
         transform.rotation = Quaternion.Slerp(transform.rotation, newrotation, fracJourney);
     }
 }

Many thanks to all of you, guys in advance! My c# code (attached to the camera):

Comment
Add comment · Show 1
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 Dobryakov · Jan 25, 2017 at 12:06 PM 0
Share

Solved!

public class test$$anonymous$$ove : $$anonymous$$onoBehaviour { public Transform startObj; public Transform endObj; private Transform endObjLookAt;

 public float rotationDuration;
 public AnimationCurve rotationCurve;

 public float movementDuration;
 public AnimationCurve movementCurve;

 private IEnumerator moveAndRotateCameraIEnumerator;

 void Start()
 {
     // If you want to do it on start just call $$anonymous$$oveAndRotateCamera() here, else call if from anywhere you want (a script, a game button, ...)
 }

 void Update()
 {
     if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
     {
         $$anonymous$$oveAndRotateCamera();
     }
 }

 public void $$anonymous$$oveAndRotateCamera(Transform startTransform = null, Transform endTransform = null)
 {
     if(startTransform)
     {
         startObj = startTransform;
     }
     else
     {
         startObj = this.transform;
     }

     if(endTransform)
     {
         endObj = endTransform;
     }
     endObjLookAt = GameObject.Find(endObj.name + "LookAt").transform;

     if(moveAndRotateCameraIEnumerator != null)
     {
         StopCoroutine(moveAndRotateCameraIEnumerator);
     }
     moveAndRotateCameraIEnumerator = $$anonymous$$oveAndRotateCameraCoroutine();
     StartCoroutine(moveAndRotateCameraIEnumerator);
 }

 private IEnumerator $$anonymous$$oveAndRotateCameraCoroutine()
 {
     //ROTATION
     Vector3 startEulerAngles = transform.eulerAngles;
     transform.LookAt(endObjLookAt);
     Vector3 deltaEulerAngles = new Vector3($$anonymous$$athf.DeltaAngle(startEulerAngles.x, transform.eulerAngles.x), $$anonymous$$athf.DeltaAngle(startEulerAngles.y, transform.eulerAngles.y), $$anonymous$$athf.DeltaAngle(startEulerAngles.z, transform.eulerAngles.z));

     Debug.Log("Starting rotation...");
     float timer = 0.0f;
     while(timer < rotationDuration)
     {
         timer += Time.deltaTime;
         transform.eulerAngles = startEulerAngles + deltaEulerAngles * rotationCurve.Evaluate(timer / rotationDuration);
         yield return new WaitForEndOfFrame();
     }
     transform.eulerAngles = startEulerAngles + deltaEulerAngles;
     Debug.Log("Rotation done!");
     //----

     //$$anonymous$$OVE$$anonymous$$ENT
     Vector3 startPosition = transform.position;

     Debug.Log("Starting movement...");
     timer = 0.0f;
     while(timer < movementDuration)
     {
         timer += Time.deltaTime;
         transform.position = Vector3.Lerp(startPosition, endObj.position, movementCurve.Evaluate(timer / movementDuration));
         transform.LookAt(endObjLookAt);
         yield return new WaitForEndOfFrame();
     }
     transform.position = endObj.position;
     transform.LookAt(endObjLookAt);
     Debug.Log("$$anonymous$$ovement done!");
     //----
 }

}

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

7 People are following this question.

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

Related Questions

3d camera not working as I want it to 0 Answers

Sit Down First person 0 Answers

Maximum Camera Angle Rotation 0 Answers

Third person look around camera, jagged results 0 Answers

Make camera follow and look at object at the same time 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