- Home /
Trigger Camera Rotation Issue
What I'm trying to do is to rotate the camera to a desire rotation when hitting a trigger with camera adjust script. The camera is facing 0 degrees, and the trigger tells its to rotate to angle 270. The problem is that when I use vector.smoothdamp, it will rotate around the longest path around (270 deg) instead of rotating -90 degrees to get to the rotation quicker.
Also I clamped the camera y rotation from 0 to 360, going to 0 if greater than 360 and vice versa, if this helps with this question.
Answer by Gruffy · Nov 23, 2013 at 11:35 PM
sorry mate , i`m not entirely sure i understand so let me try to clarify it. Are you saying you want to move a camera through a spherical interpolation from one transform point to another ?.. If so, here is an improvement on the Transform.LookAt(method provided by UnityEngine)
This method uses the same premise as what ive written below but isnt cheaper than this implementation.
there are other optimizations you could make to the script, like taking out Update created types and cache them in Start , then update the cached type instead during Update instead of making a new cachable type. I havent because this script is simply carrying out a purpose.
You MUST CHANGE THE VOID UPDATE FUNCTION TO READ...."void OnTriggerEnter(Collider col)" this will enable collision trigger to occur through script and obviously add a collider to the object you want to look at (check "isTrigger" in inspector) and a name the camera too (Unity`s preset tag is Main Camera, i suggest keeping that). here is the script applicable to what i`ve said above.
using UnityEngine;
/// <summary>
/// Attaching this script to an object will make that object face the specified target.
/// The most ideal use for this script is to attach it to the camera and make the camera look at its target.
/// </summary>
public class LookAtTarget : MonoBehaviour
{
public Transform target;
public float speed = 0.1f;
Transform mTrans;
void Start ()
{
//Cachethe transform
mTrans = transform;
}
void LateUpdate ()
{
if (target != null)
{
//declare and assign dir vector
Vector3 dir = target.position - mTrans.position;
//cache theh dir vector magnitude
float mag = dir.magnitude;
//if we move out of the dir the vector is facing, turn towards it
if (mag > 0.001f)
{
Quaternion lookRot = Quaternion.LookRotation(dir);
mTrans.rotation = Quaternion.Slerp(mTrans.rotation, lookRot, Mathf.Clamp01(speed * Time.deltaTime));
}
}
}
// void OnTriggerStay (Collider col)
// {
// if(col.gameObject.tag == "MainCamera")
// {
// if (target != null)
// {
// Vector3 dir = target.position - mTrans.position;
// float mag = dir.magnitude;
//
// if (mag > 0.001f)
// {
// Quaternion lookRot = Quaternion.LookRotation(dir);
// mTrans.rotation = Quaternion.Slerp(mTrans.rotation, lookRot, Mathf.Clamp01(speed * Time.deltaTime));
// }
// }
// }
// }
}
Below here is another script i quickly made to literally rotate towards the object of choice , specified again through inspector.
attach this script to the camera and then go down the road of changing Update to OnTriggerStay( to execute the script inside a trigger colllider)
using UnityEngine;
using System.Collections;
public class FromToRot : MonoBehaviour
{
public Transform from; //set this as your camera in inspector
public Transform to; //set this to gameobject/ transform you want to look at
public float speed = 0.1f; //speed multiplier (bigger number = faster rotation)
void Update()
{
//rotates camera (or whatever object you assign to the "from" transform cache in inspector)
transform.rotation = Quaternion.Slerp(from.rotation, to.rotation, Time.time * speed);
}
//i have commented out this as you will most likley want to see the script in action before you apply it to a trigger
//however, the code below is what you ll need , so here it is
// void OnTriggerStay (Collider col)
// {
// //rotates camera (or whatever object you assign to the "from" transform cache in inspector)
// transform.rotation = Quaternion.Slerp(from.rotation, to.rotation, Time.time * speed);
// }
}
//Gruffy 2013
Let me know, if you are unclear on any of what ive given you and, indeed, if this answers your question
Take Care . Gruffy
Yes, the quaternion.slerp might help. But I forgot to mention that this is an orbiting camera script that is controllable by mouse and can only rotate with this variable vector3 camControl. Could I just change camControl to a Quaternion variable?
hey $$anonymous$$alblytron, sry, been away.. back now... so orbiting camera eh!? with that in $$anonymous$$d, yes quaternions are indeed the way to go but another approach would be to create a flight system for your camera and get it to translate and rotate about a set of waypoints. The camera would aim to rest at each waypoint`s position, whilst targeting your player. I think you need to take a jaunt over to unify wiki Unify
This is a most excellent place to start what you are after.
I will be around tomorrow morning and can help you then, im a bit tied up for now (i have to get some sleep at some point tonight im afraid :( ) Check out that link and look at a few of the scripts there, you get lucky or pleasantly surprised :) Like I said though, can give a bit more help tomorrow morning U$$anonymous$$ time :) Gruffy
Your answer
Follow this Question
Related Questions
Problem with the rotation of camera in third person 1 Answer
Camera movement causing UI Pointer Enter/Exit triggers to break? 0 Answers
Rotation seems to pass through condition checks 1 Answer
How to make my player look in the direction of its movement? 3 Answers
How do I set a child object to not rotate if the parent WILL be rotating? 1 Answer