Hi,
I’m working with the “Roller Ball” tutorial trying to place a counter that ticks whenever the ball is in motion and output it along with the “Pickup” output…I’m a new user and new to coding in general this is where I’m at code wise…
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
private Rigidbody rb;
private int count;
//To track/output movment
public Text moveText;
private int move;
private float oldPos;
void Start ()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText ();
SetMoveText ();
winText.text = "";
move = 0;
oldPos = Vector3;
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
//attempt to get counter and output data
float delta = transform.position - oldPos;
SetMoveText ();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ( "Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
if (count >= 13)
{
winText.text = "You Win!";
}
}
void SetMoveText ()//Display movement
{
moveText.text = "Movement: " + move.ToString ();
}
}
Hi,
I’m working with the “Roller Ball” tutorial trying to place a counter that ticks whenever the ball is in motion and output it along with the “Pickup” output…I’m a new user and new to coding in general this is where I’m at codewise…
If you have a class variable (up at the top) perhaps called:
float odometer;
Then your line 38 would go something like this:
odometer += Vector3.Distance( oldPos, transform.position); // add this fragment of distance to the odometer
Then for the display of the distance, make Unity UI Text object in your scene, and drag a reference into a public variable in your script:
public Text scoreText; // gonna need to be using UnityEngine.UI; at the top!
And finally, after the line where you update odometer, try this line:
scoreText.text = System.String.Format( "Distance: {0:0.00}m", odometer);
I'm typing the above without a compiler, so no guarantee of perfection!
I tried adjusting to…
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
private Rigidbody rb;
private int count;
private float distance;
private float oldPos;
public Text moveText;
void Start ()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText ();
winText.text = "";
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void LateUpdate()
{
distance += Vector3.Distance( oldPos, transform.position);
moveText.text = System.String.Format( "Distance: {0:0.00}m", distance);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ( "Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
if (count >= 13)
{
winText.text = "You Win!";
}
}
}
…but it will not compile. Unity is giving the following errors…
Assets/Scripts/PlayerController.cs(37,37): error CS1503: Argument #1' cannot convert float’ expression to type `UnityEngine.Vector3’
Assets/Scripts/PlayerController.cs(37,37): error CS1502: The best overloaded method match for `UnityEngine.Vector3.Distance(UnityEngine.Vector3, UnityEngine.Vector3)’ has some invalid arguments
and I don’t know what to do from here.
I’ve also tried it this way, but I can’t get it to compile either atm…
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed;
public Text countText;
public Text winText;
private Rigidbody rb;
private int count;
private float distance;
private float position;
public Text disText;
void Start ()
{
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText ();
winText.text = "";
SetDistanceText ();
position = transform.position;
}
void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis ("Horizontal");
float moveVertical = Input.GetAxis ("Vertical");
Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
rb.AddForce (movement * speed);
}
void LateUpdate()
{
if (position != transform.position) {
distance = distance + 1;
position = transform.position;
SetDistanceText ();
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ( "Pick Up"))
{
other.gameObject.SetActive (false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Count: " + count.ToString ();
if (count >= 13)
{
winText.text = "You Win!";
}
}
void SetDistanceText ()
{
disText.text = "Movement: " + distance.ToString ();
}
}
ah, oldPos should be of type Vector3.
And before you move the thing in youtr FixedUpdate() function, save its old position
oldPos = transform.position;
I’m referring to the script ABOVE the one above this reply… I didn’t read the very one immediately above this.
Got it working thanks for your help!