Category Archives: Uncategorized

Prevent player from leaving screen – in progress

Still haven’t made this work in the way I want to.  Current notes:

 

Several options:

  1. Add a wall around the screen using 3D cube objects.  However,  one of the objects needs a non-kinematic rigid body.  I need each of my player objects to be kinematic, so that they don’t spin around when they collide with things.  http://answers.unity3d.com/questions/216204/objects-pass-through-each-other.html
  2. Include in script with collider triggers.

I’ve seen this work really easily in the roll-a-ball unity tutorial, and can’t figure out what’s different about that and what I’m doing.

 

  1. limit player’s movement space
  2. reposition when it hits a box
  3. make it not kinematic
  4. limit mouse’s movement space

Mathf.Clamp

 

 

http://docs.unity3d.com/Manual/LayerBasedCollision.html

http://answers.unity3d.com/questions/414362/keep-character-inside-of-an-objects-bounds.html

 

http://answers.unity3d.com/questions/623959/how-to-keep-object-from-going-off-screen.html

This video looks helpful:

http://stackoverflow.com/questions/26789457/characters-passing-through-objects-unity3d

 

Clone object

I don’t want to instantiate a prefab, I want to duplicate the exact gameobject I have the script attached to.

Apparently other people have thought about this and don’t love instantiating non prefabs, but can do it:  http://answers.unity3d.com/questions/957617/how-to-clone-a-gameobject-in-runtime-not-with-inst.html

I don’t want a hidden “original” one because I’ve got too many things popping in and out of my script already.  I’d really like to clone it at an exact moment in the game state, before I reset the original object to have no audio components.

Here’s what I’ve tried so far:

private GameObject answerClone;

Vector3 clonePosition = new Vector3(0, 0, 0);

answerClone = Instantiate(gameObject, clonePosition, Quaternion.identity);

 

I also tried  :

answerClone = Instantiate(this.gameObject, clonePosition, Quaternion.identity);

Both get this error.

Assets/Scripts/PlayerFinalWord.cs(172,13): error CS0266: Cannot implicitly convert type `UnityEngine.Object’ to `UnityEngine.GameObject’. An explicit conversion exists (are you missing a cast?)

 

Ah, so I need to explicitly cast this as a gameobject.  This site showed me how: http://answers.unity3d.com/questions/254195/c-cast-object-to-gameobject-not-working.html

This code now does the trick:

GameObject answerClone = (GameObject)Instantiate(answer, clonePosition, Quaternion.identity);

However,  I don’t see the object on my screen , because it’s still being affected by the commands to its original object, “answer”.  uh oh.

Things I’m reading that haven’t inspired me but might have the right answer:

http://gamedev.stackexchange.com/questions/77544/scripts-wont-affect-clones-unity3d

http://gamedev.stackexchange.com/questions/77544/scripts-wont-affect-clones-unity3d

http://answers.unity3d.com/questions/781985/how-to-spawn-prefab-to-a-certain-position.html

Maybe I really do need an early invisible OTHER answer.  Ugh.  So many invisible things running around, spawning other invisible things!   I guess I need to just create an entirely new game object and give it the TextMesh and AudioSource components of Answer at the right moment in time.

Alright so I made an empty gameobject with 3 children: NotebookEntryOne, NotebookEntryTwo, and NotebookEntryThree.  When a word gets put into the notebook, it’ll go through and index the children, and keep count.  This makes use of a code I’ve used before, to make an object have a yellow border around it from a yellow child object:

endingHighlight = ending.gameObject.transform.GetChild(0);

So here it is:

            if (other.tag == Notebook)
            {
                Transform notebookOne = (Transform)notebookEntries.gameObject.transform.GetChild(notebookCount);
                notebookOne.transform.position = new Vector3(notebookOne.transform.position.x, notebookOne.transform.position.y, 5f);
                TextMesh notebookOneText = notebookOne.GetComponent<TextMesh>();
                notebookOneText.text = thisText.text;
                notebookCount += 1; 
            }

Problems that happened on the way:

  • I forgot that GetChild returns a Transform
  • forgot that I need .text after a TextMesh to convert it from a Textmesh to a string
  • forgot the “new” in front of my Vector3 – easy fix
  • Note that I’m just moving the entry from behind the notebook image to in front of it (with the z transform.position changing from 10 to 5).

I’m super excited about this – looks kind of like magic!  Now to do the same for the other possible slot that answers can end up in – the garbage!

End game when all objects gone

I’ll of course have other things happen for the “game over” state, more like “you won!” and “next level!”, but I wanted a quick easy way to show that the code was working.

    private GameObject[] onsets;

 

    public void AddScore (int newScoreValue)
    {
        score += newScoreValue;
        UpdateScore();
    }

    void UpdateScore()
    {
        scoreText.text = Score:  + score;
        onsets = GameObject.FindGameObjectsWithTag(Onset);
        if (onsets.Length == 0)
        {
            scoreText.text = game over!;
        }
    }

The other option was to do a count for how many times the score got increased, but that would need to be reset if I wanted there to be more or fewer onsets in the game.  I like the version that checks if there are any left, and if there aren’t, declares the game done.


Adding text (scoremanager 1)

Turns out the simplest way to add UI text *isn’t* Canvas/text.

From https://unity3d.com/learn/tutorials/projects/space-shooter/counting-points at :26 :

guitext

[note – .tiff images don’t work here, but .png does.  “Add Media” button at the top left]

This also looks like it solves the fuzziness problem of TextMesh.  But it’s drawn on a layer ABOVE the game, not in the game world, so I can’t see it in the game view.  Viewport Space, rather than Screen Space.  Screen Space is pixels – Viewport space is a single width and height value (0,0 in lower left, 1,1 in upper right).   This makes it less desirable for to put on a game object, and doesn’t really seem like its purpose, but I’ll try later anyhow.

I’m liking this tutorial bc it goes through how to call a function on another component in a different game object (like I was thinking about in this post).

 

#todo – test out other objects with this, update relevant posts with this

#todo – when do you use UI Canvas vs. adding a TextUI component to an obect?  UI Canvas was really hard to place position-wise, had fuzzy text, and needed alterations to its settings so the camera could see it.

UI Canvas not showing in game view

UI elements in a canvas don’t show up in your main camera unless you change the setting.  Click on Canvas, look at the Canvas component, and switch Render Mode to “World Space” instead of the automatic “Screen Space – Overlay”.

http://answers.unity3d.com/questions/835737/gameobjects-added-to-canvas-do-not-show-in-game.html

Finding a good host for a beginner’s programming blog

Hi!  I wasn’t expecting to have my first problem-solving post be about the blog itself, but this ended up being more time-consuming than I’d thought, so I’m gonna go meta. Here’s what I ended up doing.

I used WordPress through BlueHost. This cost money (I paid sthing like $71.95 for it – unclear if I needed all the things that it added on to it, but I was feeling really impatient and wanted to get things on a roll.  If I didn’t need the extras, please say in comments!).  Reason why I went for BlueHost: WordPress.com doesn’t allow you to add plugins (as far as I can tell/ google tells me), so I needed an easy hosting system that could also install plugins.  Why plugins?  Because I want code blocks to not be ugly.  Why not do it all myself?  I’ll do that soon!  Right now, one learning topic at a time – and the current topic is Unity 3D/C#, so I can get some fun indie games up and running asap.

Heads-up – BlueHost takes what feels like forever to set up (probably about 45 mins-60 mins in reality), and looks like it’s having an error or a firewall problem while it’s setting up.  The website assures you it will take “a few minutes”, but when I waited 30 mins to talk to one of their live chat folks, she told me it could take as much as 4 hours.  Oof!  You get an email when it’s working, and right after that mine started, so that was happy!  Set your expectations to not being able to play with your new website immediately, and don’t waste time like I did, and you’ll be a happier blogger.

Then I went to the sidebar, plugins, selected “add new,” and searched for “code block.”  I picked Smart Syntax because I’d been googling around and saw that Google prettify was good, and Smart Syntax uses that, so I figured I’d start with that.  The others also looked fine and I’d seen references to those as well.

Test run of code block:
    public void FillDictionary()
    {
        WordDict.Add(gug, Trash);
        WordDict.Add(hug, Notebook);
        WordDict.Add(bug, Notebook);
        WordDict.Add(plug, Notebook);
        WordDict.Add(wug, Trash);
    }

Code block is looking good!  Unclear if it was Smart Syntax that did that or if cut and paste just works well in this editor.  I’ll figure that out later, but for now am satisfied that this appears to be working.  Haven’t figured out fonts yet, and would love to make everything, especially the code, a smaller font.

Other positive thing – it looks like it’s autosaving every few minutes (little bar at the bottom saying wordcount and time when draft was saved).  Time is set to … 4 hours later than it is.  I’ll also fix that later, but I’m thrilled that this appears to autosave.  In my earlier (read: 2 hours ago) forays into SquareSpace and WordPress.com, I lost some of my initial blurbs, and found the Save button frustrating because it would take me out of the editor.  (There’s a decent chance I was doing something wrong, but I’d rather have a nicer UI system.)

Slightly negative thing – it feels pretty slow navigating around different parts of this site.  Worth it for now, but I want to keep looking for other options.