Hi.Im wasted a lot time figuring out and searching in google...no success So i ask Here : Script A - its possible to change float gasInput = Input.GetAxis("Vertical"); so its works like when i select (RTS) tank and click mouse 1 its start move to destination? im tried a lot ways..and my game or scene now freeze when i press mouse 1 :/ any can help or give a hint?or this code will not work..? Script A Code (CSharp): void Inputs(){ if(!canControl){ gasInput = 0; brakeInput = 0; steerInput = 0; return; } //Motor Input. gasInput = Input.GetAxis("Vertical"); //Brake Input brakeInput = -Mathf.Clamp(Input.GetAxis("Vertical"), -1f, 0f); //Steering Input. steerInput = Input.GetAxis("Horizontal"); Camera script Code (CSharp): public static Vector3 GetDestination() { if (moveToDestination == Vector3.zero) { RaycastHit hit; Ray r = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(r, out hit)) { while (!passables.Contains(hit.transform.gameObject.name)) { if (!Physics.Raycast(hit.transform.position, r.direction, out hit)) break; } } if(hit.transform !=null) moveToDestination = hit.point; } return moveToDestination; } Unit Script Code (CSharp): public bool selected = false; public float floorOffset = 1; public float speed; public float stopDistanceOffset = 0.5f; public Vector3 moveToDest = Vector3.zero; // Update is called once per frame public void Update () { if (GetComponent<Renderer>().isVisible && Input.GetMouseButton(0)) { Vector3 camPos = Camera.main.WorldToScreenPoint(transform.position); camPos.y = CameraOperator.InvertMouseY(camPos.y); selected = CameraOperator.selection.Contains(camPos); if (selected) GetComponent<Renderer>().material.color = Color.red; else GetComponent<Renderer>().material.color = Color.white; } if (selected && Input.GetMouseButtonUp(1)) { Vector3 destination = CameraOperator.GetDestination(); if(destination != Vector3.zero) { moveToDest = destination; moveToDest.y += floorOffset; } } UpdateMove(); } private void UpdateMove() { if (moveToDest != Vector3.zero && transform.position != moveToDest) { Vector3 direction = (moveToDest - transform.position).normalized; direction.y = 0; transform.GetComponent<Rigidbody>().velocity = direction * speed ; if (Vector3.Distance(transform.position, moveToDest) < stopDistanceOffset) moveToDest = Vector3.zero; } else transform.GetComponent<Rigidbody>().velocity = Vector3.zero; }
Your code looks pretty reasonable, except (1) it's very hard to read because it isn't indented properly, and (2) you're using physics, which you probably don't need for something like an RTS game, and so that makes everything harder than it should be. Also, the logic in your UpdateMove seems a little odd... it seems to say: set our velocity to go towards the destination, and then if we're not very close to it, set it back to zero. That doesn't make much sense. Perhaps you meant something like this. Code (CSharp): private void UpdateMove() { if (moveToDest != Vector3.zero && transform.position != moveToDest) { Vector3 delta = moveToDest - transform.position; if (delta.magnitude < stopDistanceOffset) { // close enough! moveToDest = Vector3.zero; } else { // adjust our velocity to move towards our destination Vector3 direction = delta; direction.y = 0; transform.GetComponent<Rigidbody>().velocity = direction.normalized * speed ; } }
Hi!Thanks for response.Im new here so yes code can be ^^ :/ I updated code u give .Yes im using phsycs..got some results now i will work to steering cuz its same ^^ even harder i think .. :/