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 /
This question was closed May 03, 2013 at 07:14 PM by xandermacleod for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by xandermacleod · May 03, 2013 at 05:04 PM · rotationlookateuleranglestilt

LookAt script Issues

Hi there.

I'm currently trying to setup up a camera rail system and have stumbled into a bit of a problem. I have a path for my camera to move along, and a target for my camera - however because my camera is using a LookAt script, once the game starts playing I dont seem to have any way of controlling the local tilt (or 'roll' i should say) of the camera. I dont suppose anyone could help me alter my LookAt script so as to 'release' my camera's ability to tilt.

Ideally I would like to have a separate public float (called Tilt perhaps) which I can use to alter the local z-rotation of my camera.

Below is my script - Any help is greatly appreciated:

 using UnityEngine;
 using System.Collections;
 
 /*        
  *         This is a modified C# version of Unity's standard SmoothLookAt script.
  * 
  *         The main benefit of this script is that it allows the user to not rotate an object on all of its axis to look at a target.
  *         For example, you might want a gun turret to swivel on the spot to face a player, but to not rotate the mesh 'vertically' if the player jumps.
  *         As a result the most common configuration with this script is to make the X-axis and Z-axis true, but to leave Y-axis false.
  * 
  */
 
 
 public class LookAtAxisLock : MonoBehaviour
 {
     public bool Smooth = true;
     public float damping = 6.0f;
     
     public GameObject RotatingObject;
     public GameObject LookTarget;
     public bool X_axis = true;
     public bool Y_axis = true;
     public bool Z_axis = true;
     private float X_position;
     private float Y_position;
     private float Z_position;
     
     void Start ()
     {
         // Make the rigid body not change rotation
            if (RotatingObject.rigidbody)
         RotatingObject.rigidbody.freezeRotation = true;
     }
     
     void LateUpdate ()
     {
         
         //Update the lookTarget's current X,Y,Z locations
         if (X_axis)
         {
             X_position = LookTarget.transform.position.x;
         } else {
             X_position = RotatingObject.transform.position.x;
         }
         if (Y_axis)
         {
             Y_position = LookTarget.transform.position.y;
         } else {
             Y_position = RotatingObject.transform.position.y;
         }
         if (Z_axis)
         {
             Z_position = LookTarget.transform.position.z;
         } else {
             Z_position = RotatingObject.transform.position.z;
         }
         
         if (Smooth)
         {
             // Look at and dampen the rotation
             Vector3 relativePosition = (new Vector3(X_position, Y_position, Z_position) - RotatingObject.transform.position); // NB: For the Quaternion to work a vector must be created from the 'origin' (kindof)
             Quaternion rotation = Quaternion.LookRotation(relativePosition);
             RotatingObject.transform.rotation = Quaternion.Slerp(RotatingObject.transform.rotation, rotation, Time.deltaTime * damping);
         }
         else
         {
             // Just lookat
             RotatingObject.transform.LookAt(new Vector3(X_position, Y_position, Z_position));
         }
Comment
Add comment · Show 2
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 robertbu · May 03, 2013 at 07:08 PM 0
Share

One simple solutions is to place the script above on an empty game object at the same position and rotation as the camera, make the camera a child of that empty game object, then use Transform.localRotation to 'roll' the camera.

avatar image xandermacleod · May 03, 2013 at 07:13 PM 0
Share

Wow! But of course! Awww jeez.... its at times like this I totally feel like doing an 'epic palm to my face'. Cheers bud.

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

12 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

Related Questions

lookAt euler angles 3 Answers

Trying to have object spin within player and always face "up" relative to camera [example pics of what I'm trying to achieve in post]] 0 Answers

How do I make transform.Lookat use Vector3.down instead of Vector3.forward? 1 Answer

LookAt on only 1 axis for 2D games? 1 Answer

Quaternion Equivalent of restricting an axes rotation to zero 3 Answers


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