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 /
avatar image
0
Question by Sorade · Mar 25, 2017 at 12:32 AM · rotationcamera rotateslerp

Make camera move and look at an object over time

Hi all,

I need a script which moves a camera from the player towards an item. I want this to be achieved over time using a Lerp/Slerp.

I thought I had it but for some reason my code only works when the object is rotated 90 degrees (i.e when the up direction is the Z rather than Y).

I've been banging my head on this for a while and I really can't figure it out. I must have a misunderstanding of some of the concepts I'm trying to use. The movement seems to be fine but the rotation only works when the objects are rotated as described above (image I have a sheet of paper object which by default is stood upright with Y being the top, when the game start it falls to the ground and the Z is now facing up).

These are the two moving and rotating functions:

     void RotateTowardCam (){
         Vector3 relativePosition =  trans.position - secondaryCam.GetComponent<Transform> ().position;
         targetRotation = Quaternion.LookRotation (relativePosition, new Vector3 (0, 0, -1));
         secondaryCam.GetComponent<Transform> ().rotation = Quaternion.Slerp (secondaryCam.GetComponent<Transform> ().rotation, targetRotation, Time.deltaTime * rotateSpeed);
         //Debug.Log (trans.rotation + " " + targetRotation + " " + secondaryCam.GetComponent<Transform> ().rotation);
     }
 
     IEnumerator MoveCameraToward (Vector3 startPos)
     {
         float distCovered = 0f;
         while (trans.localPosition != secondaryCam.GetComponent<Transform> ().position * offsetFromCam && secondaryCam.GetComponent<Camera>().fieldOfView != zoomFOV) {
             distCovered += Time.deltaTime * lookAtSpeed;
             secondaryCam.GetComponent<Transform> ().position = Vector3.Lerp (startPos, trans.position + trans.forward * offsetFromCam, distCovered);
 
             secondaryCam.GetComponent<Camera> ().fieldOfView = Mathf.Lerp (secondaryCam.GetComponent<Camera> ().fieldOfView, zoomFOV, Time.deltaTime * lookAtSpeed);
             yield return new WaitForSeconds (0.0f);
         }
     }

And this is the entire class:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class LookAtOnClick : MonoBehaviour {
 
     private Rigidbody body;
     private Collider col;
     private Transform trans;
     public GameObject playerCam;
     public GameObject secondaryCam;
     public float lookAtSpeed;
     public float rotateSpeed;
     public float offsetFromCam;
     public float zoomFOV;
 
     private Quaternion targetRotation;
     private bool currentlyLooked;
 
     private void Awake()
     {
         // Assuming a simple, flat hierarchy
         body = GetComponent<Rigidbody>();
         col = GetComponent<Collider>();
     }
 
     public void Start (){
         trans = GetComponent<Transform>();
     }
 
     void Update(){
         if (currentlyLooked) {
             RotateTowardCam ();
         }
     }
 
     public void HitByRay () {
         //Debug.Log ("I was hit by a Ray");
         LookAtItem ();
     }
 
     private void LookAtItem(){
         if (Input.GetButtonDown ("Fire1")) {
             secondaryCam.SetActive (true);
             secondaryCam.GetComponent<Transform> ().position = Camera.main.transform.position;
             secondaryCam.GetComponent<Transform> ().rotation = Camera.main.transform.rotation;
             secondaryCam.GetComponent<Camera> ().fieldOfView = Camera.main.fieldOfView;
             StartCoroutine (MoveCameraToward (secondaryCam.GetComponent<Transform> ().position));
             currentlyLooked = true;
             Camera.main.enabled = false;
             EventManager.TriggerEvent ("InViewMode");
         }
     }
 
     void RotateTowardCam (){
         Vector3 relativePosition =  trans.position - secondaryCam.GetComponent<Transform> ().position;
         targetRotation = Quaternion.LookRotation (relativePosition, new Vector3 (0, 0, -1));
         secondaryCam.GetComponent<Transform> ().rotation = Quaternion.Slerp (secondaryCam.GetComponent<Transform> ().rotation, targetRotation, Time.deltaTime * rotateSpeed);
         //Debug.Log (trans.rotation + " " + targetRotation + " " + secondaryCam.GetComponent<Transform> ().rotation);
     }
 
     IEnumerator MoveCameraToward (Vector3 startPos)
     {
         float distCovered = 0f;
         while (trans.localPosition != secondaryCam.GetComponent<Transform> ().position * offsetFromCam && secondaryCam.GetComponent<Camera>().fieldOfView != zoomFOV) {
             distCovered += Time.deltaTime * lookAtSpeed;
             secondaryCam.GetComponent<Transform> ().position = Vector3.Lerp (startPos, trans.position + trans.forward * offsetFromCam, distCovered);
 
             secondaryCam.GetComponent<Camera> ().fieldOfView = Mathf.Lerp (secondaryCam.GetComponent<Camera> ().fieldOfView, zoomFOV, Time.deltaTime * lookAtSpeed);
             yield return null;
         }
     }
 }


Thank you for your help.

Comment
Add comment
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

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

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

Rotating about and only one axis 1 Answer

How to make the player rotate to where an object is being thrown? 1 Answer

Slerp/Rotational Problem Query 1 Answer

Slerp Problem?: Enemy in Constant Rotation! 1 Answer

How to get Angle float of specific axis.(Turret clamping related). 2 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