// Use this for initialization void Start() { bishopB = GameObject.Find("bishopB").GetComponent <BishopMove>(); bishopW = GameObject.Find("bishopW").GetComponent <BishopMove>(); bishopBT = GameObject.Find("bishopB").transform; bishopWT = GameObject.Find("bishopW").transform; //Instantiate (Resources.Load ("BishopCDBubble"), new Vector3 (bishopBT.position.x, bishopBT.position.y + 0.30f, bishopBT.position.z), new Quaternion (0, 0, 0, 1)); //Instantiate (Resources.Load ("BishopCDBubble"), new Vector3 (bishopWT.position.x, bishopWT.position.y + 0.30f, bishopWT.position.z), new Quaternion (0, 0, 0, 1)); aStar = new A_Star_Controler(); moveTimer = moveTimerCap; setTimer = true; setTarget = true; }
// Update is called once per frame void Update() { //setting target of movment to mouse position when mouse is cliced if (Input.GetMouseButtonDown(0)) { //raycast from the cameras position looking for the ground RaycastHit hit; //cheking to see if you have hit an object with your click if (!Physics.Raycast(camera.ScreenPointToRay(Input.mousePosition), out hit, 100)) { return; } //cheking to see if the object you hit has a colider if (!hit.transform) { return; } //make the target location where the raycast hit target = hit.point; move = true; //setting the A* target to the new target //Debug.Log("Creat instance of A* with \n startPoint = " + GameObject.Find("Hand").transform.position + "\n endPoint = " + target); A_Star = new A_Star_Controler(GameObject.Find("Hand").transform.position, target); if (A_Star.movesToMake.Count > 0) { //pop off the first movment location from the stack of moves target = (Vector3)A_Star.movesToMake.Pop(); } } try { var temp = target - transform.position; if (temp.magnitude < .005f && A_Star.movesToMake.Count > 0) { //pop off the first movment location from the stack of moves target = (Vector3)A_Star.movesToMake.Pop(); } } catch (System.NullReferenceException e) { } target.y = .5f; //the direction that the character needs to go var steering = steeringDirection(); //limmiting the stearing vecor with a max speed steering /= timeToTarget; if (steering.magnitude > maxSpeed) { steering.Normalize(); steering *= maxSpeed; } //exicuting a lurping turn twards the target if (move) { rb.transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(steering), Time.deltaTime * turnSpeed); //rb.transform.rotation = Quaternion.LookRotation(steering); //moving twards the target rb.velocity = steering; } else { rb.velocity = Vector3.zero; rb.angularVelocity = Vector3.zero; } }