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 omatase · Aug 23, 2015 at 05:18 AM · rotationcamera rotatetranslationvector3.lerpquaternion.slerp

Help with translating and rotating a camera

I've built a simple 3D scene and put some 2D sprites in the scene. I have a camera that overlooks all 8 sprites. There are 4 close to the camera and 4 far away in the distance. I'm trying to programmatically manipulate the camera as the player interacts with the game. I have spent about 4 hours on this and have learned a lot about it but I am still unable to get it to work correctly. I would appreciate some help.

Here are the two things I want the camera to be able to do:

  1. When one of the four characters in the foreground are clicked I want the camera to rotate towards the character and zoom to a certain distance away from the selected character.

  2. I want to at some point zoom the camera in on the 4 characters in the distance.

Both of these things need to be interpolated so that the camera repositions itself over maybe 500 ms.

I can quite easily make the camera look at the selected character with this code:

 Camera.main.transform.LookAt(transform.position);

but this doesn't result in the camera being animated to the new "look at" rotation. The other thing I can't figure out is how to translate the camera along a line that runs both through it and a target object.

Here is a screenshot of the scene. for reference.

alt text

Appreciate any help!

camera-work.jpg (227.8 kB)
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

2 Replies

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

Answer by omatase · Aug 25, 2015 at 03:20 AM

I got it!

 Camera.main.GetComponent<CameraScript>().Rotate(transform.position - Camera.main.transform.position);
 Camera.main.GetComponent<CameraScript>().Reposition(transform.position + (transform.forward * -20));

and

 using UnityEngine;
 using System.Collections;
 
 public class CameraScript : MonoBehaviour {
 
     private Vector3? _targetPosition;
     private Vector3? _targetRotation;
     private float _damping = 5.0f;
 
     // Use this for initialization
     void Start () {
         _targetPosition = null;
         _targetRotation = null;
     }
     
     // Update is called once per frame
     void Update () {
     
         if (_targetRotation != null)
         {
             var newRotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(_targetRotation.Value), _damping * Time.deltaTime);
 
             if (newRotation == this.transform.rotation)
             {
                 _targetRotation = null;
             }
             else
             {
                 this.transform.rotation = newRotation;
             }
         }
 
         if (_targetPosition != null)
         {
             var newPosition = Vector3.MoveTowards(transform.position, _targetPosition.Value, 50 * Time.deltaTime);
 
             if (newPosition == transform.position)
             {
                 _targetPosition = null;
             }
             else
             {
                 transform.position = newPosition;
             }
         }
     }
 
     public void Reposition(Vector3 newPosition)
     {
         _targetPosition = newPosition;
     }
 
     public void Rotate(Vector3 newRotation)
     {
         _targetRotation = newRotation;
     }
 
 }
 
Comment
Add comment · 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
0

Answer by Foulcloud · Aug 24, 2015 at 10:02 PM

I think what you are after are tweening functions. These allow you to move from one point to another over time and specify the curve/path. I highly recommend HOTween. There is a free version for download and you can create tweens on objects with one line of code.

Comment
Add comment · Show 1 · 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 omatase · Aug 25, 2015 at 03:19 AM 0
Share

I'm using HOTween as well in other places, but the animation is really just a small part of what I need. I've been working on it for a few hours more and have found the complete answer to the problem. I'm going to post it below.

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

26 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

Related Questions

How do you make a Tps cam with RotateAround around the X and Y axis without rotating on the Z axis? 1 Answer

Roll a ball camera problem? 1 Answer

how to rotate bullet towards target direction?unity2D 0 Answers

Combining meshes (different materials) together for rotation/translation 1 Answer

Aligning a transform according to two different positions and directions 1 Answer


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