I am trying to teach myself to use unity by creating a basic space sim type game. At the moment, I’m simply trying to make the game allow the player to fly around. So working from the FPS tutorial, I have created a capsule with a camera an the various scripts, and then started editing the FPSWalker script to try to make it move in 3d.
Heres what I have at the moment:
var speed = 6.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function FixedUpdate() {
if (Input.GetButton ("Fire1")) {
moveDirection = Vector3(Input.GetAxis("Mouse x"), Input.GetAxis("Mouse y"), Input.GetAxis("Mouse z"));
}
// Comment applying gravity as we don't want any
//moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags CollisionFlags.CollidedBelow) != 0;
}
@script RequireComponent(CharacterController)
The intention is that this should cause the character to move in whatever direction they are facing when the left ctrl button is pressed. This doesn’t work, the character sits and spins happily, but won’t move.
Can someone suggest where I’m going wrong? I’m guessing I’ve misunderstood what 1 or more functions do, but I’m not sure which.
First of all, you have miss matching braces in your code which will cause a lot of problems. I recommend swapping from using opening braces on the same line as the declaration to the line below.
From
if (true) {
// code
}
To
if (true)
{
// code
}
It makes it so much easier to follow program flow as well as identify brace problems (note: After writing this post and reading yours several times over I see there is no brace problem as far as the code goes but the offset braces and inconsistent tabs made the code very hard to follow, I still recommend using the other brace format)
As for your other problem, What you want to do is have the character always move along a single axis relative to the object. Each object in the world has their own XYZ axis that is independent to the world XYZ called it’s local coordinates/axis. What you want to do is rotate the object around it’s local axis so that a single axis (Lets say Z axis in this case for the sake of the example) is always pointing ‘forwards’ relative to the object.
When you move the mouse you need to use the rotate function (or rotate.euler, I don’t have the API in front of me) to rotate the object. After you do that you then need to move the object ‘forwards’ when the key is hit. I haven’t written this type of code in Unity in a while so I am not going to embarrass myself by attempting it (When I did it I had a default camera class that took care of the rotation of the character which meant I only had to worry about moving the character along the axis).
Hello Link! First, there is no such thing as “Mouse z”, unless you’ve defined it in your code. Then, as I understand it, you want to make you character move in one direction: forward. But you can rotate so that this “forward” will be in another direction, right?
So you will want to rotate around the y-axis to look to the right/left and all around.
Then there’s a Vector3 variable which points at an object’s local forward, it’s defined as Vector3.forward.
You can use it to move in the direction your object is facing, which can be done in several ways but I tend to use the function transform.TransformDirection(…), you may want to look it up.
I hope that will lead you to the right direction, good luck!
/Kweiko
EDIT: oh Scared has allready replied, I writing the reply while testing the idea so I didn’t update the page
var speed = 6.0;
private var moveDirection = Vector3.zero;
function FixedUpdate()
{
if (Input.GetButton ("Fire1"))
{
moveDirection = Vector3.forward;
}
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime);
grounded = (flags CollisionFlags.CollidedBelow) != 0;
}
@script RequireComponent(CharacterController)
I’m not at home at the mo, but will be testing this as soon as I can. I looked at Vector3.forward previously and took it to mean that it would simply move the character along the x axis (description says “Shorthand for writing Vector3(0, 0, 1)” - does it automatically set the vectors to all the axis, of will this just move it along the x axis?
Edit - Also, would I need to set the movedirection varialbe back to Vector3.zero when the key is not being pressed? I’m assuming the script is run once a frame, so will automatically go back to Vector3.zero the next frame after the key is released?
Hello again, for some reason I don’t get emails whenever a watched thread is updated anymore, and I couldn’t find this thread for some time. Hope all worked out fine anyway.
Vector3(0, 0, 1) is only in the z-axis (the different axises come always in this order: (x, y, z)).
You’ve surely figured it out by now, but I think resetting moveDirection to Vector3.zero is necessarily. It can’t go back to it’s initial value since you only say “if Fire1 is pressed, moveDirection’s new value will be vector3.forward”, nothing about “else, let it’s value be …” or something similar.