Hello All,
I’ve been playing with my Unity again and somehow can’t understand what’s going on.
I tried changing the FPS script so that the keyboard input axis(Horizontal) will rotate instead of moving sideways.
Using the simple example from the doc’s I thought I got it working the way I wanted it, but I’m running into a problem that I can’t figure out. I even tried wearing my unify dev hoodie hoping it will help me figure this one out but had no luck.
Can someone “virtually” hit me up the side of my head and help me figure this out.
Using the FPS script;
-
The player can still move forward or sideways while pressing the jump button.(can hop like a wabbit or a kermit)
-
The player will collide with any collider and the collider(a cube or a sphere) will not move(can not be pushed).
This is perfect except that I don’t want to use the mouse for my rotation.
Using the keyboard script;
-
The player can not move forward or rotate once the jump button is press. It will only move again when the jump button is not press( can not hop forward).
-
The player will collide with any collider but it’s pushing any collider that it collides with.
I’m attaching the FPS script and the KeyboardRotation script.
//The FPS Script(standard)
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;
}
function Awake ()
{
rigidbody.freezeRotation = true;
var controller : CharacterController = GetComponent(CharacterController);
if (!controller)
gameObject.AddComponent("CharacterController");
}
//Keyboard script with rotation on horizontal
/// This script moves the character controller forward
/// and rotate left or right based on the arrow keys.
/// It also jumps when pressing space.
/// Make sure to attach a character controller to the same game object.
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
// added for keyboard rotation(horizontal)
var rotationspeed = 100.0;
function Awake()
{
rigidbody.freezeRotation = true;
}
function FixedUpdate()
{
if (grounded)
{
// We are grounded, so recalculate
// move direction directly from axes
var translation = Input.GetAxis("Vertical") * speed;
var rotation = Input.GetAxis("Horizontal") * rotationspeed;
translation *= Time.deltaTime;
rotation *= Time.deltaTime;
transform.Translate(0,0,translation);
transform.Rotate(0,rotation,0);
if (Input.GetButton ("Jump"))
{
moveDirection.y = jumpSpeed;
}
//not moving when airborne, this is what I've been trying to do to make it hop
/*else if(!grounded)
{
//var translationair = Input.GetAxis("Vertical") * speed;
//var rotationair = Input.GetAxis("Horizontal") * rotationspeed;
//translation *= Time.deltaTime;
//rotation *= Time.deltaTime;
//transform.Translate(0,0,translation);
//transform.Rotate(0,rotation,0);
//moveDirection = Vector3(translation,0,rotation);
//moveDirection = transform.TransformDirection(moveDirection);
//moveDirection *=speed;
rigidbody.AddForce (Vector3.forward*translation);
rigidbody.velocity.z = translation;
}*/
}
// 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;
}
Any help is appreciated.
Thanks,
Ray