M
M
Max Bogachov2017-03-18 12:18:54
2D
Max Bogachov, 2017-03-18 12:18:54

How to fix CS0019?

Guys, here is an error (+ such a current line is different)

Assets/Scripts/moving.cs(23,15): error CS0019: Operator `<' cannot be applied to operands of type `UnityEngine.Vector3' and `float'

I understood what it asks, only in this code it worked
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class moving : MonoBehaviour {
  private float speed = 5f;
  private float jumpForce = 15F;
  private Rigidbody2D rigidbody;
  private SpriteRenderer sprite;
  private bool isGrounded;

  private void Awake(){
    rigidbody = GetComponent<Rigidbody2D> ();
    sprite = GetComponent <SpriteRenderer> ();
  }

  private void Run(){
    Vector3 direction = transform.right * Input.GetAxis("Horizontal");
    transform.position = Vector3.MoveTowards (transform.position, transform.position + direction, speed * Time.deltaTime);
    sprite.flipX = direction.x < 0.0f;
  }

  private void Jump(){
    rigidbody.AddForce (transform.up * jumpForce, ForceMode2D.Impulse);
  }

  private void CheckGrounded(){
    Collider2D[] colliders = Physics2D.OverlapCircleAll (transform.position,0.3f);

    isGrounded = colliders.Length > 1;			
  }

  private void FixedUpdate(){
    CheckGrounded ();
  }

  private void Update () {
    if (Input.GetButton ("Horizontal"))
      Run ();
    else if (isGrounded && Input.GetButtonDown ("Jump"))
      Jump();
  }
}

And this is just a mistake.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class moving : MonoBehaviour {
  public GameObject obj;
  private float speed = 5f;
  private Rigidbody2D rb;
  private SpriteRenderer spr;

  private void Awake(){
    rb = GetComponent <Rigidbody2D> ();
    spr = GetComponent <SpriteRenderer> ();
  }

  private void RunX(){
    Vector3 directionX = transform.right * Input.GetAxis("Horizontal");
    spr.flipX = directionX < 0.0f;
    transform.position = Vector3.MoveTowards (transform.position, transform.position + directionX,speed * Time.deltaTime);
  }
  private void RunY(){
    Vector3 directionY = transform.up * Input.GetAxis ("Vertical");
    spr.flipY = directionY < 0.0f;
    transform.position = Vector3.MoveTowards (transform.position, transform.position + directionY,speed * Time.deltaTime);
  }

  private void Update () {
    RunY ();
    RunX ();
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nexeon, 2017-03-18
@Geekinder

Stitches

spr.flipX = directionX < 0.0f;
spr.flipY = directionY < 0.0f;

Replaced by:
spr.flipX = directionX.x < 0.0f;
spr.flipY = directionY.y < 0.0f;

T
TheTalion, 2017-03-18
@TheTalion

You are trying to compare different types. Well it's written right there! You are comparing Vector3 to float. Maybe you wanted to compare some specific vector from Vector3? In Google, all these errors are described in full by the name of the error (CS0019).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question