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!
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.
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.
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, ins$$anonymous$$d of a transform I have a Rect Transform. I think it is changing the behaviour slightly.
But Thank you for your help!
Hi Drummermonkey01. I'm assu$$anonymous$$g 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 ins$$anonymous$$d. See if that resolves your issue.
Just to ask how do I effect the rotation and the speed in which the camera moves? So that $$anonymous$$y camera, when it moves rotates as well but also I can control the speed of both the movement and the rotation?
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.
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? $$anonymous$$eaning 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 Transform$$anonymous$$overs on the same object each with a different tweenDuration. On the button, you call a tween position from one Transform$$anonymous$$over 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.
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
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.
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);
}
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...
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