private GravityGunState CheckState(GravityGunState state) { switch (state) { case GravityGunState.Unoccupied: if (isMouseDown && HasObject() && !IsObjectInRange()) { return(GravityGunState.Pulling); } else { return(state); } case GravityGunState.Pulling: if (!isMouseDown) { return(GravityGunState.Unoccupied); } else { return(state); } default: return(GravityGunState.Unoccupied); } }
void FixedUpdate() { if (gravityGunState == GravityGunState.Free) { if (Input.GetKey(KeyCode.Space)) { RaycastHit hit; if (Physics.Raycast(transform.position, transform.forward, out hit, catchRange, layerMask)) { if (hit.rigidbody) { rigid = hit.rigidbody; gravityGunState = GravityGunState.Catch; } } } } else if (gravityGunState == GravityGunState.Catch) { rigid.MovePosition(transform.position + transform.forward * holdDistance); if (!Input.GetKey(KeyCode.Space)) { gravityGunState = GravityGunState.Occupied; } } else if (gravityGunState == GravityGunState.Occupied) { rigid.MovePosition(transform.position + transform.forward * holdDistance); if (Input.GetKey(KeyCode.Space)) { gravityGunState = GravityGunState.Charge; } } else if (gravityGunState == GravityGunState.Charge) { rigid.MovePosition(transform.position + transform.forward * holdDistance); if (currentForce < maxForce) { currentForce += forceChargePerSec * Time.deltaTime; } else { currentForce = maxForce; } if (!Input.GetKey(KeyCode.Space)) { gravityGunState = GravityGunState.Release; } } else if (gravityGunState == GravityGunState.Release) { rigid.AddForce(transform.forward * currentForce); currentForce = minForce; gravityGunState = GravityGunState.Free; } }
private void Reset() { state = GravityGunState.Unoccupied; canShootRay = true; hitObjectTransform = null; if (hitObjectRB) { hitObjectRB.useGravity = true; hitObjectRB = null; } newDistance = baseDistance; UpdateHoldPointPosition(newDistance); distanceFromHoldPointToObject = Mathf.Infinity; pullDirection = Vector3.zero; }
private void Update() { if (Input.GetMouseButtonDown(0)) { isMouseDown = true; ShootRay(); } if (Input.GetMouseButtonUp(0)) { isMouseDown = false; Reset(); } if (isMouseDown) { if (HasObject()) { UpdateDistanceFromHoldPointToObject(); UpdatePullDirection(); } CheckIfChangeHoldPointDistance(); } state = CheckState(state); switch (state) { case GravityGunState.Unoccupied: break; case GravityGunState.Pulling: PullObject(pullDirection); break; } }
// Handle rotation here to ensure smooth application. void Update() { if (isRemotePlayer) return; float rotationAmount; if(target.name.Contains("Builder")) { if (Input.GetKeyUp("r")){ if(blockammo>0){ PhotonNetwork.Instantiate("pBlock",transform.position+transform.forward,transform.rotation,0); blockammo--; } } if (Input.GetKeyUp("t")){ if(plankammo>0){ PhotonNetwork.Instantiate("pPlatform",transform.position+transform.forward,transform.rotation,0); plankammo--; } } } if(target.name.Contains("Mover")) { if(gravityGunState == GravityGunState.Free) { if(Input.GetButton("Fire1")) { RaycastHit hit; LayerMask layerMask = -1; if(Physics.Raycast(transform.position, transform.forward-transform.up,out hit, 50.0f, layerMask)) { if(hit.rigidbody) { rigid = hit.rigidbody; rigid.isKinematic = true; gravityGunState = GravityGunState.Catch; } } } } else if(gravityGunState == GravityGunState.Catch) { rigid.transform.position = transform.position + transform.forward * holdDistance; rigid.transform.rotation = transform.rotation; if(!Input.GetButton("Fire1")) gravityGunState = GravityGunState.Occupied; } else if(gravityGunState == GravityGunState.Occupied) { rigid.transform.position = transform.position + transform.forward * holdDistance; rigid.transform.rotation = transform.rotation; if(Input.GetButton("Fire1")) gravityGunState = GravityGunState.Charge; } else if(gravityGunState == GravityGunState.Charge && Screen.lockCursor == true) { rigid.transform.position = transform.position + transform.forward * holdDistance; rigid.transform.rotation = transform.rotation; if(!Input.GetButton("Fire1") && Screen.lockCursor == true) { if(rigid.name.Contains("pPlatform")) rigid.isKinematic = true; else rigid.isKinematic = false; gravityGunState = GravityGunState.Release; } } else if(gravityGunState == GravityGunState.Release && Screen.lockCursor == true) { gravityGunState = GravityGunState.Free; } } if (Input.GetMouseButton (1) && (!requireLock || controlLock || Screen.lockCursor)) // If the right mouse button is held, rotation is locked to the mouse { if (controlLock) { Screen.lockCursor = true; } rotationAmount = Input.GetAxis ("Mouse X") * mouseTurnSpeed * Time.deltaTime; } else { if (controlLock) { Screen.lockCursor = false; } rotationAmount = Input.GetAxis ("Horizontal") * turnSpeed * Time.deltaTime; } target.transform.RotateAround (target.transform.up, rotationAmount); if (Input.GetKeyDown(KeyCode.Backslash) || Input.GetKeyDown(KeyCode.Plus)) { walking = !walking; } }
// Handle rotation here to ensure smooth application. void Update() { if (isRemotePlayer) return; float rotationAmount; if(target.name.Contains("Builder")) { if (Input.GetKeyUp("1") && !menuOn){ if(blockammo>0){ //Creating object var builtBlock = PhotonNetwork.Instantiate("pBlock", transform.position + transform.forward, transform.rotation, 0); builtBlock.tag = "PlacedBlock"; blockammo--; } } if (Input.GetKeyUp("2") && !menuOn){ if(plankammo>0){ var builtPlatform = PhotonNetwork.Instantiate("pPlatform", transform.position + transform.forward * transform.localScale.z * 2, transform.rotation, 0); builtPlatform.tag = "PlacedPlatform"; plankammo--; } } } if(target.name.Contains("Mover")) { if(gravityGunState == GravityGunState.Free) { if(Input.GetKeyUp("t") && !menuOn) { //float range = target.transform.localScale.z * triggerHoldRange; //float rad = target.collider.radius; RaycastHit hit; //removed layermask, not actually needed //int layerMask = 1 << 8; print ("Searching for pickable objects"); //if (Physics.SphereCast(target.transform.position, 0.2f, target.transform.forward, out hit, 2.0f, layerMask)){ //origin, height, direction, hit, radius, layer if(Physics.CapsuleCast(target.transform.position,(target.transform.position - target.transform.up),0.2f,target.transform.forward,out hit,holdDistance * 2.0f)){//distance, ,radius if(hit.rigidbody) { Debug.Log("Picked an object"); rigid = hit.rigidbody; // rigid.isKinematic = true; //This prevents vikings from picking up other vikings. Only platforms and blocks can be picked up. if (rigid.tag.Contains("BlockTrigger") || rigid.tag.Contains("PlacedBlock") || rigid.tag.Contains("PlatformTrigger") || rigid.tag.Contains("PlacedPlatform")) { if (rigid.gameObject.GetComponent<BoxUpdate>()) { rigid.gameObject.GetComponent<BoxUpdate>().setCarry(true); } if (rigid.gameObject.GetComponent<ProjectileUpdate>()) { rigid.gameObject.GetComponent<ProjectileUpdate>().enabled = true; } gravityGunState = GravityGunState.Catch; } else rigid = null; } } } } else if(gravityGunState == GravityGunState.Catch) { //holdDistance = transform.localScale.z + rigid.transform.localScale.z; rigid.transform.position = transform.position + transform.forward * holdDistance; rigid.transform.rotation = transform.rotation; if(!Input.GetKeyUp("t") && !menuOn) gravityGunState = GravityGunState.Occupied; } else if(gravityGunState == GravityGunState.Occupied) { if (!rigid) { gravityGunState = GravityGunState.Free; return; } rigid.transform.position = transform.position + transform.forward * holdDistance; rigid.transform.rotation = transform.rotation; if(Input.GetKeyUp("t") && !menuOn) gravityGunState = GravityGunState.Charge; } else if(gravityGunState == GravityGunState.Charge) { rigid.transform.position = transform.position + transform.forward * holdDistance; rigid.transform.rotation = transform.rotation; if(!Input.GetKeyUp("t") && !menuOn) { if(rigid.name.Contains("pPlatform")) { // rigid.isKinematic = true; rigid.gameObject.GetComponent<ProjectileUpdate>().enabled = false; } else { if (rigid.gameObject.GetComponent<BoxUpdate>()) { rigid.gameObject.GetComponent<BoxUpdate>().setCarry(false); } rigid.isKinematic = false; } gravityGunState = GravityGunState.Release; } } else if(gravityGunState == GravityGunState.Release) { rigid = null; gravityGunState = GravityGunState.Free; } } //swapped //if (Input.GetMouseButton (1) && (!requireLock || controlLock || Screen.lockCursor)) /* if (menuOn && (!requireLock || controlLock || Screen.lockCursor)) // If the right mouse button is held, rotation is locked to the mouse { if (controlLock) { Screen.lockCursor = false; } rotationAmount = Input.GetAxis ("Horizontal") * turnSpeed * Time.deltaTime; //rotationAmount = 0; //return; } else { if (controlLock) { Screen.lockCursor = true; } rotationAmount = Input.GetAxis ("Mouse X") * mouseTurnSpeed * Time.deltaTime; }*/ if (!menuOn) { rotationAmount = Input.GetAxis ("Horizontal") * turnSpeed * Time.deltaTime; target.transform.RotateAround (target.transform.up, rotationAmount); } if (Input.GetKeyDown(KeyCode.Backslash) || Input.GetKeyDown(KeyCode.Plus)) { walking = !walking; } if(Input.GetKeyDown(KeyCode.P) && !menuOn) { GameManagerVik manager = GameObject.Find("Code").GetComponent<GameManagerVik>(); if (manager.level_tester_mode) { if (manager.selectedClass == "Builder") { manager.selectedClass = "Mover"; target.name = "Mover"; jumpSpeed = 15.0f; } else if (manager.selectedClass == "Mover") { manager.selectedClass = "Jumper"; target.name = "Jumper"; jumpSpeed = 21.5f; } else if (manager.selectedClass == "Jumper") { manager.selectedClass = "Viewer"; target.name = "Viewer"; jumpSpeed = 15.0f; } else if (manager.selectedClass == "Viewer") { manager.selectedClass = "Builder"; target.name = "Builder"; jumpSpeed = 15.0f; } } } //New GUI for in game "pause" menu if (Input.GetKeyDown(KeyCode.F1) || Input.GetKeyDown(KeyCode.Escape)){ menuToggle(); } }
void Update() // Handle rotation here to ensure smooth application. { if (isRemotePlayer) { return; } float rotationAmount; if (target.name.Contains("Builder")) { if (Input.GetKeyUp("1") && !menuOn) { if (blockammo > 0) { //Creating object var builtBlock = PhotonNetwork.Instantiate("pBlock", transform.position + transform.forward, transform.rotation, 0); builtBlock.tag = "PlacedBlock"; blockammo--; } } if (Input.GetKeyUp("2") && !menuOn) { if (plankammo > 0) { var builtPlatform = PhotonNetwork.Instantiate("pPlatform", transform.position + transform.forward * transform.localScale.z * 2, transform.rotation, 0); builtPlatform.tag = "PlacedPlatform"; plankammo--; } } } if (target.name.Contains("Mover")) { if (gravityGunState == GravityGunState.Free) { if (Input.GetKeyUp("t") && !menuOn) { //float range = target.transform.localScale.z * triggerHoldRange; //float rad = target.collider.radius; RaycastHit hit; //removed layermask, not actually needed //int layerMask = 1 << 8; print("Searching for pickable objects"); //if (Physics.SphereCast(target.transform.position, 0.2f, target.transform.forward, out hit, 2.0f, layerMask)){ //origin, height, direction, hit, radius, layer if (Physics.CapsuleCast(target.transform.position, (target.transform.position - target.transform.up), 0.2f, target.transform.forward, out hit, holdDistance * 2.0f)) //distance, ,radius { if (hit.rigidbody) { Debug.Log("Picked an object"); rigid = hit.rigidbody; // rigid.isKinematic = true; //This prevents vikings from picking up other vikings. Only platforms and blocks can be picked up. if (rigid.tag.Contains("BlockTrigger") || rigid.tag.Contains("PlacedBlock") || rigid.tag.Contains("PlatformTrigger") || rigid.tag.Contains("PlacedPlatform")) { if (rigid.gameObject.GetComponent <BoxUpdate>()) { rigid.gameObject.GetComponent <BoxUpdate>().setCarry(true); } if (rigid.gameObject.GetComponent <ProjectileUpdate>()) { rigid.gameObject.GetComponent <ProjectileUpdate>().enabled = true; } gravityGunState = GravityGunState.Catch; } else { rigid = null; } } } } } else if (gravityGunState == GravityGunState.Catch) { //holdDistance = transform.localScale.z + rigid.transform.localScale.z; rigid.transform.position = transform.position + transform.forward * holdDistance; rigid.transform.rotation = transform.rotation; if (!Input.GetKeyUp("t") && !menuOn) { gravityGunState = GravityGunState.Occupied; } } else if (gravityGunState == GravityGunState.Occupied) { if (!rigid) { gravityGunState = GravityGunState.Free; return; } rigid.transform.position = transform.position + transform.forward * holdDistance; rigid.transform.rotation = transform.rotation; if (Input.GetKeyUp("t") && !menuOn) { gravityGunState = GravityGunState.Charge; } } else if (gravityGunState == GravityGunState.Charge) { rigid.transform.position = transform.position + transform.forward * holdDistance; rigid.transform.rotation = transform.rotation; if (!Input.GetKeyUp("t") && !menuOn) { if (rigid.name.Contains("pPlatform")) { // rigid.isKinematic = true; rigid.gameObject.GetComponent <ProjectileUpdate>().enabled = false; } else { if (rigid.gameObject.GetComponent <BoxUpdate>()) { rigid.gameObject.GetComponent <BoxUpdate>().setCarry(false); } rigid.isKinematic = false; } gravityGunState = GravityGunState.Release; } } else if (gravityGunState == GravityGunState.Release) { rigid = null; gravityGunState = GravityGunState.Free; } } //swapped //if (Input.GetMouseButton (1) && (!requireLock || controlLock || Screen.lockCursor)) /* * if (menuOn && (!requireLock || controlLock || Screen.lockCursor)) * * // If the right mouse button is held, rotation is locked to the mouse * { * * if (controlLock) * { * Screen.lockCursor = false; * } * * rotationAmount = Input.GetAxis ("Horizontal") * turnSpeed * Time.deltaTime; * //rotationAmount = 0; * //return; * } * else * { * if (controlLock) * { * Screen.lockCursor = true; * } * * rotationAmount = Input.GetAxis ("Mouse X") * mouseTurnSpeed * Time.deltaTime; * * }*/ if (!menuOn) { rotationAmount = Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime; target.transform.RotateAround(target.transform.up, rotationAmount); } if (Input.GetKeyDown(KeyCode.Backslash) || Input.GetKeyDown(KeyCode.Plus)) { walking = !walking; } if (Input.GetKeyDown(KeyCode.P) && !menuOn) { GameManagerVik manager = GameObject.Find("Code").GetComponent <GameManagerVik>(); if (manager.level_tester_mode) { if (manager.selectedClass == "Builder") { manager.selectedClass = "Mover"; target.name = "Mover"; jumpSpeed = 15.0f; } else if (manager.selectedClass == "Mover") { manager.selectedClass = "Jumper"; target.name = "Jumper"; jumpSpeed = 21.5f; } else if (manager.selectedClass == "Jumper") { manager.selectedClass = "Viewer"; target.name = "Viewer"; jumpSpeed = 15.0f; } else if (manager.selectedClass == "Viewer") { manager.selectedClass = "Builder"; target.name = "Builder"; jumpSpeed = 15.0f; } } } //New GUI for in game "pause" menu if (Input.GetKeyDown(KeyCode.F1) || Input.GetKeyDown(KeyCode.Escape)) { menuToggle(); } }