Simple controller code for moving around

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…

float rotationMouseX, rotationMouseY;

        rotationMouseX = Input.GetAxis ("Mouse X") * sensitivity;
        rotationMouseY = Input.GetAxis ("Mouse Y") * sensitivity;

        rotationX += rotationMouseX;
        rotationY += rotationMouseY;

        float movementForward = Input.GetAxis("Vertical") * speed;
        float movementSide = Input.GetAxis("Horizontal") * speed;
        Vector3 totalMovement = new Vector3(movementSide,0.0f, movementForward);


        rigidbody.rotation = Quaternion.Euler (-rotationY, rotationX,0.0f);
        //transform.Translate (movementSide,0.0f,movementForward);
        rigidbody.velocity= totalMovement;
        //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.

        float speed = 5;
        Vector3 position = transform.position;                                  //  transform current position
        Vector3 forward = transform.forward;                                    //  3d direction the transform is looking at.
        Vector3 right = new Vector3(forward.z, forward.y, -forward.x);          //  2d clockwise perpendicular vector to (x, 0, z)
        Vector3 left = new Vector3(-forward.z, forward.y, forward.x);           //  2d counterclockwise perpendicular vector to (x, 0, z)
        transform.position = position + forward * speed * Time.deltaTime;       //  moving forward
        transform.position = position + right * speed * Time.deltaTime;         //  moving to the right
        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.

Both a first and a third person controller is included with Unity.

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! :slight_smile:
Not sure how to find this, will look further :slight_smile:

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:

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:

7845414--994797--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!

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. :frowning:

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. :frowning:

[quote="unity_Z2QusjT3x3iwFQ, post:5, topic: 561019, username:unity_Z2QusjT3x3iwFQ"] 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.) [/quote]
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.

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!

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.

Will think about it! Thanks!