Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Syntax acting strange for 2D movement controller

Discussion in 'Scripting' started by bwool, Jun 27, 2023.

  1. bwool

    bwool

    Joined:
    Mar 30, 2023
    Posts:
    20
    Apologies if this is a dumb question. I'm a very new developer.

    I'm trying to create a simple movement script for units in a tower defense style game, but am running into issues from the very beginning with syntax around the if statement.

    Any advice on how to resolve or best go about my task would be greatly appreciated!

    Here is a copy of my code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class UnitMovement : MonoBehaviour
    6. {
    7.     public float speed;
    8.     public int distance;
    9.     private Rigidbody2D rb;
    10.     private Vector2 moveAmount;
    11.  
    12.     private void Start()
    13.     {
    14.         rb = GetComponent<Rigidbody2D>();
    15.     }
    16.  
    17.     private void Update()
    18.     {
    19.         Vector2 moveInput = new Vector2(1, 0);
    20.         moveAmount = moveInput * speed;
    21.     }
    22.  
    23.     private void FixedUpdate()
    24.     {
    25.         if (rb.position != rb.position + new Vector2(distance, 0)
    26.         {
    27.             rb.MovePosition(rb.position + moveAmount * Time.fixedDeltaTime);
    28.         }
    29.     }
    30. }
    Here is the error I'm getting in Visual Studio:

    Screenshot 2023-06-27 at 3.53.26 PM.png
     
  2. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    107
    It looks like you're missing an extra closing parentheses at the end of line 25.

    Every ( must be followed up with a ). If you look closely at line 25, you'll see that there are two opening parentheses versus just a single closing one.
     
    Bunny83, bwool and MaxwellTan like this.
  3. bwool

    bwool

    Joined:
    Mar 30, 2023
    Posts:
    20
    Boy that's embarrassing. Thank you for your response.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,954
    Before you go any further, testing floating point numbers for equality (what you are doing comparing above, comparing vector positions), whether or not you get the parenthesis correct, is always a recipe for disaster.

    Floating (float) point imprecision:

    Never test floating point (float) quantities for equality / inequality. Here's why:

    https://starmanta.gitbooks.io/unitytipsredux/content/floating-point.html

    https://forum.unity.com/threads/debug-log-2000-0f-000-1f-is-200.1153397/#post-7399994

    https://forum.unity.com/threads/why-doesnt-this-code-work.1120498/#post-7208431

    "Think of [floating point] as JPEG of numbers." - orionsyndrome on the Unity3D Forums
     
    bwool likes this.
  5. bwool

    bwool

    Joined:
    Mar 30, 2023
    Posts:
    20
    I definitely needed to know this. Thank you.
     
    Kurt-Dekker likes this.