Hi guys !
I want to hide a player in a cube which call Forest when he press “E”. Then if the player hit again “E” he quits the Cube Forest to pop at a location.
I tried so many times to do that but I don’t know why, my loop while freeze my game, and I don’t find how can I assign the key “E” for this two events.
Here is my code (he doesn’t work, but if you can give me some advices pleaaaase and why my game freeze ? )
THANKS THANKS A LOT !
PlayerMovement :
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
public Forest forest;
public int moveSpeed;
public Vector3 playerPosition;
public Vector3 positionTransform;
public bool inForest;
void Start () {
forest = forest.GetComponent<Forest>();
inForest = false;
}
void Update()
{
// CHARACTER MOVEMENT
transform.Translate(Input.GetAxisRaw("Horizontal") * Time.deltaTime * moveSpeed, 0f, 0f);
transform.Translate(0f, 0f, Input.GetAxisRaw("Vertical") * Time.deltaTime * moveSpeed);
playerPosition = GameObject.Find("Player").transform.position;
//GO FOREST
while (inForest == true && Input.GetKeyDown("e"))
{
forest.HideForest();
if (Input.GetKeyDown("e"))
{
forest.ExitForest();
}
}
}
//COLLIDER
void OnTriggerEnter(Collider other)
{
if (other.transform.tag == "Forest")
{
inForest = true;
}
}
}
Here is my Forest Code :
using UnityEngine;
using System.Collections;
public class Forest : MonoBehaviour {
public Vector3 playerForest;
public Vector3 exitPos;
public void HideForest()
{
//PLAYER IS HIDDIND TO THIS VECTOR
playerForest = new Vector3(20, -1, -5);
GameObject.Find("Player").transform.position = playerForest;
}
public void ExitForest()
{
if (Input.GetKey("e"))
{
exitPos = GameObject.Find("ForestExit").transform.position;
GameObject.Find("Player").transform.position = exitPos;
}
}
}