I recently started learning to code in C# for Unity; in the following script trying to enable and disable an object (enemy) based on the position of the mouse.
The problem is the code works fine in enabling the object, but I can't figure out how to disable it once it's been activated, so that the object appears and disappears as the mouse moves back and forth - in and out of range. Please let me know if you have a solution. Thank you!
using UnityEngine; using System.Collections;
public class Paddle : MonoBehaviour {
public GameObject enemy;
// Use this for initialization
void Start () {
enemy.SetActive(false);
}
// Update is called once per frame
void Update () {
Vector3 paddlePos = new Vector3 (8f,this.transform.position.y,0f);
float mousePosInBlocks = Input.mousePosition.x / Screen.width * 16;
paddlePos.x = Mathf.Clamp(mousePosInBlocks, 6f, 8f);
this.transform.position = paddlePos;
if (mousePosInBlocks < 6f) {
print ("1");
} else if(mousePosInBlocks <= 6.5f) {
print("2");
enemy.SetActive(true);
} else if(mousePosInBlocks <= 7.5f) {
print("3");
} else {
print("4");
}
}
}
Aucun commentaire:
Enregistrer un commentaire