Search Unity

Camera Movement

Discussion in 'Scripting' started by shawnpigott, Sep 28, 2005.

  1. shawnpigott

    shawnpigott

    Joined:
    Sep 28, 2005
    Posts:
    262
    I am writing a game for a young man with cerebral palsy. He can interact with the computer using two switches that I have mapped to the 'a' and 'b' keys.

    I want to have the character of the game move foreword a grid space (an arbitrary amount as he may or may not gold down the switch) when he hits the 'a' switch and have the character move 90 degrees to the right when he hits the 'b' switch. The camera needs to follow the character from above or behind. The character needs to follow the ground in an attempt to get out of a maze.

    I have tried to do this myself and have have no success. Any advice would be appreciated. If you need any further information please email me at:
    shawn@ampertec.com

    Thanks
     
  2. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
    I can help you with the basic movement part ... create a script with this in it and save it as MoveForward.js. Then create a new tag in the Inspector and call it "Character". Select the charcater object and assign it this tag in the top panel of the Inspector.

    Code (csharp):
    1.  
    2. // a script that moves a character a set amount forward
    3. // when the mouse button is pressed
    4.  
    5. function OnMouseDown ()
    6. {
    7.     target = GameObject.FindGameObjectWithTag ("Character").transform;
    8.     target.transform.localPosition.z += 10.0;
    9. }
    10.  
    A simple example of the camera following can be found in the second Scripting tutorial on pages 16-17. It's exactly what you want.

    Replace 10 with whatever distance you need. But this will work on the click down, maybe is should be on the release of the button? Do you want the 90° right movement to be proceeded by a turn to that direction then a move? If not, just change the z in the above code to x.

    Feel free to email me as well.
     
  3. shawnpigott

    shawnpigott

    Joined:
    Sep 28, 2005
    Posts:
    262
    Thanks for the response.

    I need the foreword movement of the character to be controlled by an 'a' keystroke.

    The 'b' keystroke needs to rotate the character 90 degrees to the right.

    I believe that the combination of the two keystrokes should let my friend navigate (although slowly if he needs to turn left) around the game I hope to make for him.

    I'll check out the camera follow example you suggested.

    Thanks,

    Shawn
     
  4. DaveyJJ

    DaveyJJ

    Joined:
    Mar 24, 2005
    Posts:
    1,558
  5. David-Helgason

    David-Helgason

    Moderator

    Joined:
    Mar 29, 2005
    Posts:
    1,104
    Very nice project. Try this:

    Code (csharp):
    1.  
    2. var walkStep = 10.0;
    3. var angleStep = 90.0;
    4. function Update () {
    5.     if (Input.GetKeyDown("a")) {
    6.         transform.position += transform.TransformDirection(Vector3.fwd * walkStep);
    7.     }
    8.     if (Input.GetKeyDown("b")) {
    9.         transform.RotateAround(transform.position, Vector3.up, angleStep);
    10.     }
    11. }
    12.  
    Create this JavaScript as RotateAndWalk.js (or something like that) and drag it onto any object. That object should now move around when you press A and B.

    The script has two properties which you can edit in the inspector to tune the movement (step size and rotation amount).

    Hope it makes sense.

    d.
     
  6. shawnpigott

    shawnpigott

    Joined:
    Sep 28, 2005
    Posts:
    262
    Thanks for the help. It's working as I had hoped.

    Shawn
     
  7. David-Helgason

    David-Helgason

    Moderator

    Joined:
    Mar 29, 2005
    Posts:
    1,104
    How delightful :)