Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Script dont work when i build game

Discussion in 'Scripting' started by HelloKity1231, Dec 3, 2012.

  1. HelloKity1231

    HelloKity1231

    Joined:
    Nov 25, 2012
    Posts:
    121
    Hello, i use a click to move script, when i press to play form unity everything works but when i build game then i press click but character dont moves :/ (mouse click work bcause i checked other actions and works, just character dont moves)

    this is my script (still not completed) (Fixed)
    WARNING: Add "Ground" tag in your world-terrain or edit script

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.        
    5. public class C2Move : MonoBehaviour      
    6. {
    7.     // The Camera we use
    8.     public Camera myCamera;
    9.        
    10.     // Target Point
    11.     private Vector3 TPoint;
    12.          
    13.     public float Speed = 5.0f;
    14.     private float MaxClickDist = 1000.0f;
    15.    
    16.     public float Distance
    17.     {    
    18.         get;    
    19.         set;        
    20.     }
    21.    
    22.     void Start ()
    23.     {
    24.         // Initial Points
    25.         TPoint = transform.position;
    26.     }
    27.    
    28.     void Update ()
    29.     {
    30.         HandleClick();
    31.     }
    32.      
    33.     void HandleClick()
    34.     {
    35.         if (Input.GetMouseButtonDown(0))
    36.         {
    37.             // Get Clicked Position in 3D World
    38.             Ray ray = myCamera.ScreenPointToRay(Input.mousePosition);
    39.             RaycastHit hit;
    40.                    
    41.             if (Physics.Raycast(ray, out hit, MaxClickDist))
    42.             {
    43.                 // This will allow take target without any prob
    44.                 if (hit.transform.tag != "Ground")
    45.                     return;
    46.                
    47.                 TPoint = hit.point;
    48.             }
    49.         }
    50.        
    51.         // Move character          
    52.         MoveCharacter();
    53.        
    54.     }
    55.            
    56.     void MoveCharacter()      
    57.     {
    58.         Distance = Vector3.Distance(TPoint, transform.position);    
    59.         if (Distance > 0.2f)
    60.         {
    61.             LookAtPoint();
    62.             transform.position = Vector3.MoveTowards(transform.position, TPoint, Time.deltaTime * Speed);
    63.         }
    64.            
    65.     }    
    66.            
    67.     void LookAtPoint ()            
    68.     {                
    69.         Distance = Vector3.Distance(TPoint, transform.position);    
    70.         if (Distance > 1.0f)      
    71.         {                    
    72.             Quaternion targetRotation = Quaternion.LookRotation(TPoint - transform.position);      
    73.             transform.rotation = targetRotation;  
    74.         }    
    75.     }
    76.      
    77. }
    78.  
     
    Last edited: Dec 3, 2012
  2. HelloKity1231

    HelloKity1231

    Joined:
    Nov 25, 2012
    Posts:
    121
    Problem fixed, script updated.