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.

1 Answer

1

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 (Unitys preset tag is Main Camera, i suggest keeping that). here is the script applicable to what ive 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?