Español (ES)
Usuario anónimo
Iniciar sesión | Regístrate gratis
Foro del juego
  • Trucos y Guías

Responder / Comentar
Foro Unity
Por Tonics19831
Hace 6 años / Respuestas: 0 / Lecturas: 136

Unity: Ayuda con este codigo: raycast que abre puerta y la cierra;

Buenas. 
La cosa es que cuando abro una puerta y no la cierro , la siguiente puerta al abrirla no lo hace bien... 
no se donde esta el fallo. Este es mi codigo. Si abro una puerta y luego abri la otra ya no se cierra.


using UnityEngine;
using System.Collections;

public class AbrirCerrarPuerta : MonoBehaviour {



public Texture2D m_useTexture;
public float RayLength = 1f;


private bool m_UseReticle=true;



private Rect m_crosshairRect;
private Ray playerAim;
private Camera playerCam;


    private Vector3 hitinicial;
    private bool entrar=false;
    private bool isOpen;
    private RaycastHit hit1;
    private RaycastHit hit;
    private bool declarar=true;
    private bool _isLerping;
    private float _timeStartedLerping;
    private Vector3 _startPosition;
    private Vector3 _endPosition;
    public float distanceToMove = 10;
    public float timeTakenDuringLerp = 1f;
    private Vector3 _finalPosition;
    void StartLerping()
    {
        _isLerping = true;
        _timeStartedLerping = Time.time;

        hitinicial= hit1.transform.eulerAngles;
        //We set the start position to the current position, and the finish to 10 spaces in the 'forward' direction
        _startPosition = hit1.transform.eulerAngles;
        _finalPosition = new Vector3(_startPosition.x, _startPosition.y + distanceToMove, _startPosition.z);
        _endPosition = hit1.transform.eulerAngles + _finalPosition ;
      
    }

    void StartLerping1()
    {
        _isLerping = false;
        _timeStartedLerping = Time.time;


        //We set the start position to the current position, and the finish to 10 spaces in the 'forward' direction
         _startPosition= new Vector3(_startPosition.x, _startPosition.y , _startPosition.z);;
        _finalPosition = hitinicial;
        _endPosition = hit1.transform.eulerAngles - _finalPosition;


    }


    void  Update (){
		playerCam = Camera.main;
		Ray playerAim = playerCam.GetComponent().ViewportPointToRay(new Vector3(0.5f, 0.5f, 0));
		
	
		if (Physics.Raycast (playerAim, out hit, RayLength))
			{
				
				if(hit.collider.gameObject.tag == "Puerta")
				{

                

              
                    hit1 = hit;
                
                if (Input.GetKeyDown(KeyCode.F) && !isOpen)
                {

                    isOpen = true;
                    
                    entrar = true;
                    StartLerping();
                    

                }else if (Input.GetKeyDown(KeyCode.F) && isOpen)
                {
                    isOpen = false;
                    
                    entrar = true;
                    StartLerping1();

                }
                 m_UseReticle = true;
				}
			}else{
					
					m_UseReticle = false;		
			}
		


		
	}


   

   



    void  Awake (){
	    
		
	    if(m_UseReticle){
		  m_crosshairRect = new Rect((Screen.width - m_useTexture.width) / 2, 
								(Screen.height - m_useTexture.height) / 2, 
								m_useTexture.width, 
								m_useTexture.height);
	    }
	}
 
	void  OnGUI (){
		
		  if(m_UseReticle){
            //GUI.Label(new Rect(Screen.width / 2 - 75, Screen.height - 100, 150, 30), "Press 'F' to open the door");
            GUI.DrawTexture(m_crosshairRect, m_useTexture);
		 }
	}





    void FixedUpdate()
    {



        if (entrar)
        {

            if (_isLerping&&entrar)
            {
                //We want percentage = 0.0 when Time.time = _timeStartedLerping
                //and percentage = 1.0 when Time.time = _timeStartedLerping + timeTakenDuringLerp
                //In other words, we want to know what percentage of "timeTakenDuringLerp" the value
                //"Time.time - _timeStartedLerping" is.
                float timeSinceStarted = Time.time - _timeStartedLerping;
                float percentageComplete = timeSinceStarted / timeTakenDuringLerp;

                //Perform the actual lerping.  Notice that the first two parameters will always be the same
                //throughout a single lerp-processs (ie. they won't change until we hit the space-bar again
                //to start another lerp)
                hit1.transform.eulerAngles = Vector3.Lerp(_startPosition, _endPosition, percentageComplete);

                //When we've completed the lerp, we set _isLerping to false
                if (percentageComplete >= 1.0f)
                {
                    _isLerping = false;
                    entrar = false;
                }
                
            }
            else if (!_isLerping&&entrar)
            {
                //We want percentage = 0.0 when Time.time = _timeStartedLerping
                //and percentage = 1.0 when Time.time = _timeStartedLerping + timeTakenDuringLerp
                //In other words, we want to know what percentage of "timeTakenDuringLerp" the value
                //"Time.time - _timeStartedLerping" is.
                float timeSinceStarted = Time.time - _timeStartedLerping;
                float percentageComplete = timeSinceStarted / timeTakenDuringLerp;

                //Perform the actual lerping.  Notice that the first two parameters will always be the same
                //throughout a single lerp-processs (ie. they won't change until we hit the space-bar again
                //to start another lerp)
                hit1.transform.eulerAngles = Vector3.Lerp(_endPosition,_startPosition , percentageComplete);

                //When we've completed the lerp, we set _isLerping to false
                if (percentageComplete >= 1.0f)
                {
                    _isLerping = true;
                    entrar = false;
                }


            }


        }

    }

}
Responder / Comentar
Subir
Foros > Unity > Ayuda con este codigo: raycast que abre puerta y la cierra;

Hay 0 respuestas en Ayuda con este codigo: raycast que abre puerta y la cierra;, del foro de Unity. Último comentario hace 6 años.

  • Regístrate

  • Información legal
Juegos© Foro 3DJuegos 2005-2024. . SOBRE FORO 3DJUEGOS | INFORMACIÓN LEGAL