I'm fairly new at Unity and i'm trying to making a game.
I want to have an subtitle fading in when you're at the end of the game. This start when you hit a button.
But when I code a image that I fadein, it plays it directly when you start the game.
Do you guys know a solution?
#pragma strict
private var guiShow : boolean = false;
var car : GameObject;
var rayLength = 10;
var guiObject : GUITexture;
var fadeTime = 1.0;
enum Fade {In, Out}
var fadesubtitles : boolean = false;
function Update ()
{
var hit : RaycastHit;
var fwd = transform.TransformDirection(Vector3.forward);
if(Physics.Raycast(transform.position, fwd, hit, rayLength))
{
if(hit.collider.gameObject.tag == "car")
{
guiShow = true;
if(Input.GetKeyDown("e"))
{
guiShow = false;
}
else if(Input.GetKeyDown("e"))
{
guiShow = false;
}
}
}
else
{
guiShow = false;
}
}
function OnGUI()
{
if(guiShow == true)
{
GUI.Box(Rect(Screen.width / 2, Screen.height / 2, 150, 25), "Press F to escape");
if(Input.GetKeyDown("f")){
fadesubtitles = true;
}
}
}
if (fadesubtitles == true){
yield FadeGUITexture(guiObject, fadeTime, Fade.In);
yield WaitForSeconds(3.0);
yield FadeGUITexture(guiObject, fadeTime, Fade.Out);
}
function FadeGUITexture (guiObject : GUITexture, timer : float, fadeType : Fade) {
if (subtitles == true){
var start = fadeType == Fade.In? 0.0 : 1.0;
var end = fadeType == Fade.In? 1.0 : 0.0;
var i = 0.0;
var step = 1.0/timer;
while (i < 1.0) {
i += step * Time.deltaTime;
guiObject.color.a = Mathf.Lerp(start, end, i)*.5;
yield;
}
}
}
I'd start your game object in the 'disabled' state (uncheck it in the inspector). Then at then end of the game, have some code that enables it.
You can use iTween.
FadeFrom(GameObject target, Hashtable args)
Example:
iTween.FadeFrom(gameObject, iTween.Hash("alpha", 0f, "amount", 1f, "time", 2f));
Related
My game is a 2D top down platform, I have an zombie enemy that moves up and down the stage, I can place obstacles (such as a create) in its way to stop the enemy, the enemy has the ability to dodge these obstacles at any random moment. If the enemy collided with the create, the enemy will stop its movement to break the create. Once the create is broken, the enemy will continue to moving up, hit point B and then will move back down the stage. My Problem: If the enemy were to dodge left (for example) and then continues to move down and passes another create, the enemy will ignore collision and the function for it to stop and break that create. How do I fix this issue?
public bool hitCollider = false;
public bool hitCollider2 = false;
public bool canMove = true;
public bool moveDown = false;
public bool isMovingUp = true;
public bool isMovingDown = false;
public bool isSomethere = false;
public bool ifMoving;
public float velocidadMax;
public float xMax;
public float xMin;
public float delay;
public float x;
private float tiempo;
public float countDown, countUp, startTime;
void Start () {
Bcol = GetComponent<BoxCollider2D> ();
Bcol.isTrigger = true;
isMovingUp = true;
rigid = GetComponent <Rigidbody2D> ();
velocidadMax = 0.12f;
x = velocidadMax;
InvokeRepeating ("turnFalse", startTime, countDown); // changing lanes
InvokeRepeating ("turnTrue", 0, countUp); // moving up or down
ifMoving = true;
movedownSpeed = -4f;
moveupSpeed = 4f;
hascollidedwHouse = false;
}
void Update () {
if (moveDown == false) {
rigid.velocity = new Vector2 (0, moveupSpeed); // moving up
if (ifMoving == true) {
//irrelevant - don't need to know code here!
} else if (ifMoving == false) {
movedownSpeed = -4;
moveupSpeed = 0;
Move ();
}
}
if (moveDown == true) {
if (hitMascot2 == true) {
} else if (hitMascot2 == false) {
movingDown = true;
Bcol.isTrigger = true;
StartCoroutine (waitMove ());
StartCoroutine (destroyObj ());
}
}
if (isMovingDown == true) {
Debug.Log ("MovingDown");
Bcol.isTrigger = true;
if (canMove == false) {
Debug.Log ("is False" + canMove);
moveupSpeed = 4f;
}
hitCollider = false;
hitCollider2 = false;
}
if (hitCollider == true) {
StartCoroutine (wait ());
Debug.Log ("I HAVE COLLIDED");
if (canMove == true) {
movedownSpeed = 0f;
}
if (canMove == false) {
movedownSpeed = -4f;
}
moveupSpeed = 0f;
}
if (hitCollider2 == true) {
StartCoroutine (wait2 ());
Debug.Log ("I HAVE COLLIDED");
if (canMove == true) {
movedownSpeed = 0f;
}
if (canMove == false) {
movedownSpeed = -4f;
}
moveupSpeed = 0f;
}
}
void OnTriggerEnter2D (Collider2D col) {
if (col.tag == "objects") {
hitCollider = true;
woodBox = true;
canMove = false;
moveDown = false;
xMax = 0;
xMin = 0;
if (hascollidedwHouse == true) {
moveDown = false;
isMovingDown = false;
movedownSpeed = 0;
moveupSpeed = 0;
hitCollider = false;
ifMoving = false;
xMax = 0;
xMin = 0;
StartCoroutine (waitDown ());
}
}
if (col.tag == "objects2") {
hitCollider2 = true;
metalBox = true;
canMove = false;
hitwoodenC = false;
hitmetalC = true;
moveDown = false;
xMax = 0;
xMin = 0;
if (hascollidedwHouse == true) {
moveDown = false;
isMovingDown = false;
movedownSpeed = 0;
moveupSpeed = 0;
ifMoving = false;
hitCollider2 = false;
xMax = 0;
xMin = 0;
StartCoroutine (waitDown2 ());
}
}
if (col.tag == "pointB") {
if (ifMoving == false) {
moveupSpeed = 0;
movedownSpeed = -4;
} else if (ifMoving == true) {
moveupSpeed = 0;
movedownSpeed = -4;
}
Debug.Log ("Collided");
moveDown = true;
isMovingDown = true;
isMovingUp = false;
canMove = false;
hascollidedwHouse = true;
}
void Move () {
if (isSomethere == false) {
if (isMovingUp == true) {
tiempo += Time.deltaTime;
if (transform.localPosition.x < xMin) {
x = Random.Range (0.0f, velocidadMax);
x = velocidadMax;
tiempo = 0.0f;
Debug.Log ("Left");
}
if (tiempo > 1.0f) { //1.0 0.3
x = -velocidadMax;
tiempo = 0.0f;
Debug.Log ("Right");
}
transform.localPosition = new Vector3 (transform.localPosition.x + x, transform.localPosition.y);
}
} else if (isSomethere == true) {
movedownSpeed = -4;
moveupSpeed = 4;
if (iSPdeath == true) {
movedownSpeed = 0;
moveupSpeed = 0;
}
}
}
void turnFalse ()
{
if( hitCollider == false) {
ifMoving = false; // moves left and right
}
if( hitCollider2 == false) {
ifMoving = false; // moves left and right
}
}
void turnTrue() {
if( hitCollider == false) {
ifMoving = true; // moves up and down
}
if( hitCollider2 == false) {
ifMoving = true; // moves up and down
}
}
IEnumerator wait() {
yield return new WaitForSeconds (2.5f);
hitCollider = false;
if (isMovingUp == true) {
canMove = true;
}
if (isMovingUp == false) {
canMove = false;
moveupSpeed = 0f;
movedownSpeed = -4f;
}
if (canMove == true) {
moveupSpeed = 4f;
}
}
IEnumerator wait2() {
yield return new WaitForSeconds (5.2f);
hitCollider = false;
if (isMovingUp == true) {
canMove = true;
}
if (isMovingUp == false) {
canMove = false;
moveupSpeed = 0f;
movedownSpeed = -4f;
}
if (canMove == true) {
moveupSpeed = 4f;
}
}
IEnumerator waitDown() {
yield return new WaitForSeconds (2.5f);
movedownSpeed = -4f;
moveupSpeed = 0f;
moveDown = true;
}
IEnumerator waitDown2() {
yield return new WaitForSeconds (5.2f);
movedownSpeed = -4f;
moveupSpeed = 0f;
moveDown = true;
}
}
I am making a shooter game in Unity. I wanted to delay the shooting script but now my code doesn't seem to be working. Any clues to how I might solve this? Other than the delay function everything worked fine.
Here is the code:
#pragma strict
private var player : GameObject;
public var speed : float;
private var bulletCounter : int;
var reloadtime : float = 2;
private var reloadTimer: float = 0.0;
function Start () {
player = this.gameObject;
}
function Update () {
if (reloadTimer > 0){
reloadTimer -= Time.deltaTime;
if (reloadTimer <= 0){
if(Input.GetKey("space")) {
Shoot();
}
}
if(Input.GetKey("w")){
if(player.transform.position.y < 20) {
player.transform.position.y += speed * Time.deltaTime;
}
}
if(Input.GetKey("s")){
if(player.transform.position.y > -20) {
player.transform.position.y -= speed * Time.deltaTime;
}
}
if(Input.GetKey("a")){
if(player.transform.position.x > -20) {
player.transform.position.x -= speed * Time.deltaTime;
}
}
if(Input.GetKey("d")){
if(player.transform.position.x < 20) {
player.transform.position.x += speed * Time.deltaTime;
}
}
}
function Shoot () {
bulletCounter++;
var bullet = GameObject.CreatePrimitive(PrimitiveType.Cube);
bullet.transform.position = player.transform.position;
bullet.AddComponent.<BulletScript>();
bullet.name = "Bullet"+bulletCounter.ToString();
var audio : AudioSource = GetComponent.<AudioSource>();
audio.Play();
reloadTimer = reloadtime;
}
}
You need to get movement code(W,S,A,D) outside the timer condition -
#pragma strict
private var player : GameObject;
public var speed : float;
private var bulletCounter : int;
var reloadtime : float = 2;
private var reloadTimer: float = 0.0;
function Start () {
player = this.gameObject;
}
function Update () {
if (reloadTimer > 0){
reloadTimer -= Time.deltaTime;
if (reloadTimer <= 0){
if(Input.GetKey("space")) {
Shoot();
}
}
}
/********* Move this outside the timer condition *********/
if(Input.GetKey("w")){
if(player.transform.position.y < 20) {
player.transform.position.y += speed * Time.deltaTime;
}
}
if(Input.GetKey("s")){
if(player.transform.position.y > -20) {
player.transform.position.y -= speed * Time.deltaTime;
}
}
if(Input.GetKey("a")){
if(player.transform.position.x > -20) {
player.transform.position.x -= speed * Time.deltaTime;
}
}
if(Input.GetKey("d")){
if(player.transform.position.x < 20) {
player.transform.position.x += speed * Time.deltaTime;
}
}
function Shoot () {
bulletCounter++;
var bullet = GameObject.CreatePrimitive(PrimitiveType.Cube);
bullet.transform.position = player.transform.position;
bullet.AddComponent.<BulletScript>();
bullet.name = "Bullet"+bulletCounter.ToString();
var audio : AudioSource = GetComponent.<AudioSource>();
audio.Play();
reloadTimer = reloadtime;
}
}
This will keep the movement enabled during wait period(reloadtime) after player shoots.
I think your idea is not so clear. Easiest way to do it would be.
function Shoot () {
...
reloadTimer = Time.time + reloadtime;
}
And if should look like this
if (reloadTimer >= Time.time){
if(Input.GetKey("space")) {
Shoot();
}
}
Currently building a 2D game, and have created a javascript code for the jump for my character 'Ezio' but it does not do anything when i press 'space' for it to jump. There are no errors with the code either.
#pragma strict
var jump :float = 0;
var jumpspeed : float = 15;
var jumptimer :float = 0;
function Start () {
}
function Update () {
if (jump == 1) {
jumptimer = jumptimer +1;
if (jumptimer >= 50) {
jumptimer = 0;
jump = 0;
}
}
}
if (Input.GetKeyDown ("space"))
{
if (jump == 0) {
rigidbody2D.velocity.y = jumpspeed;
jump = 1;
}
}
Any suggestions on what could be the issue?
Try this:
#pragma strict
var jump :float = 0;
var jumpspeed : float = 15;
var jumptimer :float = 0;
function Start () {
}
function Update() {
if (Input.GetKeyDown("space")) {
if (jump == 1) {
jumptimer = jumptimer + 1;
if (jumptimer >= 50) {
jumptimer = 0;
jump = 0;
}
} else {
rigidbody2D.velocity.y = jumpspeed;
jump = 1;
}
}
}
function OnTriggerEnter(col : Collider){
if(col.tag == "Player")
{
player.transform.position = SpawnPoint.position;
audio.PlayOneShot(Sound);
VioSign.enabled = true;
if(pauseEnabled == false){
pauseEnabled = true;
AudioListener.volume = 1;
Time.timeScale = 0;
Screen.showCursor = true;
}
}
}
This is my respawnpoint script .
#pragma strict
var wheelFL : WheelCollider;
var wheelFR : WheelCollider;
var wheelRL : WheelCollider;
var wheelRR : WheelCollider;
var maxTorque : float = 50;
function Start(){
rigidbody.centerOfMass.y = -0.9;
}
function FixedUpdate () {
wheelRR.motorTorque = maxTorque * Input.GetAxis("Vertical");
wheelRL.motorTorque = maxTorque * Input.GetAxis("Vertical");
wheelFL.steerAngle = 20 * Input.GetAxis("Horizontal");
wheelFR.steerAngle = 20 * Input.GetAxis("Horizontal");
}
Here is my Car Control Script I want to stop the car after respawning again to the respawn point . I'm having a difficult time to solve it. please help me :)
If you just want to stop the car's current movement, you can simply set its velocity to zero:
function OnTriggerEnter(col : Collider){
if(col.tag == "Player")
{
player.transform.position = SpawnPoint.position;
player.gameObject.rigidBody.velocity = Vector3.zero;
audio.PlayOneShot(Sound);
VioSign.enabled = true;
if(pauseEnabled == false){
pauseEnabled = true;
AudioListener.volume = 1;
Time.timeScale = 0;
Screen.showCursor = true;
}
}
}
i am making a game in which i am using is-clicked function when i click the object the letter written on it displayed now i want that when i clicked the same object again the word disappear... now how can i do that?
#pragma strict
static var nextPos = 200;
var word: String;
var sel: String;
var isClicked : boolean=false;
var xpos: float = 200;
function OnMouseDown()
{
if (!isClicked) {
isClicked = true;
xpos = nextPos;
nextPos += 8;
}
}
function OnGUI()
{
if (gameObject.name == "Sphere(Clone)" && isClicked )
{
GUI.Label(new Rect(xpos,260,400,100), "A");
}
else if (gameObject.name == "Sphere 1(Clone)" && isClicked )
{
GUI.Label(new Rect(xpos,260,400,100), "B");
}
else if (gameObject.name == "Sphere 2(Clone)" && isClicked )
{
GUI.Label(new Rect(xpos,260,400,100), "C");
}
else if (gameObject.name == "Sphere 3(Clone)" && isClicked )
{
GUI.Label(new Rect(xpos,260,400,100), "D");
}
}
write in OnMouseDown
else if(isClicked)
{
isClicked = false;
// do your xpos stuff here
}