Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by Kalbytron · Nov 22, 2013 at 11:32 PM · camerarotationtrigger

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.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 2 · 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 Kalbytron · Nov 24, 2013 at 08:17 AM 0
Share

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?

avatar image Gruffy · Nov 26, 2013 at 01:34 AM 0
Share

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

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.

Follow this Question

Answers Answers and Comments

17 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

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


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