Roll-a-ball Tutorial Q&A

Here’s the code:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class playerControl : MonoBehaviour {

    public float speed;
    public Text countText;
    public Text winText;

    private Rigidbody rb;
    private int count;

    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);
    }
    // Destroy everything that enters the trigger
  
    void OnTriggerEnter (Collider other)
    {
        if (other.gameObject.CompareTag ("pickup"))
        {
            other.gameObject.SetActive (false);
            count = count +1;
            SetCountText();
        }
    }
    void SetCountText()
    {
        countText.text = "Count: " + count.ToString ();
        if (count >= 13) {
            winText.text = "You Win!";
        }
    }
}
3 Likes

You have a speed variable but you never use it to modify your ball movement.

EDIT I found it the issue. I didn’t have (movement * speed) set up. Just (movement).

Thanks again! :slight_smile:

Yay!

So, i’ve been at the part 3 of the tutorial for the past hour. I can not seem to get the ball to move…
here is the ode i have so far:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PlayerController : MonoBehaviour {

public float speed;

private Rigidbody rb;

void Start ()
{
rb = GetComponent();
}

void FixedUpdate ()
{
float moveHorizontal = Input.GetAxis (“Horizontal”);
float moveVertical = Input.GetAxis (“Vertical”);

Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

rb.AddForce (movement * speed);
}
}

and here is the error that keeps popping up :frowning:
NullReferenceException: Object reference not set to an instance of an object
PlayerController.FixedUpdate () (at Assets/scripts/PlayerController.cs:20)

i’ve tried just about everything, i’m using unity 5.0.1f1 personal
pls help…

2 Likes

so I realized that the default speed was set to 0, this may have been the reason for my problems, because it is working just fine now.

1 Like

Please learn to format your code using code tags.

http://forum.unity3d.com/threads/using-code-tags-properly.143875/

1 Like

Are you still getting this error:

If so, you may not have a Rigidbody attached.

You did say that it was working, so it looks like you’ve figured it out.

Hola, escribí todos los códigos que me pidieron, pero no logro hacer que la esfera se mueva, no me aparece error alguno.:(.
Ayuda.
I speak spanish

1 Like

1 Like

I did do it :slight_smile:

I start getting errors once I try to add text in lesson 8. The counter is working correctly otherwise.
The referenced script on this Behaviour is missing!
GameObject (named ‘EventSystem’) references runtime script in scene file. Fixing!

Well it seems the errors came from a built-in script being corrupted somehow…? Anyway I just started over and I worked this time.

Hey guys, I wanted to improve this game a little bit, so I wrote a script which spawns pickUp elements in random places of playground, after that I wanted to create script which spawns holes in random places in the ground, which our player can fall through but I dont have any idea how to remove collider in certain point of the ground. Could you give me some advice? Or any link to tutorial? I’ve tried to find something on my own, but still don’t have anything.

PS. For now I am using something like this:

void OnTriggerStay(Collider myCollision)
{
if (myCollision.gameObject.name == “Player”) {
myCollision.enabled = false;
}
}

But I don’t think that this is the most professional way.

I don’t think you can make holes in the collider. What you can do is make different ground with holes n differents places and swap grounds to make holes “appears”. But I think it’s gonna causes some issues because you will lose you mesh, your ridig body and your collider for a frame. And I don’t think thats good.

Hey^^

Im not a pro because im still working through the tutorials, but i guess you could disable the SphereCollider and after a short delay reenable it. For the wholes you could use Invoke and instantiate something like a black round circle on the floor which looks like a hole. But i dont know how you can make that you can “see through” this wholes and the floor beneath in that spot where you spawned the circle… :confused:
With this the player just would fall through the circle, which would like rly unrealistic^^

MAybe if it’s possible to change a material ingame in one frame. For that you would need a dedicated texture. Something with a black circle. Then trigger a materiel change.

Yes but if you rly want the “holes” to appear randomly, you cant just switch to another texture with a hole where you want it to be^^
I googled a bit and it seems to be difficult to generate realtime holes in meshes without knowledge of shaders etc. :confused:

You are right guys, it doesn’t seem to be easy task xD I am gonna stick to my idea, maby it looks a little bit unrealistic, but works for now. There are more basic things that I have to learn first :stuck_out_tongue:

Can anyone tell me why “rb.AddForce(Vector3.zero);” still moves the ball?
It gets velocity over time (i guess something like epsilon), but if you wait long enough the ball starts moving veeeery slow and builds up in speed.
If i remove the rb.AddForce() line, it stays at velocity = 0.