• 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 /
  • Help Room /
avatar image
0
Question by Oshigawa · May 19, 2016 at 01:54 PM · camerascripting problemlocalposition

Referencing MainCamera localPosition

Hello,

i've found a simple script for shaking the camera, here's the code, nothing complex:

 using UnityEngine;
 using System.Collections;
 
 public class CameraShake : MonoBehaviour
 {
     // Transform of the camera to shake. Grabs the gameObject's transform
     // if null.
     public Transform camTransform;
     
     // How long the object should shake for.
     public float shakeDuration = 0f;
     
     // Amplitude of the shake. A larger value shakes the camera harder.
     public float shakeAmount = 0.7f;
     public float shakeAmountDecrease = 1.0f;
     public float decreaseFactor = 1.0f;
     
     Vector3 originalPos;
     
     void Awake()
     {
         if (camTransform == null)
         {
             camTransform = GetComponent(typeof(Transform)) as Transform;
         }
     }
     
     void OnEnable()
     {
         originalPos = camTransform.localPosition;
     }
 
     void Update()
     {
         if (shakeDuration > 0)
         {
             camTransform.localPosition = originalPos + Random.insideUnitSphere * shakeAmount;
             
             shakeDuration -= Time.deltaTime * decreaseFactor;
         }
         else
         {
             shakeDuration = 0f;
             camTransform.localPosition = originalPos;
         }
         
         if (shakeAmount > 0)
         {
         
         shakeAmount -= Time.deltaTime * shakeAmountDecrease;
         
         }    
         
         else
         {
             
         shakeAmount = 0f;
         camTransform.localPosition = originalPos;
         }
         
     }
 }
 

Since it will be activated on enemy despawn, i replaced the awake() method with the usual find game object/get component stuff, so basically, my camTransform is now camera:

 GameObject MainCamera = GameObject.Find("MainCamera");
 Camera camera = MainCamera.GetComponent<Camera>();

BUT

something happens. Unity asks me if i made a backup and changes my script. Everywhere there was camera, now's GetComponent().

Now it looks like this, and it doesn't work

 using UnityEngine;
 using System.Collections;
 
 public class CameraShake2 : MonoBehaviour
 {
     // Transform of the camera to shake. Grabs the gameObject's transform
     // if null.
 
     // How long the object should shake for.
     public float shakeDuration = 0f;
     public float decreaseFactor = 1.0f;
     
     // Amplitude of the shake. A larger value shakes the camera harder.
     public float shakeAmount = 0.7f;
     public float shakeAmountDecrease = 1.0f;
     
     
     Vector3 originalPos;
     
     void Awake()
     
     {
         GameObject MainCamera = GameObject.Find("MainCamera");
         Camera camera = MainCamera.GetComponent<Camera>();
     }
             
     void OnEnable()
     
     {
         originalPos = GetComponent<Camera>().localPosition;
     }
 
     void Update()
     
     {    
     if (shakeDuration > 0)
     
     {
             GetComponent<Camera>().localPosition = originalPos + Random.insideUnitSphere * shakeAmount;
             
             shakeDuration -= Time.deltaTime * decreaseFactor;
         }
         else
         {
             shakeDuration = 0f;
             GetComponent<Camera>().localPosition = originalPos;
         }
         
         if (shakeAmount > 0)
         {
         
         shakeAmount -= Time.deltaTime * shakeAmountDecrease;
         
         }    
         
         else
         {
             
         shakeAmount = 0f;
         GetComponent<Camera>().localPosition = originalPos;
         }
         
     }
 }
 

The error i get is the following: Type UnityEngine.Camera' does not contain a definition for localPosition' and no extension method localPosition' of type UnityEngine.Camera' could be found (are you missing a using directive or an assembly reference?)

I don't get it :(

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
1
Best Answer

Answer by $$anonymous$$ · May 19, 2016 at 02:56 PM

You need the transform that comes with the camera, now you are using the camera component itself which is not a transform. Use Camera.main.transform to get the transform of the main camera of your project or just use Camera.main.transform.localPosition = .....

Comment
Add comment · Show 4 · 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 Oshigawa · May 20, 2016 at 04:46 AM 0
Share

Hello Roland,

i will try it out, thank You.

avatar image Oshigawa Oshigawa · May 20, 2016 at 06:45 AM 0
Share

Works like a charm, here's the whole script if anyone needs it

 using UnityEngine;
 using System.Collections;
 
 public class CameraShake2 : $$anonymous$$onoBehaviour
 {
     // Transform of the camera to shake. Grabs the gameObject's transform
     // if null.
 
     // How long the object should shake for.
     public float shakeDuration = 0f;
     public float decreaseFactor = 1.0f;
     
     // Amplitude of the shake. A larger value shakes the camera harder.
     public float shakeAmount = 0.7f;
     public float shakeAmountDecrease = 1.0f;
     
     
     Vector3 originalPos;
                     
     void OnEnable()
     
     {
         originalPos = Camera.main.transform.localPosition;
     }
 
     void Update()
     
     {    
     if (shakeDuration > 0)
     
     {
         Camera.main.transform.localPosition = originalPos + Random.insideUnitSphere * shakeAmount;
             
         shakeDuration -= Time.deltaTime * decreaseFactor;
     }
     
     else
     
     
     {
         shakeDuration = 0f;
         Camera.main.transform.localPosition = originalPos;
     }
         
     if (shakeAmount > 0)
         
     {
         shakeAmount -= Time.deltaTime * shakeAmountDecrease;
     }    
         
         else
         {
             
         shakeAmount = 0f;
         Camera.main.transform.localPosition = originalPos;
         }
         
     }
 }
 
avatar image NoseKills · May 20, 2016 at 04:28 PM 0
Share

I believe you were originally using the variable GameObject.camera in your script. It was removed in 5.3.5 so Unity wanted to modify your scripts to avoid compilation errors.

Right now the things you do in Awake() are pointless. You could just delete the whole method. You make a local variable 'camera' that you don't use for anything and it doesn't exist outside the scope of that method. Perhaps you thought you were using it before when infact you were referencing GameObjec.camera.

avatar image Oshigawa · May 21, 2016 at 05:52 AM 0
Share

Hello Nose$$anonymous$$ills,

yes, i've seen in the log that there's no use for the Awake() part of the script, thanks for pointing that out, kinda missed it in a hurry. I edited the post and deleted it.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

68 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 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 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 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

Camera Switch error 0 Answers

Why are my keys being registered multiple times? 1 Answer

Switching Cameras, Movement not working. 1 Answer

How to rotate a bone along with the camera 0 Answers

Camera self rotation script problem 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