Search Unity

moving camera with mouse [NEED HELP]

Discussion in 'Scripting' started by markis, Nov 19, 2010.

  1. markis

    markis

    Joined:
    Oct 11, 2010
    Posts:
    51
    Hello, i want to create camera moving like in facebook game "FarmVille". Can someone give me a tip or link to tutorial how to create it?
     
  2. Metal Head

    Metal Head

    Joined:
    Aug 14, 2010
    Posts:
    411
    Here's a pretty simple way to do it:

    Code (csharp):
    1.  
    2. if(Input.GetKey("a")) transform.position.x -= Time.deltaTime * 100;
    3. if(Input.GetKey("d")) transform.position.x += Time.deltaTime * 100;
    4.  
    Basically the idea is to modify the camera's transform.position. You can do it with several functions, that was just a direct variable manipulation. I wrote it like that,because I tought it should be easier to understand.

    Check the Transform and the Input class, you should find better methods there :)

    And I know it's not with the mouse, you could use Input.GetAxis("MouseX") for example and sum it with the transform.position in this case.
     
  3. markis

    markis

    Joined:
    Oct 11, 2010
    Posts:
    51
    u mean like this:

    Code (csharp):
    1. function Update () {
    2.     var h : float = Input.GetAxis ("Mouse X");
    3.     var v : float = Input.GetAxis ("Mouse Y");
    4.     transform.Translate (v, h, 0);
    5. }
     
    Last edited: Nov 19, 2010
  4. markis

    markis

    Joined:
    Oct 11, 2010
    Posts:
    51
    Ok. I tested and thats the code:
    Code (csharp):
    1. var speed : float = 2.0;
    2.  
    3. function Update () {
    4.     if(Input.GetAxis("Fire1"))
    5.     {
    6.     var h : float = speed * Input.GetAxis ("Mouse X");
    7.     var v : float = speed * Input.GetAxis ("Mouse Y");
    8.     transform.Translate (h, 0, v);
    9.     }
    10. }
    Thanks for the help :)