[Help] Event Trigger buttons not working?
I have a left/right button for my 2D platformer, but they're not working..
Here's my Event Trigger for one of the buttons:
I'm pretty sure these are set right, and here's my PlayerController script:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public float speed = 10, jumpVelocity = 10;
public LayerMask playerMask;
public bool canMoveInAir = true;
Transform myTrans, tagGround;
Rigidbody2D myBody;
bool isGrounded = false;
float hInput = 0;
void Start ()
{
// myBody = this.rigidbody2D;//Unity 4.6-
myBody = this.GetComponent<Rigidbody2D>();//Unity 5+
myTrans = this.transform;
tagGround = GameObject.Find (this.name + "/tag_ground").transform;
}
void FixedUpdate ()
{
isGrounded = Physics2D.Linecast (myTrans.position, tagGround.position, playerMask);
#if !UNITY_ANDROID && !UNITY_IPHONE && !UNITY_BLACKBERRY && !UNITY_WINRT || UNITY_EDITOR
Move(Input.GetAxisRaw("Horizontal"));
if(Input.GetButtonDown("Jump"))
Jump();
#else
Move (hInput);
#endif
}
void Move(float horizonalInput)
{
if(!canMoveInAir && !isGrounded)
return;
Vector2 moveVel = myBody.velocity;
moveVel.x = horizonalInput * speed;
myBody.velocity = moveVel;
}
public void Jump()
{
if(isGrounded)
myBody.velocity += jumpVelocity * Vector2.up;
}
public void StartMoving(float horizonalInput)
{
hInput = horizonalInput;
}
}
I don't know what's wrong, they still don't respond. The jump button works, but not left/right. Is it an error in my code? Please help! >.<
Answer by IHackedDeath · Aug 31, 2015 at 01:59 PM
Hi BradenInman,
I have looked through your code and I am not sure why you call the Move Function twice in the fixed update function, maybe one is colliding with the other and causing an issue.
Also I am not sure but I think canMoveInAir is always equal to true therefore the function stops straight away, maybe if you tried some debug.log's in those areas you could see where the error is occuring exactly and maybe removing the if (!canMoveInAir && !isGrounded) and possible using another if statement that works will help to solve the problem.
I think maybe isGrounded also may be causing a problem but I will need to check code further and test for myself but I thought I would try help before any of that :)
Hope this helps.
Kind Regards,
IHackedDeath.
@IHackedDeath Hmmm, I tried removing a few of the things you mentioned, and everything else still works fine. But the left/right buttons are still disfunctional.. :/
Have you tried using some debug.log's in some of your lines to see where the code is or isn't working?
Sometimes there is a unusual error that gets looked over that a debug.log can solve.
I would try in the move function, place a debug.log just under the if statement and one just after the return.
If the first debug.log is called to the console then it is something wrong with the if statement, but if the second debug.log is called to the console then there is something else wrong with the code somewhere.
Your answer
Follow this Question
Related Questions
Does the sequence in which I keep the objects in a scene matter really? 1 Answer
How can I generate and setup buttons via code 1 Answer
How to use the name of the button as a value of a variable? 0 Answers
How do I control my car with UI buttons? 0 Answers
UI Button click event through script 1 Answer