我在MainCamera里写了一个屏震功能,由一个bool参数触发,代码如下
public class CameraMove : MonoBehaviour {
GameObject MainCharacter;
float ShakeTime = 0.2f;
float ShakeAmount = 0.1f;
public bool Shake;
Vector2 ShakeVector;
void Start () {
MainCharacter = GameObject.Find ("Chief");
Shake = false;
}
void Update () {
if (Shake) {
ShakeVector.x = ShakeAmount * (Random.Range (0, 2) - 0.5f) * 2;
ShakeVector.y = ShakeAmount * (Random.Range (0, 2) - 0.5f) * 2;
ShakeTime -= Time.deltaTime;
}
if (ShakeTime <= 0) {
ShakeVector.x = 0;
ShakeVector.y = 0;
ShakeTime = 0.2f;
Shake = false;
}
transform.position = new Vector3 (MainCharacter.transform.position.x + ShakeVector.x,MainCharacter.transform.position.y + ShakeVector.y + 3 , transform.position.z);
}
}
另一个脚本代码如下
public class AttackCollider : MonoBehaviour {
Animator CharacterAnimator;
GameObject gameobject;
void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.tag == "Enemy") {
gameObject.GetComponent<CameraMove> ().Shake = true;
}
}
void Start () {
gameobject = GameObject.Find ("Main Camera");
}
我在这个脚本中给它赋值,但总是显示object reference not set to an instance of an object
明明mono都识别出来脚本参数了,求帮助!!!
public class CameraMove : MonoBehaviour {
GameObject MainCharacter;
float ShakeTime = 0.2f;
float ShakeAmount = 0.1f;
public bool Shake;
Vector2 ShakeVector;
void Start () {
MainCharacter = GameObject.Find ("Chief");
Shake = false;
}
void Update () {
if (Shake) {
ShakeVector.x = ShakeAmount * (Random.Range (0, 2) - 0.5f) * 2;
ShakeVector.y = ShakeAmount * (Random.Range (0, 2) - 0.5f) * 2;
ShakeTime -= Time.deltaTime;
}
if (ShakeTime <= 0) {
ShakeVector.x = 0;
ShakeVector.y = 0;
ShakeTime = 0.2f;
Shake = false;
}
transform.position = new Vector3 (MainCharacter.transform.position.x + ShakeVector.x,MainCharacter.transform.position.y + ShakeVector.y + 3 , transform.position.z);
}
}
另一个脚本代码如下
public class AttackCollider : MonoBehaviour {
Animator CharacterAnimator;
GameObject gameobject;
void OnTriggerEnter2D(Collider2D other) {
if (other.gameObject.tag == "Enemy") {
gameObject.GetComponent<CameraMove> ().Shake = true;
}
}
void Start () {
gameobject = GameObject.Find ("Main Camera");
}
我在这个脚本中给它赋值,但总是显示object reference not set to an instance of an object
明明mono都识别出来脚本参数了,求帮助!!!