I am in charge of working on the minion script. My task was to get the minion flying, hover around the character, and be able to maneuver. It has been awhile that I actually sat down and scripted in unity so it took me some time to figure out what scripts was needed.
I started by using the FPS walker script that has been prefab’d in unity.
-------------------------------
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
function FixedUpdate() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
if (Input.GetButton ("Jump")) {
moveDirection.y = jumpSpeed;
}
}
// Apply gravity
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)
----------------------------------
As I was trying to read the script, I had to look up on what some of functions were doing like FixedUpdate() which I found out that its like the regular FixedUpdate but should only be used for rigid bodies. for what the bodies starts adding force, it will count/update for every fixed frame of it.
To simplify the character controller script to use for the minion. I established the keyboard speed with var keyboardSpeed = 50; testing the script at this speed was too fast so I brought it down to 1.
From there I added function
FixedUpdate()
{
Var keyboardX = Input.GetAxis(“Horizontal”) * keyboardSpeed * Time.deltaTime;
//The script line above stats the default settings for moving on the X axis is set to default keys and run on the set keyboard speed and smoothly per frame. I completed with the rest of the moving lines:
Var keyboardZ = Input.GetAxis(“Vertical”) * keyboardSpeed * Time.deltaTime;
Var keyboardY = Input.GetKey(“Space”) * keyboardSpeed * Time.deltaTime;
//The jump script above had to be set to the default jump settings until I can figure out a more specific and manual way of scripting the jump. The script line for jump didn’t work and I had to figure out a fix which I did later.
var newPos = rigidbody.position + Vector3(keyboardX, keyboardY, keyboardZ);
//take the current rigidbody position + the changed(added) axis changes from key inputs = newPos
rigidbody.MovePosition(newPos);
//take the newPos coordinates and move the rigidbody to that location
}
@script RequireComponent(Rigidbody)
I used unity3d to look up script and reference.
The jump was tricky for me to get working and I wasn’t completely sure if we were going to use the jump but I figured I could always mod the script later to do a ascend and descend. The jump had to set its y variable and assigned an input. I couldn’t get it right, right away. I was getting a input script error for that script line. After talking to rob about it, he told me about the default jump that’s in unity.
I just had to add a variable at the top
Var Jump
Then change
Input.GetKey(“Space”)
To
Input.GetAxis(“Jump”)
Jump is the default setting for jump in unity and it works for now but the gravity is applied to the character which restricts the floating/hovering that Alex wants.
No comments:
Post a Comment