- Home /
Hey Guys, I am trying to get my player to move when I push this button but it keeps saying that it cannot convert bool to float. Please help me!
using UnityEngine; using System.Collections;
public class right4 : MonoBehaviour {
public float speed;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnGUI()
{
if (GUI.RepeatButton(new Rect(500, 200, 50, 50), "→"))
{
float moveHorizontal = Input.GetButton("right4");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, 0.0f);
rigidbody.AddForce (movement * speed * Time.deltaTime);
}
}
}
Answer by HuskyPanda213 · Sep 21, 2014 at 05:36 PM
The error is in the code: float moveHorizontal = Input.GetButton("right4"); what it is saying is it cannot convert the floating-point number type to a boolean. Input.GetButton() returns a boolean, not a float--Input.GetAxis() returns a float though. This code should work:
using UnityEngine;
using System.Collections;
public class right4 : MonoBehaviour {
public float speed;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnGUI()
{
if (GUI.RepeatButton(new Rect(500, 200, 50, 50), "→"))
{
float moveHorizontal = Input.GetAxis("Horizontal");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, 0.0f);
rigidbody.AddForce (movement * speed * Time.deltaTime);
}
}
}
Ok, I added that code but now the button will not move the player
Your answer
Follow this Question
Related Questions
InvalidCastException: Cannot convert from source type to destination type using a Rigidbody2D 2 Answers
Rigidbody makes object "fly" when moving up mountain 0 Answers
'GetAxis' is not a member of 'function(): void' 1 Answer
How to let a GameObject generate force but not be affected by certain forces? 0 Answers
Rigidbody climbing stairs 2 Answers