Search Unity

C# RTS controller

Discussion in 'Scripting' started by nooy, Feb 23, 2010.

  1. nooy

    nooy

    Joined:
    Feb 5, 2010
    Posts:
    1
    Hi, i need help,

    In the scene, I have the cube Player (with arms and point spawnPoint1) and cube Enemy.
    The player moves on the plane (with A * Pathfinding Project 2.5http://forum.unity3d.com/viewtopic.php?t=43822)left mouse button and chooses the enemy using the right mouse button.
    What do I need to add the script to the player turned and attacked(shoot) the enemy selected, pressing the left mouse button?

    Player script
    MainPlayer.cs
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MainPlayer : MonoBehaviour {
    5.  
    6.  
    7.    
    8.     private CharacterController controller;
    9.  
    10.    
    11.             public Transform bullitPrefab;
    12.     public Transform Spawn;
    13.     private GameObject fireBullit;
    14.  
    15.    
    16.    
    17.         void Update () {
    18.    
    19.             Shoot();
    20.         }
    21.    
    22.    
    23.    
    24.     public void Shoot () {
    25.        
    26.  
    27.    
    28.     }
    29.    
    30. }
    31.  
    Camera script
    Player.cs

    Code (csharp):
    1. // Attach this script to the scene camera
    2. // Create and apply a tag labeled 'SelectableActor' to all selectable game objects;
    3. // Right mouse click to select left click to do something to them.
    4.  
    5. using UnityEngine;
    6. using System.Collections;
    7.  
    8. public class Player : MonoBehaviour
    9. {
    10.  
    11.     private Actor currentTargetedActor;
    12.  
    13.  
    14.     void Update()
    15.     {
    16.  
    17.         // Check if the user right mouse clicked
    18.         if (Input.GetMouseButtonDown(1))
    19.         {
    20.  
    21.             Debug.Log("");
    22.             Ray ray = camera.ScreenPointToRay(Input.mousePosition);
    23.             RaycastHit hitInfo;
    24.             if (Physics.Raycast(ray, out hitInfo, Mathf.Infinity))
    25.             {
    26.                 // you could just check if you have a component call 'Actor' attached instead
    27.                 // of using tags if you wanted.
    28.                 if (hitInfo.collider.tag == "SelectableActor")
    29.                 {
    30.  
    31.                     Actor selectedActor = (Actor)hitInfo.collider.gameObject.GetComponent("Actor");
    32.  
    33.                     // Check we don't already have the actor selected
    34.                     if (currentTargetedActor != selectedActor)
    35.                     {
    36.  
    37.                         if (currentTargetedActor != null) currentTargetedActor.selected = false;
    38.  
    39.                         selectedActor.selected = true;
    40.                         currentTargetedActor = selectedActor;
    41.  
    42.                     }
    43.                     Debug.Log("You have selected: " + currentTargetedActor.gameObject.name);
    44.                 }
    45.                 else
    46.                 {
    47.                     //We hit something but is wasn't a selectedable actor so deselect current actor if we have one selected.
    48.                     if (currentTargetedActor != null)
    49.                     {
    50.                         currentTargetedActor.selected = false;
    51.                         currentTargetedActor = null;
    52.                     }
    53.                 }
    54.  
    55.             }        
    56.             else
    57.             {
    58.                 //We hit nothing so deselect actor if we have one selected.
    59.                 if (currentTargetedActor != null)
    60.                 {
    61.                     currentTargetedActor.selected = false;
    62.                     currentTargetedActor = null;
    63.                 }
    64.             }
    65.  
    66.         }
    67.  
    68.         else if(Input.GetMouseButtonDown(0)  currentTargetedActor)
    69.         {
    70.             //Fire a projectile/heal an actor  
    71.     //      MainPlayer mp = (MainPlayer)gameObject.GetComponent("MainPlayer");
    72.     //      mp.Shoot();
    73.            
    74.            
    75.     //      currentTargetedActor.Actor();
    76.  
    77.            
    78.             //.Shoot();
    79.         }
    80.  
    81.     }
    82.  
    83. }
    84.  
    Player and Enemy script (enemy has the type of enemy)
    Actor.cs
    Code (csharp):
    1. // Attach to all selectable game objects
    2.  
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class Actor : MonoBehaviour {
    7.  
    8.     public enum ActorTypes {Enemy, Friend, Player};
    9.     public ActorTypes actorType;
    10.     private bool isSelected;
    11.    
    12.     // Called when selected and deselected
    13.     public bool selected
    14.     {
    15.         get { return isSelected; }
    16.         set
    17.         {
    18.             isSelected = value;
    19.         }
    20.     }
    21.  
    22.    
    23.     //Called when this actor is selected and user right mouse clicks in the scene.
    24.   public void Action()
    25.     {
    26.         switch (actorType)
    27.         {
    28.             case ActorTypes.Enemy:
    29.                
    30.                        
    31.                
    32.                 Debug.Log("Doing Something good to: " + this.gameObject.name);
    33.                 break;
    34.             case ActorTypes.Friend:
    35.                 Debug.Log("Doing Something good to: " + this.gameObject.name);
    36.                 break;
    37.            
    38.             case ActorTypes.Player:
    39.                 Debug.Log("Doing Something good to: " + this.gameObject.name);
    40.                 break;
    41.         }
    42.  
    43.     }
    44.  
    45.  
    46. }
    47.  
    project - scene _MIAN
    http://rs568.rapidshare.com/files/354785230/NEWRTS.zip
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Hi, welcome to the forum!

    I'm not quite sure what you mean. Do you want the player to turn as it is moving toward the enemy or turn to face the enemy, or do you want the enemy to face the player as it moves or... ?