Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

RTS Like Camera Moves?

Discussion in 'Scripting' started by AGhost, May 2, 2006.

  1. AGhost

    AGhost

    Joined:
    Apr 13, 2006
    Posts:
    90
    Just playing around with a camera script today and I found I needed help.
    I am trying to get an RTS like movement with the camera.

    Can someone help me so that the camera only moves if the player's mouse is near the edge of the screen?

    Here's my code:

    Code (csharp):
    1. var speed = 6.0;
    2. private var moveDirection = Vector3.zero;
    3.  
    4. function FixedUpdate() {
    5.  
    6.     moveDirection = new Vector3(Input.GetAxis("Mouse Y")*10, 0, -Input.GetAxis("Mouse X")*10);
    7.     moveDirection = transform.TransformDirection(moveDirection);
    8.     moveDirection *= speed;
    9.    
    10.     // Move the controller
    11.     var controller : CharacterController = GetComponent(CharacterController);
    12.     controller.Move(moveDirection * Time.deltaTime);
    13. }
    14.  
    15. function Awake ()
    16. {
    17.     var controller : CharacterController = GetComponent(CharacterController);
    18.     if (!controller)
    19.         gameObject.AddComponent("CharacterController");
    20. }
    21.  
    Can anyone help or give any suggestions on how to do this?

    Thanks

    Ghost
     
  2. hsparra

    hsparra

    Joined:
    Jul 12, 2005
    Posts:
    750
    How about this brute force approach?
    Code (csharp):
    1.  
    2. var speed = 1.0;
    3. var sensitivity = 10.0;   // Number of pixels from edge
    4. private var moveDirection = Vector3.zero;
    5.  
    6. function FixedUpdate() {
    7.  
    8.    mousePos = Input.mousePosition;
    9.    moveX = 0.0;
    10.    moveY = 0.0;
    11.  
    12.    if (mousePos.x <= sensitivity) {
    13. //        moveX = Input.GetAxis("Mouse X")*10;
    14.         moveX = 1.0 * speed;
    15.     }
    16.     if (mousePos.x >= Screen.width - sensitivity) {
    17.         moveX = -1.0 * speed;
    18.     }
    19.     if (mousePos.y <= sensitivity) {
    20.         moveY = 1.0 * speed;
    21.     }
    22.     if (mousePos.y >= Screen.height - sensitivity) {
    23.         moveY = -1.0 * speed;
    24.     }
    25.  
    26.    moveDirection = new Vector3(moveY, 0, -moveX);
    27.    // Translating so move relative to where the camera currently is located
    28.     transform.Translate(moveDirection);
    29.    moveDirection *= speed;
    30.    
    31.    // Move the controller
    32.    var controller : CharacterController = GetComponent(CharacterController);
    33.    controller.Move(moveDirection * Time.deltaTime);
    34. }
    35.  
    36. function Awake ()
    37. {
    38.    var controller : CharacterController = GetComponent(CharacterController);
    39.    if (!controller)
    40.       gameObject.AddComponent("CharacterController");
    41. }
    42.  
     
  3. AGhost

    AGhost

    Joined:
    Apr 13, 2006
    Posts:
    90
    Thanks,

    With a little tweaking, it works!

    Ghost