public void TargetingRaycast() { // Current mouse position on screen Vector3 mp = Input.mousePosition; // Current target object transform component Transform targetTransform = null; // If camera component is available if (cam != null) { RaycastHit hitInfo; Ray ray = cam.ScreenPointToRay(new Vector3(mp.x, mp.y, 0f)); if (Physics.Raycast(ray.origin, ray.direction, out hitInfo, targetingRayLength, targetingLayerMask.value)) { targetTransform = hitInfo.collider.transform; } } if (targetTransform != null) { HighlightableObject ho = targetTransform.root.GetComponentInChildren <HighlightableObject>(); if (ho != null) { if (Input.GetButtonDown("Fire1")) { //显示信息 } ho.On(Color.red); } } }
public void TargetingRaycast() { // Current mouse position on screen Vector3 mp = Input.mousePosition; // Current target object transform component Transform targetTransform = null; // If camera component is available if (cam != null) { RaycastHit hitInfo; // Create a ray from mouse coords Ray ray = cam.ScreenPointToRay(new Vector3(mp.x, mp.y, 0f)); // Targeting raycast if (Physics.Raycast(ray.origin, ray.direction, out hitInfo, targetingRayLength, targetingLayerMask.value)) { // Cache what we've hit targetTransform = hitInfo.collider.transform; } } // If we've hit an object during raycast if (targetTransform != null) { // And this object has HighlightableObject component HighlightableObject ho = targetTransform.root.GetComponentInChildren <HighlightableObject>(); if (ho != null) { // If left mouse button down if (Input.GetButtonDown("Fire1")) { // Start flashing with frequency = 2 ho.FlashingOn(2f); } // If right mouse button is up if (Input.GetButtonUp("Fire2")) { // Stop flashing ho.FlashingOff(); } // If middle mouse button is up if (Input.GetButtonUp("Fire3")) { // Switch flashing ho.FlashingSwitch(); } // One-frame highlighting (to highlight an object which is currently under mouse cursor) ho.On(Color.red); } } }
// Update is called once per frame void Update() { Ray ray; RaycastHit hit; ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit)) { if (hit.collider.gameObject != null) { HighlightableObject ho = hit.collider.transform.GetComponentInChildren <HighlightableObject>(); if (ho != null) { ho.On(Color.red); //ho.OccluderOn(); } } } }
public void TargetingRaycast() { // Current mouse position on screen Vector3 mp = Input.mousePosition; // Current target object transform component targetTransform = null; // If camera component is available if (cam != null) { RaycastHit hitInfo; // Create a ray from mouse coords Ray ray = cam.ScreenPointToRay(new Vector3(mp.x, mp.y, 0f)); // Targeting raycast if (Physics.Raycast(ray.origin, ray.direction, out hitInfo, targetingRayLength, targetingLayerMask.value)) { // Cache what we've hit targetTransform = hitInfo.collider.transform; } } // If we've hit an object during raycast if (targetTransform != null) { // And this object has HighlightableObject component // HighlightableObject ho = targetTransform.root.GetComponentInChildren<HighlightableObject>(); HighlightableObject ho = targetTransform.GetComponent <HighlightableObject>(); if (ho != null && !IsMousePointUI) { ho.On(Color.cyan);//如果鼠标经过 就会执行 } } }
public void TargetingRaycast() { // Current mouse position on screen Vector3 mp = Input.mousePosition; Transform targetTransform = null; // If camera component is available if (cam != null) { RaycastHit hitInfo; // Create a ray from mouse coords Ray ray = cam.ScreenPointToRay(new Vector3(mp.x, mp.y, 0f)); // Targeting raycast if (Physics.Raycast(ray.origin, ray.direction, out hitInfo, targetingRayLength, targetingLayerMask.value)) { if (RPG_Animation.instance != null && hitInfo.collider.transform.gameObject != RPG_Animation.instance.transform.gameObject) { // Cache what we've hit targetTransform = hitInfo.collider.transform; } } } // If we've hit an object during raycast if (targetTransform != null) { // And this object has HighlightableObject component HighlightableObject ho = targetTransform.GetComponent <HighlightableObject>(); if (ho != null) { // If left mouse button down if (Input.GetButtonDown("Fire1")) { if (lasttargetTransform == targetTransform) { if ((Time.time - doubleClickStart) < 0.3f) { Common.DEBUG_MSG("Double Clicked!"); setTarget(targetTransform); } doubleClickStart = Time.time; return; } doubleClickStart = Time.time; setTarget(targetTransform); } /* * // If right mouse button is up * if (Input.GetButtonUp("Fire2")) * // Stop flashing * ho.FlashingOff(); * * // If middle mouse button is up * if (Input.GetButtonUp("Fire3")) * // Switch flashing * ho.FlashingSwitch(); */ // One-frame highlighting (to highlight an object which is currently under mouse cursor) ho.On(Color.red); } } }
// Update is called once per frame void Update() { //If it's my turn if (isMyTurn) { //And we LEFT click if (Input.GetKeyDown(KeyCode.Mouse0)) { mousePosUp = Input.mousePosition; mouseXUp = mousePosUp.x; mouseYUp = mousePosUp.y; leftUpRay = mainCam.ScreenPointToRay(new Vector3(mouseXUp, mouseYUp, 0)); Debug.DrawRay (leftUpRay.origin, leftUpRay.direction * 800, Color.yellow); //And that click is on a if (Physics.Raycast(leftUpRay, out leftUpHit, 800, nonTileMask)) { mouse3DHitUp = leftUpHit.point; publicLeftHit = leftUpHit.point; Debug.Log(leftUpHit.collider.gameObject.name); GameObject tempHit = leftUpHit.transform.gameObject; script_2PBot tempBot = tempHit.transform.gameObject.GetComponent<script_2PBot>(); //Game object on the "Child" layer if (tempHit.gameObject.tag == "Child") { if (selectedUnit != null) { if (tempUnitControl.targetMover.target != null) { //Deselect All Units selectedUnit.transform.position = startPos; //Disable the Unit's Movement tempUnitControl = (script_2PBot)selectedUnit.GetComponent("script_2PBot"); tempUnitControl.selected = false; tempUnitControl.selectedBy = null; tempUnitControl.currentState = 0; tempUnitControl.targetMover.target.transform.position = selectedUnit.transform.position; //Reset our variables movingUnit = false; } ClearUnits(); } //If this is OUR unit and not the enemy's if (gameManager.player1Turn) { if (tempBot.player1Unit == true) { //Select Unit selectedUnit = tempHit; targetScript.selectedUnit = selectedUnit; unitStats = selectedUnit.GetComponent<UnitStats>(); tempUnitControl = (script_2PBot)tempHit.GetComponent("script_2PBot"); tempUnitControl.targetMover.target.transform.position = selectedUnit.transform.position; tempUnitControl.selected = true; tempUnitControl.selectedBy = this.gameObject; //tempUnitControl.currentState = 1; //Hi-light the unit tempRend = tempHit.gameObject.GetComponentInChildren<Renderer>(); //tempRend.material.shader = hiLightShader; tempHL = selectedUnit.gameObject.GetComponent<HighlightableObject>(); tempHL.ConstantOn(highlightColor); //Get the unit's Starting Position startPos = tempHit.gameObject.transform.position; if (tempUnitControl.canMove) { movingUnit = true; } showAttack = false; } else //This is the enemy's unit { Debug.Log("Enemy Unit hit."); // if (movingUnit) // { // showAttack = true; // tempUnitControl.botAI.target.position = tempHit.transform.position; // } //else //{ selectedUnit = tempHit; targetScript.selectedUnit = selectedUnit; unitStats = selectedUnit.GetComponent<UnitStats>(); tempUnitControl = (script_2PBot)tempHit.GetComponent("script_2PBot"); Debug.Log("Selecting unit."); tempUnitControl.selected = true; tempUnitControl.currentState = 0; tempUnitControl.selectedBy = this.gameObject; tempRend = tempHit.gameObject.GetComponentInChildren<Renderer>(); //tempRend.material.shader = enemyShader; tempHL = selectedUnit.gameObject.GetComponent<HighlightableObject>(); tempHL.ConstantOn(Color.red); //} } } else if (gameManager.player2Turn) { if (tempBot.player2Unit == true) { //Select Unit selectedUnit = tempHit; targetScript.selectedUnit = selectedUnit; unitStats = selectedUnit.GetComponent<UnitStats>(); tempUnitControl = (script_2PBot)tempHit.GetComponent("script_2PBot"); tempUnitControl.targetMover.target.transform.position = selectedUnit.transform.position; tempUnitControl.selected = true; tempUnitControl.selectedBy = this.gameObject; //tempUnitControl.currentState = 1; //Hi-light the unit tempRend = tempHit.gameObject.GetComponentInChildren<Renderer>(); //tempRend.material.shader = hiLightShader; tempHL = selectedUnit.gameObject.GetComponent<HighlightableObject>(); tempHL.ConstantOn(highlightColor); //Get the unit's Starting Position startPos = tempHit.gameObject.transform.position; if (tempUnitControl.canMove) { movingUnit = true; } showAttack = false; } else //This is the enemy's unit { Debug.Log("Enemy Unit hit."); // if (movingUnit) // { // showAttack = true; // tempUnitControl.botAI.target.position = tempHit.transform.position; // } // else // { selectedUnit = tempHit; targetScript.selectedUnit = selectedUnit; unitStats = selectedUnit.GetComponent<UnitStats>(); tempUnitControl = (script_2PBot)tempHit.GetComponent("script_2PBot"); tempUnitControl.selected = true; tempUnitControl.selectedBy = this.gameObject; tempUnitControl.currentState = 0; tempRend = tempHit.gameObject.GetComponentInChildren<Renderer>(); //tempRend.material.shader = enemyShader; Debug.Log("Highlighting"); tempHL = selectedUnit.gameObject.GetComponent<HighlightableObject>(); tempHL.ConstantOn(Color.red); //} } } } else // This is not a unit, so clear everything { Debug.Log("Clearing All"); if (GUIUtility.hotControl == 0) { //Send the unit back to the starting spot if (selectedUnit != null) { if (tempUnitControl.selectedBy == tempUnitControl.ourPlayer) { selectedUnit.transform.position = startPos; } //Disable the Unit's Movement tempUnitControl = (script_2PBot)selectedUnit.GetComponent("script_2PBot"); tempUnitControl.selected = true; tempUnitControl.currentState = 0; //Reset our variables movingUnit = false; showAttack = false; ClearUnits(); } } } } } //If we RIGHT click if (Input.GetKeyDown(KeyCode.Mouse1)) { mousePosUp = Input.mousePosition; mouseXUp = mousePosUp.x; mouseYUp = mousePosUp.y; leftUpRay = mainCam.ScreenPointToRay(new Vector3(mouseXUp, mouseYUp, 0)); Debug.DrawRay (leftUpRay.origin, leftUpRay.direction * 800, Color.yellow); //And that click is on a if (Physics.Raycast(leftUpRay, out leftUpHit, 800)) { mouse3DHitUp = leftUpHit.point; publicLeftHit = leftUpHit.point; Debug.Log(leftUpHit.collider.gameObject.name); GameObject tempHit = leftUpHit.transform.gameObject; script_2PBot tempBot = tempHit.transform.gameObject.GetComponent<script_2PBot>(); //we hit a unit if (tempHit.gameObject.tag == "Child") { //we have a unit selected if (selectedUnit != null) { //we are a jock if (playerType == PlayerType.Jock) { //we selected a nerd if (tempHit.gameObject.name.Contains("Nerd")) { //our selected unit is a jock if (selectedUnit.gameObject.name.Contains("Jock")) { Debug.Log("Nerd Hit"); targetEnemy = tempHit.gameObject; if (Vector3.Distance(selectedUnit.gameObject.transform.position, targetEnemy.transform.position) < tempUnitControl.remainingDistance) { if (tempUnitControl.canAttack) { tempUnitControl.selected = true; tempUnitControl.currentState = 1; //Get the unit's Starting Position startPos = tempHit.gameObject.transform.position; tempUnitControl.targetMover.target.transform.position = targetEnemy.transform.position; attackMode = true; AkSoundEngine.PostEvent("Play_AttackSelect", this.gameObject); AkSoundEngine.PostEvent("Set_State_Attack", this.gameObject); Destroy(tempUnitControl.localMoveDistanceObj); } } } } } else if (playerType == PlayerType.Nerd) { if (tempHit.gameObject.name.Contains("Jock")) { if (selectedUnit.gameObject.name.Contains("Nerd")) { Debug.Log("Jock Hit"); targetEnemy = tempHit.gameObject; if (Vector3.Distance(selectedUnit.gameObject.transform.position, targetEnemy.transform.position) < tempUnitControl.remainingDistance) { if (tempUnitControl.canAttack) { tempUnitControl.selected = true; tempUnitControl.currentState = 1; //Get the unit's Starting Position startPos = tempHit.gameObject.transform.position; tempUnitControl.targetMover.target.transform.position = targetEnemy.transform.position; attackMode = true; AkSoundEngine.PostEvent("Play_AttackSelect", this.gameObject); AkSoundEngine.PostEvent("Set_State_Attack", this.gameObject); Destroy(tempUnitControl.localMoveDistanceObj); } } } } } } } } } //End of Right Click if (attackMode) { if (selectedUnit != null) { float distance = Vector3.Distance(selectedUnit.gameObject.transform.position, targetEnemy.gameObject.transform.position); if (distance < unitStats.AtkRng) { script_2PBot botScript = selectedUnit.gameObject.GetComponent<script_2PBot>(); if (botScript.canAttack) { Vector3 spawnSpot = new Vector3(targetEnemy.transform.position.x, targetEnemy.transform.position.y + 3, targetEnemy.transform.position.z); Instantiate(attackEffect, spawnSpot, Quaternion.identity); tempUnitControl.targetMover.target.transform.position = selectedUnit.transform.position; tempUnitControl.currentState = 0; Debug.Log("Attacking!!!!"); UnitStats enemyScript = targetEnemy.gameObject.GetComponent<UnitStats>(); enemyScript.CurrentHealth -= unitStats.AtkDmg; attackMode = false; movingUnit = false; botScript.currentState = 0; botScript.canAttack = false; ClearUnits(); } } } } if (movingUnit) { if (movesRemaining > 0) { if (Input.GetKeyDown(KeyCode.Space)) { if (Vector3.Distance(selectedUnit.transform.position, startPos) > 1) { movesRemaining--; } //Disable the Unit's Movement tempUnitControl = (script_2PBot)selectedUnit.GetComponent("script_2PBot"); tempUnitControl.selected = true; tempUnitControl.currentState = 0; if (Vector3.Distance(tempUnitControl.targetSpot, tempUnitControl.spotPos) < tempUnitControl.remainingDistance) { Vector3 newPos = new Vector3(tempUnitControl.targetMover.target.transform.position.x, tempUnitControl.gameObject.transform.position.y, tempUnitControl.targetMover.target.transform.position.z); tempUnitControl.gameObject.transform.position = newPos; } //Reset our variables movingUnit = false; tempUnitControl.canMove = false; ClearUnits(); AkSoundEngine.PostEvent("Play_Confirm", this.gameObject); } } } //Constant Update for Highlighting mousePosUp = Input.mousePosition; mouseXUp = mousePosUp.x; mouseYUp = mousePosUp.y; leftUpRay = mainCam.ScreenPointToRay(new Vector3(mouseXUp, mouseYUp, 0)); Debug.DrawRay (leftUpRay.origin, leftUpRay.direction * 800, Color.yellow); //And that click is on a if (Physics.Raycast(leftUpRay, out leftUpHit, 800)) { mouse3DHitUp = leftUpHit.point; publicLeftHit = leftUpHit.point; //Debug.Log(leftUpHit.collider.gameObject.name); GameObject tempHit = leftUpHit.transform.gameObject; script_2PBot tempBot = tempHit.transform.gameObject.GetComponent<script_2PBot>(); //Game object on the "Child" layer if (tempHit.gameObject.tag == "Child") { if (playerType == PlayerType.Nerd) { if (tempHit.gameObject.name.Contains("Nerd")) { tempHL = tempHit.gameObject.GetComponent<HighlightableObject>(); tempHL.On(highlightColor); } if (tempHit.gameObject.name.Contains("Jock")) { tempHL = tempHit.gameObject.GetComponent<HighlightableObject>(); tempHL.On(Color.red); } } if (playerType == PlayerType.Jock) { if (tempHit.gameObject.name.Contains("Jock")) { tempHL = tempHit.gameObject.GetComponent<HighlightableObject>(); tempHL.On(highlightColor); } if (tempHit.gameObject.name.Contains("Nerd")) { tempHL = tempHit.gameObject.GetComponent<HighlightableObject>(); tempHL.On(Color.red); } } } } } }