I am fiddling with my character controller.
When grounded: take vector x,y,z
When not grounded (e.g jumping and falling) it should only take the X and Z.
This way, the character respond to the joystick while jumping too.
…But I need to retrieve the Y vector as well
E.g:
moveDirection = new Vector3(Input.GetAxis("Horizontal"), CURRENT_Y_SPEED_HERE, Input.GetAxis("Vertical"));
If the player is falling down very hard, the current Y would be -20 or something, so how do I capture this “y velocity property”?
full code:
var speed = 30.0;
var gravity = 1.0;
var jumpSpeed = 8.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
private var difference : float = 0.0;
private var lastPosition = Vector3.zero;
private var thisPosition = Vector3.zero;
// -----------------------------------------------------------------------------------------
function Update () {
thisPosition = transform.position;
if (!grounded) {
// THIS IS THE CODE THAT DOESNT WORK YET
moveDirection.x = Input.GetAxis("Horizontal");
moveDirection.z = Input.GetAxis("Vertical");
MD = Camera.mainCamera.transform.TransformDirection(moveDirection);
moveDirection.x = MD.x *= speed;
moveDirection.z = MD.z *= speed;
}
else if (grounded) {
// THIS WORKS FINE
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = Camera.mainCamera.transform.TransformDirection(moveDirection);
moveDirection *= speed;
moveDirection.y = 0;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// hier eventuele code voor jumpen
moveDirection.y -= gravity * Time.deltaTime;
// Move the controller
var controller : CharacterController = GetComponent(CharacterController);
var flags = controller.Move(moveDirection * Time.deltaTime); // verplaats de controller, return flag waarden voor collision detection
grounded = (flags CollisionFlags.CollidedBelow) != 0;
lastPosition = transform.position;
}
Thanks.
Indeed that is invalid syntax. It didn’t change the behaviour though, so I guess Unity seems to handle it well.
What I basicly want to do is exactly the same code for grounded and !grounded, but not use 0 for the Y-ax. In that situation I want the current Y velocity of the character and pass it to the moveDirection vector.
If I do use 0 for the Y-ax, the character will not jump because a soon as he is airborne, he wont jump any further because of the moveDirection Y = 0.
E.g (pseudo code):
// NOT GROUNDED (IN AIR, JUMPING OR FALLING DOWN):
moveDirection = new Vector3(Input.GetAxis("Horizontal"), CURRENT Y VELOCITY, Input.GetAxis("Vertical"));
// GROUNDED (WALKING):
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
I tried to use (thisPosition.y - lastPosition.y) to get the current velocity, but it causes the character to eject to the moon
// THIS IS THE CODE THAT DOESNT WORK YET
var MD = Vector3 (Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
MD = Camera.mainCamera.transform.TransformDirection(moveDirection); moveDirection.x = MD.x * speed;
moveDirection.z = MD.z * speed;
Try this. (This doesn’t maintain the y velocity when calculating MD, which is what you want)
Apart from that i cant really see what is wrong with the script without a running example.
Thanks for your patience. I have implemented your code.
This is the actual demo (but be warned, it looks totally chaotic): http://www.stickystudios.com/etc/har.html
I am not sure what is wrong with this code, it looks okay .
var speed = 30.0;
var gravity = 1.0;
var jumpSpeed = 8.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
private var difference : float = 0.0;
// -----------------------------------------------------------------------------------------
function Update () {
if (grounded) {
// THIS WORKS FINE
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = Camera.mainCamera.transform.TransformDirection(moveDirection);
moveDirection *= speed;
moveDirection.y = 0;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
else if (!grounded) {
// THIS IS THE CODE THAT DOESNT WORK YET
var MD = Vector3 (Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
MD = Camera.mainCamera.transform.TransformDirection(MD);
// feed x and z to the original moveDirection vector, but leave Y alone.
moveDirection.x = MD.x * speed;
moveDirection.z = MD.z * speed;
}
// perform gravity
moveDirection.y -= gravity * Time.deltaTime;
var controller : CharacterController = GetComponent(CharacterController);
// Move the controller
var flags = controller.Move(moveDirection * Time.deltaTime); // verplaats de controller, return flag waarden voor collision detection
// check what the controller returned and set the grounded value to true or false depending on collision.
grounded = (flags CollisionFlags.CollidedBelow) != 0;
}