Search Unity

Simple controller code for moving around

Discussion in 'Scripting' started by Crayox, Dec 21, 2014.

  1. Crayox

    Crayox

    Joined:
    Dec 21, 2014
    Posts:
    9
    Hi guys,
    I have been searching for the simplest code to control my camera/character but couldn't find it.

    I'm a 3d visualization artist so this is very new to me. I just need my camera to walk around my interiors or exteriors and be controlled by mouse for panning and "wasd" for moving.

    Here are my attempts...
    Code (CSharp):
    1. float rotationMouseX, rotationMouseY;
    2.  
    3.         rotationMouseX = Input.GetAxis ("Mouse X") * sensitivity;
    4.         rotationMouseY = Input.GetAxis ("Mouse Y") * sensitivity;
    5.  
    6.         rotationX += rotationMouseX;
    7.         rotationY += rotationMouseY;
    8.  
    9.         float movementForward = Input.GetAxis("Vertical") * speed;
    10.         float movementSide = Input.GetAxis("Horizontal") * speed;
    11.         Vector3 totalMovement = new Vector3(movementSide,0.0f, movementForward);
    12.  
    13.  
    14.         rigidbody.rotation = Quaternion.Euler (-rotationY, rotationX,0.0f);
    15.         //transform.Translate (movementSide,0.0f,movementForward);
    16.         rigidbody.velocity= totalMovement;
    17.         //rigidbody.velocity = new Vector3 (0.0f, 0.0f,movementSide);
    I tried several ways, and finally translate.forward for rigidbody.velocity made my camera go where it was pointed at, but couldn't use that for strafing with "a,d" or left/right arrows. Also if I use transform.translate I can't use collisions as I understand, and I need those too.

    For my character I am using a camera with capsule collider and rotation restraints so it doesn't fall over - is that the best way for this? I need collisions, as sometimes I'll have exteriors and need my character go up and down the street, or up the stairs etc.

    Thanks in advance.
     
  2. MathiasDG

    MathiasDG

    Joined:
    Jul 1, 2014
    Posts:
    114
    Code (CSharp):
    1.  
    2.         float speed = 5;
    3.         Vector3 position = transform.position;                                  //  transform current position
    4.         Vector3 forward = transform.forward;                                    //  3d direction the transform is looking at.
    5.         Vector3 right = new Vector3(forward.z, forward.y, -forward.x);          //  2d clockwise perpendicular vector to (x, 0, z)
    6.         Vector3 left = new Vector3(-forward.z, forward.y, forward.x);           //  2d counterclockwise perpendicular vector to (x, 0, z)
    7.         transform.position = position + forward * speed * Time.deltaTime;       //  moving forward
    8.         transform.position = position + right * speed * Time.deltaTime;         //  moving to the right
    9.         transform.position = position + left * speed * Time.deltaTime;          //  moving to the left
    These perpendicular vectors are 2d perpendicular, wich means they will move to the right /left on the xz plane but keep moving up/down on the y axis accordingly to the transform.forward.
    You could set the y coordinate of the right / left to 0 if you wanted to.
    Or you could try and calculate the 3d perpendicular vector to the forward, but that will only make a difference if your character can rotate around freely on the 3d space.
     
  3. toreau

    toreau

    Joined:
    Feb 8, 2014
    Posts:
    204
    Both a first and a third person controller is included with Unity.
     
  4. Crayox

    Crayox

    Joined:
    Dec 21, 2014
    Posts:
    9
    Thanks guys! Will check out what you wrote MathiasDG, and toreau - I thought it would be but I guess I'm jumping ahead of myself being excited about all this! :)
    Not sure how to find this, will look further :)
     
  5. unity_Z2QusjT3x3iwFQ

    unity_Z2QusjT3x3iwFQ

    Joined:
    Jan 23, 2022
    Posts:
    7
    Hello!

    I hope it´s okay to borrow this thread because I have similar problems when it comes to have a working "look around" function.

    I´m following the coding for this step by step in this Youtube video:



    The video also show how to get a "walk around" function but I have not come to that part yet.
    So:

    What I´ve done so far is to follow the video showing the coding for the "look around" function and I now have this code:

    Code.JPG

    What I can see it is exact the same code as in the video but I get these errors that makes it only possible to enter my Unity project in Safe Mode:

    Code Error.JPG

    What have I done wrong and how do I fix it so I can have this "look around" function?
    (It would have been nice if the look around and move around function were ready to use in Unity for simply just add for a project.)

    Thanks in advance!
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    To be honest, it's not really okay necroing a post from 2014 for your own problem. Please just create your own thread, it's free and simple. Also, when posting code, please use code-tags; using images means someone has to type out code to refer to it or make corrections for you.

    In the end, you've created a necro post for a typo which should really be easy to see if I'm honest. :(

    Here:
    Input GetAxis

    GetAxis is a method of Input. You've just put a space in which is wrong. The error tells you which lines the problem is on so a quick look at the video (7:28) would show you the problem. This would save a lot of work bothering with the forums. :(



    But why? This is a super simple, even trivial piece of code. Why would we ship it by default? To save you learning C# and basic input? This isn't as generic as you're inferring.
     
  7. unity_Z2QusjT3x3iwFQ

    unity_Z2QusjT3x3iwFQ

    Joined:
    Jan 23, 2022
    Posts:
    7
    Thank you for your quick reply and comments.

    First of all, I borrowed the thread because in other forums it´s quite common to see comments about people starting new threads with the same questions that already have been discussed earlier. Therefore I thought it would be better to connect my question in this thread. But enough about that.

    Okay I´ll have a look again and compare the code in the video to my own. Thanks!
     
  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,497
    The problem with necroing is that "the same problem" isn't the same problem. It's often the same error which can have hundreds of different root causes or "it sounds similar to my problem" and we just end up with threads that span many, many years where lots of posts have nothing whatsoever to do with each other.

    "Linking your post" doesn't help anyone if the last post was 7 years ago and likely completely outdated anyway even if it were.

    Yours is a classic example of "same problem" when it's not at all.
     
  9. unity_Z2QusjT3x3iwFQ

    unity_Z2QusjT3x3iwFQ

    Joined:
    Jan 23, 2022
    Posts:
    7
    Will think about it! Thanks!