c# if player.transform.position ?

hi guys

i am trying to do a if statement to say if the transform position of my game object is not equal to (0,0.5,0) then set a public bool variable to true.

it does not like what i have entered can anyone see what i have done wrong? i get the below error

Assets/Scripts/Timer.cs(23,17): error CS0029: Cannot implicitly convert type UnityEngine.Vector3' to bool’

using UnityEngine;
using UnityEngine.UI;  //telling unity i am using the UI namespace
using System.Collections;


public class Timer : MonoBehaviour {
public float timeRemaining = 60; // set my default level time
public Text winText; //  my main text field displays the win/lose text
public Text timer; // new text ui to display timer
public GameObject player;
public bool playerMoved = false;



    // Use this for initialization
    void Start () {
   
    }
   
    // Update is called once per frame
    void Update () {

        if (player.transform.position = new Vector3(0,0,0))
            {
                playerMoved = true;
            }


        if (timeRemaining > 0)
            {
            // Debug.Log("time left..."+timeRemaining); uncomment for logging
            timeRemaining -= Time.deltaTime; // time remaining - scene active time
            setTime (); // calling the setTime function converts numeric to text for text gui
            if (timeRemaining <= 0) // check to see if the timeRemaining vairable is less or equal to 0
            {
                timeRemaining = 0; // set the timer to 0 prevents going into -
                setTime (); // calling the setTime function converts numeric to text for text gui
                winText.text ="OUT OF TIME!"; // set text gui
            }
        }
    }

    void setTime () // new function as using the same line of would use the same code twice
    {
        timer.text = "Timer: " +  timeRemaining.ToString ("0:00"); // concat the text Timer: with the string value of timeRemaining
    }
}

Looks like you’ve got a ‘position =’ rather than a ‘position ==’

It’s not a great idea to compare a position because of floating point inaccuracies - you’re probably better of getting the distance between the object and 0,0,0 (magnitude) and then checking it’s lower than a small value (e.g. floating point epsilon)

Hi

thanks for the reply i basically wanted to say if player position is not equal to (0,0.5,0) [players starting position] then start the timer.

it is now working by doing the following

    void Update () {

        if (player.transform.position != new Vector3(0f,0.5f,0f))
            {
                playerMoved = true;
            }


        if (timeRemaining > 0 & playerMoved == true)
            {
            // Debug.Log("time left..."+timeRemaining); uncomment for logging
            timeRemaining -= Time.deltaTime; // time remaining - scene active time
            setTime (); // calling the setTime function converts numeric to text for text gui
            if (timeRemaining <= 0) // check to see if the timeRemaining vairable is less or equal to 0
            {
                timeRemaining = 0; // set the timer to 0 prevents going into -
                setTime (); // calling the setTime function converts numeric to text for text gui
                winText.text ="OUT OF TIME!"; // set text gui
            }
        }
    }

sorry I’m new to scripting I’m just expanding of the roll-a-ball tutorial and making it a bit more game like, what are floating point inaccuracies?

You need to be using the == operator instead of the = operator. The == operator is for assignments, and produces a bool value that if statements needs, the = is the assignment operator.

However, this code isn’t likely to work anyway. You shouldn’t be comparing two floating point values (vectors are 3 floating point values) directly. Float point values are inaccurate, you cannot assume that .1f * 10f will equal 1f. You can assume, however, that it will be very close to 1f. So if you’re moving an object in an update loop, you cannot assume that it will arrive exactly at its destination.

Instead, you can try something like if( Vector3.Distance(a, b) < 0.01f ) or the more optimized if ((a - b).sqrMagnitude < 0.01f). Here, 0.01f is some sufficiently small number, it might need some tweaking, but the point is you shouldn’t be comparing vectors and floating using ==, you should be testing if they’re “close enough.”