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 :(
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 = .....
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;
}
}
}
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.
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

Follow this Question
Related Questions
Boolean variable is never true, even if declared true or ticked in the inspector 1 Answer
Can someone help me with this camera orbiting script? 0 Answers
How to script a random asset generator> 0 Answers
How can I get all cameras enabled true false states ? 0 Answers
camera rotation if "s" is pressed 1 Answer