Search Unity

Can be done or no...

Discussion in 'Getting Started' started by Aress28, Feb 7, 2016.

  1. Aress28

    Aress28

    Joined:
    Dec 12, 2015
    Posts:
    100
    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):
    1. void Inputs(){
    2.  
    3.         if(!canControl){
    4.             gasInput = 0;
    5.             brakeInput = 0;
    6.             steerInput = 0;
    7.             return;
    8.         }
    9.      
    10.         //Motor Input.
    11.         gasInput = Input.GetAxis("Vertical");
    12.  
    13.         //Brake Input
    14.         brakeInput = -Mathf.Clamp(Input.GetAxis("Vertical"), -1f, 0f);
    15.  
    16.         //Steering Input.
    17.         steerInput = Input.GetAxis("Horizontal");
    Camera script
    Code (CSharp):
    1. public static Vector3 GetDestination()
    2.     {
    3.      
    4.             if (moveToDestination == Vector3.zero)
    5.             {
    6.                 RaycastHit hit;
    7.                 Ray r = Camera.main.ScreenPointToRay(Input.mousePosition);
    8.  
    9.                 if (Physics.Raycast(r, out hit))
    10.                 {
    11.                     while (!passables.Contains(hit.transform.gameObject.name))
    12.                     {
    13.                         if (!Physics.Raycast(hit.transform.position, r.direction, out hit))
    14.                             break;
    15.  
    16.                     }
    17.                  
    18.                 }
    19.                 if(hit.transform !=null)
    20.             moveToDestination = hit.point;
    21.          
    22.         }
    23.         return moveToDestination;
    24.         }
    Unit Script
    Code (CSharp):
    1. public bool selected = false;
    2.   public float floorOffset = 1;
    3.   public float speed;
    4.  
    5.   public float stopDistanceOffset = 0.5f;
    6.  
    7.   public Vector3 moveToDest = Vector3.zero;
    8.  
    9.  
    10.  
    11.  
    12.    // Update is called once per frame
    13.    public void Update () {
    14.  
    15.   if (GetComponent<Renderer>().isVisible && Input.GetMouseButton(0))
    16.   {
    17.   Vector3 camPos = Camera.main.WorldToScreenPoint(transform.position);
    18.   camPos.y = CameraOperator.InvertMouseY(camPos.y);
    19.   selected = CameraOperator.selection.Contains(camPos);
    20.  
    21.   if (selected)
    22.   GetComponent<Renderer>().material.color = Color.red;
    23.   else
    24.   GetComponent<Renderer>().material.color = Color.white;
    25.   }
    26.   if (selected && Input.GetMouseButtonUp(1))
    27.   {
    28.   Vector3 destination = CameraOperator.GetDestination();
    29.   if(destination != Vector3.zero)
    30.   {
    31.   moveToDest = destination;
    32.   moveToDest.y += floorOffset;
    33.   }
    34.   }
    35.   UpdateMove();
    36.  
    37.    }
    38.   private void UpdateMove()
    39.   {
    40.   if (moveToDest != Vector3.zero && transform.position != moveToDest)
    41.   {
    42.   Vector3 direction = (moveToDest - transform.position).normalized;
    43.   direction.y = 0;
    44.   transform.GetComponent<Rigidbody>().velocity = direction * speed ;
    45.  
    46.   if (Vector3.Distance(transform.position, moveToDest) < stopDistanceOffset)
    47.   moveToDest = Vector3.zero;
    48.   }
    49.   else
    50.   transform.GetComponent<Rigidbody>().velocity = Vector3.zero;
    51.   }
     
    Last edited: Feb 7, 2016
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    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):
    1. private void UpdateMove() {
    2.     if (moveToDest != Vector3.zero && transform.position != moveToDest) {
    3.     Vector3 delta = moveToDest - transform.position;
    4.     if (delta.magnitude < stopDistanceOffset) {
    5.         // close enough!
    6.         moveToDest = Vector3.zero;
    7.     } else {
    8.         // adjust our velocity to move towards our destination
    9.         Vector3 direction = delta;
    10.         direction.y = 0;
    11.         transform.GetComponent<Rigidbody>().velocity = direction.normalized * speed ;
    12.     }
    13. }
     
    Aress28 likes this.
  3. Aress28

    Aress28

    Joined:
    Dec 12, 2015
    Posts:
    100
    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 .. :/