• 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 /
avatar image
Question by Drummermonkey01 · Sep 29, 2015 at 01:59 PM · 3d

When a button is Pressed how will I get the camera to move to another Position?

Hello I really could use some help with this:

I have a button which is attached to a game object and is interacted within the real world space. I also have a camera that moves around a game object. How would I once the button is pressed get the camera to move to a specific Position or to the button that has been pressed. (just to say this is being developed for android)

I really could use some help with this, Thank you!

Comment

People who like this

0 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Dr-Nick · Sep 29, 2015 at 04:17 PM

There are countless ways for you to do this but hopefully these instructions will give you a better idea of what you need to do. For my solution I wrote a script with two main functions which move a transform to a new position. One function uses a parameters specified on the script (vector position and a bool to determine if we're moving in world space or local space) and the other moves the transform to the same world position of another transform.

 using UnityEngine;
 using System.Collections;
 
 public class TransformMover : MonoBehaviour 
 {
     public Transform targetTransform;
     public Vector3 position;
     public Vector3 rotation;
     public bool useWorldSpace = true;
     public float tweenDuration = 1.0f;
 
 
     private Coroutine _currentCoroutine = null;
 
 
 
     public void MoveToPosition()
     {
         if (targetTransform != null)
         {
             if (useWorldSpace)
             {
                 targetTransform.position = position;
             }
             else
             {
                 targetTransform.localPosition = position;
             }
         }
     }
 
     public void MoveToRotation()
     {
         if (targetTransform != null)
         {
             if (useWorldSpace)
             {
                 targetTransform.rotation = Quaternion.Euler(rotation);
             }
             else
             {
                 targetTransform.localRotation = Quaternion.Euler(rotation);
             }
         }
     }
 
 
     public void TweenToRotation()
     {
         if (_currentCoroutine != null)
         {
             StopCoroutine(_currentCoroutine);
             _currentCoroutine = null;
         }
         
         if (gameObject.activeInHierarchy && targetTransform != null)
         {
             Quaternion targetRotation = Quaternion.Euler(rotation);
             _currentCoroutine = StartCoroutine(TweenRotate(tweenDuration,targetRotation,useWorldSpace));
         }
     }
 
 
 
     public void MoveToTransformPosition(Transform target)
     {
         if (targetTransform != null && target != null)
         {
             targetTransform.position = target.position;
         }
     }
 
     public void MoveToTranformOrientation(Transform target)
     {
         if (targetTransform != null && target != null)
         {
             targetTransform.position = target.position;
             targetTransform.rotation = target.rotation;
         }
     }
 
     public void TweenToTranformOrientation(Transform target)
     {
         if (_currentCoroutine != null)
         {
             StopCoroutine(_currentCoroutine);
             _currentCoroutine = null;
         }
 
         if (gameObject.activeInHierarchy && targetTransform != null && target != null)
         {
             _currentCoroutine = StartCoroutine(TweenMoveRotate(tweenDuration,target));
         }
     }
 
     public void TweenToTranformPosition(Transform target)
     {
         if (_currentCoroutine != null)
         {
             StopCoroutine(_currentCoroutine);
             _currentCoroutine = null;
         }
         
         if (gameObject.activeInHierarchy && targetTransform != null && target != null)
         {
             _currentCoroutine = StartCoroutine(TweenMove(tweenDuration,target));
         }
     }
 
     public void TweenToTranformRotation(Transform target)
     {
         if (_currentCoroutine != null)
         {
             StopCoroutine(_currentCoroutine);
             _currentCoroutine = null;
         }
         
         if (gameObject.activeInHierarchy && targetTransform != null && target != null)
         {
             _currentCoroutine = StartCoroutine(TweenRotate(tweenDuration,target));
         }
     }
 
     public void TweenToRotation(Transform target)
     {
         if (_currentCoroutine != null)
         {
             StopCoroutine(_currentCoroutine);
             _currentCoroutine = null;
         }
         
         if (gameObject.activeInHierarchy && targetTransform != null && target != null)
         {
             _currentCoroutine = StartCoroutine(TweenMove(tweenDuration,target));
         }
     }
 
 
     private IEnumerator TweenRotate(float duration, Quaternion targetRotation, bool worldSpace)
     {
         Quaternion startRotation = worldSpace ? targetTransform.rotation : targetTransform.localRotation;
         
         float t = 0;
         float timeElapsed = 0;
         
         while (timeElapsed < duration)
         {
             timeElapsed += Time.deltaTime;
             t = timeElapsed/duration;
 
             if (worldSpace)
             {
                 targetTransform.rotation = Quaternion.Lerp(startRotation, targetRotation,t);
             }
             else
             {
                 targetTransform.localRotation = Quaternion.Lerp(startRotation, targetRotation,t);
             }
             yield return null;
         }
 
         if (worldSpace)
         {
             targetTransform.rotation = targetRotation;
         }
         else
         {
             targetTransform.localRotation = targetRotation;
         }
     }
 
 
 
 
     private IEnumerator TweenMoveRotate(float duration, Transform target)
     {
         Vector3 startPosition = targetTransform.position;
         Quaternion startRotation = targetTransform.rotation;
 
         float t = 0;
         float timeElapsed = 0;
 
         while (timeElapsed < duration)
         {
             timeElapsed += Time.deltaTime;
             t = timeElapsed/duration;
 
             targetTransform.position = Vector3.Lerp(startPosition, target.position,t);
             targetTransform.rotation = Quaternion.Lerp(startRotation, target.rotation,t);
             yield return null;
         }
         targetTransform.position = target.position;
         targetTransform.rotation = target.rotation;
 
 
 
     }
 
     private IEnumerator TweenMove(float duration, Transform target)
     {
         Vector3 startPosition = targetTransform.position;
         
         float t = 0;
         float timeElapsed = 0;
         
         while (timeElapsed < duration)
         {
             timeElapsed += Time.deltaTime;
             t = timeElapsed/duration;
             
             targetTransform.position = Vector3.Lerp(startPosition, target.position,t);
             yield return null;
         }
         targetTransform.position = target.position;
     }
 
     private IEnumerator TweenRotate(float duration, Transform target)
     {
         Quaternion startRotation = targetTransform.rotation;
         
         float t = 0;
         float timeElapsed = 0;
         
         while (timeElapsed < duration)
         {
             timeElapsed += Time.deltaTime;
             t = timeElapsed/duration;
             
             targetTransform.rotation = Quaternion.Lerp(startRotation, target.rotation,t);
             yield return null;
         }
         targetTransform.rotation = target.rotation;
     }
 
 }

You can attach this script component to any object in the scene but it's best you attach it to your button. On your button, set the click event to reference the object that contains the TransformMover component and select one of the two functions MoveToPosition or MoveToTransformPosition. Your button inspector should look something like this. alt text

This script also has a few tween methods to different positions and rotations via values specified in the inspector or you supply a transform to tween to. You could also use animation to move your objects and have the animation play on the button press. Again, this isn't the only way to implement this feature. Ultimately the design of your program should determine how you go about moving objects on clicking a button.


screen-shot-2015-09-29-at-45928-pm.png (42.8 kB)
Comment

People who like this

0 Show 10 · 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 Drummermonkey01 · Sep 30, 2015 at 12:23 PM 0
Share

Thank you for the Code and for the useful information it has really helped me!

However I can not get it to work, I have done everything you have said but when I press the button the button moves not the Camera, I think it might be to do with my Camera, instead of a transform I have a Rect Transform. I think it is changing the behaviour slightly.

But Thank you for your help!

avatar image Dr-Nick · Sep 30, 2015 at 02:19 PM 0
Share

Hi Drummermonkey01. I'm assuming you're using the transform method. I've just check the above code and noticed it was using the transform of the component it is attached to (which was why it was moving the button for you). I've updated the code above so the transform method moves the targetTransform instead. See if that resolves your issue.

avatar image Drummermonkey01 Dr-Nick · Oct 05, 2015 at 05:30 PM 0
Share

Just to ask how do I effect the rotation and the speed in which the camera moves? So that My camera, when it moves rotates as well but also I can control the speed of both the movement and the rotation?

avatar image Dr-Nick Drummermonkey01 · Oct 06, 2015 at 04:43 PM 0
Share

I've added new functions to the above method to adjust the rotation and position a transform to match another transform over a period of time.

Show more comments
avatar image Dr-Nick · Oct 12, 2015 at 04:23 PM 0
Share

Hi Drummermonkey Your last question doesn't quite make sense. Are you asking if can set the position and rotation set independently from each other? Meaning you could tween the rotation in 1 second and the position in 2 seconds. If you want to the above, the best way to do it with my solution is have two different TransformMovers on the same object each with a different tweenDuration. On the button, you call a tween position from one TransformMover and the rotation tween with the other. Anything more complicated that that, I'd recommend you learn more about the Unity animation system to move a camera around the scene.

avatar image Drummermonkey01 Dr-Nick · Oct 14, 2015 at 07:49 AM 0
Share

As in just a position and rotation coordinates within the inspector and the speed of which the object get there. So I press a button and my camera moves and rotates (depending on what coordinates I put in the inspector)

Do you know what I mean? I can elaborate more if you wish

avatar image Dr-Nick Dr-Nick · Oct 14, 2015 at 05:43 PM 0
Share

I've updated the above method with rotation methods to tween to with a rotation vector modifiable in the inspector. I'm not going to make any additional changes to the above script as I believe it more adequately answers the question. Hope this puts you on the right track Drummermonkey01. Good luck with your project.

avatar image Dr-Nick Dr-Nick · Oct 15, 2015 at 09:43 AM 0
Share

If you feel adventurous, you can add a curve parameter in the code and when you tween a value, you use the value from the curve. If you were to modify the existing code, you could add these properties to the top of the code

 public AnimationCurve _curve; //The curve smooth the movement
 public bool _useCurve = false; //If true- move as the curve, false linear movement

Then in one of the tween methods do something like this. This example is position movement.

 if (_useCurve)
 {
        targetTransform.position = Vector3.Lerp(startPosition, target.position,_curve.Evaluate(t));
 }
 else
 {
     targetTransform.position = Vector3.Lerp(startPosition, target.position,t);
 }




Show more comments
avatar image

Answer by Amoldadu · Apr 08, 2017 at 04:04 AM

@ Dr-Nick how can I play animation of main camera on the button press? Please help me...

Comment

People who like this

0 Show 0 · 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

Answer by maxvp · Jun 06, 2017 at 01:28 PM

fantastic script - very powerful thanks Dr-Nick by using tween to transform I could add a target that teh camera tweens to

Comment

People who like this

0 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

30 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

Related Questions

How to "blackout" a room in 3d game ? 0 Answers

Is it possible to see a render texture through another render texture? 1 Answer

Getting more specific with pickups 0 Answers

Will unity3d run on Intel atom,Can I run unity3d on Intel atom z8350 and 2gb ram 0 Answers

SOS my game is broken 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