- Home /
Answer by OneCept-Games · Jan 03, 2018 at 10:43 PM
Use the Lerp functions around the Camera rotation Y axis. Or use a Tween asset, like DOTween.
Answer by Carson365 · Jan 03, 2018 at 10:46 PM
I'm new to coding so I'm not sure how to do that. Could you please give me a little bit of code to get started? Thanks.
// Save the original rotation
Quaternion OrgRotation = Camera.main.transform.rotation;
Quaternion rotation = new Quaternion(x,y,z,w);
// Do your stuff here
Camera.main.transform.rotation = Quaternion.Lerp(OrgRotation, rotation, 0.5f);
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class SettingsSign : $$anonymous$$onoBehaviour {
public Camera cam;
Vector3 orgRotation = new Vector3 (6.395f, -89.965f, 0.0f);
void Start () {
cam = GetComponent<Camera>();
}
void Update () {
}
void On$$anonymous$$ouseDown () {
orgRotation = camera.transform.rotation.eulerAngles;
Quaternion rotation = orgRotation;
cam.transform.rotation = Quaternion.Lerp (orgRotation, Quaternion.Euler (rotation), 0.5f);
}
}
I'm getting errors. What is wrong?
Sorry, I edited it. The point is to make a new Quaternion you can rotate to. I just pasted x,y,z,w, but you can build this up in various depending on where you want to turn your camera.
ugh sorry. I don't quite understand still since I am a beginner. Anymore information you can tell me? Thanks.
ok. Try pasting this in:
// Save the original rotation
Quaternion OrgRotation = Camera.main.transform.rotation;
Quaternion rotation = new Quaternion(OrgRotation.x,OrgRotation.y+180,OrgRotation.z,OrgRotation.w);
// Do your stuff here
Camera.main.transform.rotation = Quaternion.Lerp(OrgRotation, rotation, 0.5f);
WHY does paste code not always work!!! Grrrr!
So I got it to work, but there is a problem Is there a way to make it, I guess smoothly rotate to that position. Where the camera actually rotates to it and doesn't automatically go to the new position? I'm not sure if this code is suppose to do that or not, but just wondering how to do it. Thanks
try use more time to rotate, like 2 seconds.
You can build a much more advanced Camera rotation. This is a very simple one. $$anonymous$$aybe download DOTween asset and try some of the Camera tweeting methods.
Also play with Slerp()
Hmm. If I make it 2 seconds (where you put 0.5f), it still just jumps to place. Is it suppose to be rotating to that rotation or just jump there?
Let's try another approach:
// $$anonymous$$aximum turn rate in degrees per second.
public float turningRate = 30f;
// Rotation we should blend towards.
private Quaternion _targetRotation = Quaternion.identity;
// Call this when you want to turn the object smoothly.
public void SetBlendedEulerAngles(Vector3 angles)
{
_targetRotation = Quaternion.Euler(angles);
}
private void Update()
{
// Turn towards our target rotation.
transform.rotation = Quaternion.RotateTowards(transform.rotation, _targetRotation, turningRate * Time.deltaTime);
}
Attach the above functions to your Camera script, and call SetBlendedEularAngles when ever you want to rotate your Camera in any direction.
Your answer
Follow this Question
Related Questions
Rotate Camera on axis and X axis not working 1 Answer
Movement with camera and rotation of objects 1 Answer
Logical Camera errors... 1 Answer
Distribute terrain in zones 3 Answers
Smooth Move script for my spaceship 0 Answers