Hello! Im new to unity and C# and Im making the movement script for my player in the game. BUT it gives me this error:
Operator ‘||’ cannot be applied to operands of type ‘bool’ and ‘float’.
I don’t know how to fix it. The error is at this line:
if(moveHorizontal > 0.1f || moveHorizontal < -0.1f)
Thank you if someone responds.
Could you please post a code snippet of your class? It will be helpful to chase down a bug
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
private Rigidbody2D rb2D;
private float moveSpeed;
private float jumpForce;
private bool isJumping;
private float moveHorizontal;
private float moveVertical;
// Start is called before the first frame update
void Start()
{
rb2D = gameObject.GetComponent();
moveSpeed = 3f;
jumpForce = 60f;
isJumping = false;
}
// Update is called once per frame
void Update()
{
moveHorizontal = Input.GetAxisRaw(“Horizontal”);
moveVertical = Input.GetAxisRaw(“Vertical”);
}
void FixedUpdate()
{
if (moveHorizontal > 0.1f || moveHorizontal < -0.1f)
{
rb2D.AddForce(new Vector2(moveHorizontal * moveSpeed, 0f), ForceMode2D.Impulse);
}
}
}
Please use code blocks.
Anyway, it looks good, is it possible that you didn’t save changes in your code?
Agreed, the above looks fine.
You can make it “read” a little better by changing the magnitude check near the bottom to be:
if (Mathf.Abs( moveHorizontal) > 0.1f)
{
.. etc.
That more clearly says “is magnitude bigger than 0.1?”