I'm having trouble getting the Input.GetButtonDown to react consistently with this script. It works, but often times it will ignore mouse clicks. I can't figure out why it is acting with such inconsistency. Here is the code I'm working off:
// *Trip* Sidescroller Script
// Based off the Unity FPS Walker
//
var speed = 6.0;
var jumpSpeed = 8.0;
var gravity = 20.0;
private var moveDirection = Vector3.zero;
private var grounded : boolean = false;
var theCamera : Camera;
var playerObj : GameObject;
var playerCoordinates;
var relative;
// Create variables for click jump
var mousePosition;
var AllowClickJump : boolean = true;
var clickJump : float = 2;
var JumpSideForce : float;
var JumpUpForce : float;
var StraightJump : float;
var airJumps;
var allowedAirJumps : int = 2;
function Awake()
{
clickJump = clickJump/10;
}
function FixedUpdate() {
if (grounded) {
// We are grounded, so recalculate movedirection directly from axes
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= speed;
}
// Mouse Jump
if (Input.GetButtonDown("Fire1") && AllowClickJump && airJumps < allowedAirJumps)
{
airJumps++;
mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
mousePosition = theCamera.ScreenToViewportPoint(mousePosition);
playerCoordinates = new Vector3(playerObj.transform.localPosition.x, playerObj.transform.localPosition.y,
playerObj.transform.localPosition.z);
pCoords = theCamera.ScreenToViewportPoint(playerCoordinates);
Debug.Log("Mouse Click: " + mousePosition + "Char Pos" + pCoords);
// Determine if the mouse click was left or right of the player
if(mousePosition.x < .4)
{
mousePosition.x = -JumpSideForce;
mousePosition.y = JumpUpForce;
}
if(mousePosition.x > .6)
{
mousePosition.x = JumpSideForce;
mousePosition.y = JumpUpForce;
}
if(mousePosition.x >.4 && mousePosition.x <.6)
{
mousePosition.y = StraightJump;
}
// Translate to moving action
moveDirection = transform.TransformDirection(mousePosition);
moveDirection = new Vector3(moveDirection.x, moveDirection.y, 0);
// Apply the jump + modifier
moveDirection *= clickJump;
} //End mouse jump
// 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;
if(grounded == true)
{
airJumps = 0;
}
}
@script RequireComponent(CharacterController)
Great answer. Thanks a ton!
– karlawarded 10 points. I was having this issue randomly, so I printed the inputs happening, and shockingly they were not all firing despite the variables they'd have to go through. Put my button inputs on an update function, and now nothing is missing its input! I would've never deduced it was fixed updates fault, so thank you very much for this insight.
– TekksinI was having this same issue where it would only work once in about every 15 tries but now it is working great
– Foxsarethbest10