Unity 3D work

well, i am using Unity 3D for the two games i’m currently working on (‘flip that mind‘ and ‘nano messiah’) and have found myself creating various solutions for the games’ requirements, thought i’d put them up for the rest of the world to benefit from 🙂

Camera collision system

made in unity 3.2, this prefab sets up a system with an orbit camera, where the camera moves close to the player if obstructed by any objects in it’s view. it’s not perfect, but is generally quite smooth 🙂 go world,benefit!

http://www.megaupload.com/?d=JGHF3SIF

how to use:-

1) download this prefab from the megaupload link up there

2) open unity, go to the assets section, right click and say ‘import package’. import the downloaded package.

3) you’ll see the prefab called ‘FTM_camera_system’

4) drag this into your project(delete/deactivate your existing camera)

5) select the ‘FTM_camera_system’ prefab, one of it’s children is ‘maincamera’, and the other is ‘maincamguide’

6) select ‘maincamera’, look at the inspector, you’ll see that in the script, the ‘target’ field says ‘none (transform)’

7) find your character controller (if you’re using the lerpz turorial, that would be lerpz) and drag it into this empty slot

this should work :) let me know if you have any problems…

Tip: to avoid the camera acting unintentionally with glass, thin bars etc, you can ask the ray shooting from the script ‘FTM_CameraCube’ (attached to ‘colchecker’, son of ‘maincamguide’) to ignore the layers these things are on, figure it out!!

i intend to build these options into version 2, but it really isn’t that hard, you’ll learn something young padawans.

EDIT: added extensive ‘how to use’ section

Unity script that allows multiple tags

note: for any object to enjoy the benefit of ‘multiple tags’, you should give it one tag first, call it ‘MultiTag’ if you please.

the script:-

var Tags : String[];

function Tag (TagX : String)

{

for(var i=0;i<Tags.Length;i++)

{

var TempBool : boolean = (TagX ==Tags[i]);

if(TempBool)

{

return true;

}

}

return false;

}

Notes:-

that’s it, store this text in a file called TagScript.js, now attach TagScript.js to any object you want to assign multiple tags to

 

 

 

 

 

 

 

 

 

and then just set the size, and type in your tags 🙂 i think if i dive into a little more of unity ‘editor’ scripting i might be able to add the tags as a drop down list, lets see, this is it for now 🙂

oh yes last thing, why i asked you to give the object the tag ‘MultiTag’ is because this script is to be used thus-

usual ‘if’ statement to check for tags :-

if(object.tag == “Tag”)

{

//do something….

}

with this script, you have to use:-

if(object.tag ==”MultiTag”)

{

if(object.transform.GetComponent(TagScript).Tag(“Tag”))

{

//do something….

}

}

if you try to run this code on things that don’t have the tag ‘MultiTag’ it’ll keep giving you those ghastly ‘Nullreferenceexception’ errors that come when an attempt is being to access something that isn’t there.

GetComponent only works if the thing you’ve typed in the bracket (in this case, my script ‘TagScript’) actually is attached to the object. so to avoid those errors, only attach ‘tagScript’ to an object you’ve already tagged as ‘MultiTag’ and have attached ‘TagScript’ to…

also it doesn’t have to be ‘transform.GetComponent’, it works also with the object’s ‘gameObject’ and ‘rigidbody’ components, just read up on how GetComponent works, it’s very useful….

17 Responses “Unity 3D work” →
  1. Looks like it works really well, but I’m having trouble implementing it could you elaborate on how to hook it up to work a little more. I’m using similar scripts to that of the Lerpz tutorial if it helps.

    Reply
    • 1) ok, here’s what you do, you download this prefab from the megaupload link up there

      2) open unity, go to the assets section, right click and say ‘import package’. import the downloaded package.

      3) you’ll see the prefab called ‘camera system’ or something.

      4) drag this into your project, delete your existing camera.

      5) in the ‘camera system’ prefab, one of it’s children is ‘maincamera’, and the other is ‘maincamguide’

      6) select ‘maincamera’, look at the inspector, you’ll see that in the script, the ‘target’ fiels says ‘none (transform)’

      7) find your character controller (if you’re using the lerpz turorial, that would be lerpz) and drag it into this empty slot

      this should work 🙂 let me know if you have any problems…

      i also noticed how shabby that prefab is, so i’m going to clean up the code a bit and re upload it…version 2 is on it’s way!!

      Reply
      • Much appreciated, just out of interest are you allowing this to be integrated into games royalty free?

  2. it’s ok man 🙂 this is too primitive a thing to be ‘sold’, i don’t even believe in these websites selling prefabs for money, I’d encourage people to ‘learn’. If you use my prefabs, you’ll learn something probably, which is cool with me 🙂

    Reply
  3. Hey! Thanks for the use of your camera and scripts, but I am having some trouble after following your steps.

    My character presently is using a ThirdPersonCamera Script which is attached directly to the Character Controller itself. I am unsure if this method makes things complicated or not but when I import the prefab and target my Character Controller under the MainCamera Script, the camera acts exactly as my original main camera and passes through walls.

    Also there is no red cube centered on my character as in your video but I assume that was only used for testing purposes. Any advice? Thanks again!

    Reply
    • deactivate/remove that ‘ThirdPersonCamera’ script which is on your character controller, and everything should be fine, yes the red cube should be visible to you, to show you that it’s working (you can turn off it’s visibility whenever you’re satisfied with everything working fine)

      and hey, if my comment above makes you remove the script, and if it all works, then you need to rethink your work methodology. If you’re pointing out that you think that the camerascript attached to your character could be the problem, then you should have already experimented with removing it! this isn’t brick and mortar, it’s a software 🙂 experiment!

      a the moment the script attached to your character is somehow overriding the script attached to the camera system…

      Reply
  4. Ok I have now done what you said, but the red cube isn’t moving with my character, meaning the camera stays static focused on the cub as my character walks away :S

    Reply
  5. Nope :/

    Reply

  6. Denis

    May 14, 2011

    Many thanks for a script “Camera collision system” =) has very much helped…
    Only in “how to use” it is not written as it to adjust what the camera in walls didn’t run. Long experimented while all has earned as it is necessary…

    1) Change “distanceMin and distanceMax” on float so it is more convenient. (I at myself have already altered)

    2) it would Would be desirable that back the camera was returned smoothly.

    3) It would be desirable to see implementation of ignoring of the given objects.

    Well and so very class script, the best in a network. Once again thanks… I Apologize for the bad English

    Reply
  7. Thank you for the script, however I am having some serious issues. I should warn you, I am not a programmer per say. However, I am familiar with the Unity engine. I have followed your instructions step by step and I’m still stumped. In order to test it, I opened a whole new scene. In that scence, I have added a terrain, the 3rd Person Controller that Unity provides, and the FTM_Camera_System. I deleted the default camera in the scene as well as the 3rd Person Camera Script that comes attached to the 3rd person controller. Then, I click on “MainCamera” and drag n’ drop my 3rd Person Controller onto the target. When I hit play, the camera does NOT automatically snap to the target (3rd person controller). The character still moves as he should, but the camera does not move at all. Also, I’m getting an error message that says “UnityException: Input Axis GeneralMove X is not setup.” I assume this is the issue, any ideas on what I’m doing incorrectly? Any help would be greatly appreciated!!

    P.s. please excuse my newbiness and be easy on me 🙂

    Thanks again!

    Reply
  8. oops, i”m really sorry! i just figured out the problem :-S but i can approach this in two ways, one..of apologizing for my mistake, and two, of actually expressing disappointment that nobody clearly pointed it out! you’re students of unity scripting! you have to learn everything inside out!!

    so, let’s have a small class on the ‘input manager’

    get to it by edit>project settings>input

    over here, you can give whatever name you want to the various inputs your game can take, if nothing is changed here, the line Input.GetAxis (“Mouse X”) takes the X axis of the mouse…something like that, my Input manager is so heavily customized and personalized that i don’t remember unity’s default settings..

    so..now i’ve given you enough hints as to what you need to change to make the script work, good luck 😉

    i am skeptical about adding that step to the steps i’ve asked you all to follow…what do you think?

    Reply
    • Thank you very much, I got it working… kinda. My only problem now is that the camera automatically snaps to the minimum distance (1) from the target when I hit play. Once I move the mouse around, it zooms out to maximum, but I would like it to START at the maximum distance from the target. I did keep the default values for distance(10), min distance(1), and max distance(10). What am I missing here? Again, please excuse my lack of scripting knowledge, I’m trying to learn!!

      Thanks again sir!

      Reply
  9. I made it work by commenting out the line “y -= Input.GetAxis(“Horizontal”) * ySpeed * 0.02;” and by changing the General Input bit with the name of my input. But the camera still enters walls. I can’t notice any difference….?

    Thanks for your work though! Look forward to the next version of it!

    Reply

  10. Bloodstorm

    September 1, 2011

    Hi there Shar

    Great script, I’ve so far got it working almost exactly as I want – I managed to figure out that GeneralMove X should be Mouse X by default (with NO scripting experience) just using the information unity gave me with the error and CTRL+F to find the problematic line.

    I managed to get the camera attached to my object (character) without any hassles, fixed the problem of the camera being right on the characters arse instead of a fairly nice distance, by increasing the minimun distance to a higher value (which should be common knowledge) and I’ve also managed to edit the script in such a way that it locks the mouse movement in the Y axis – now my only problem is that I want the camera looking down on the character at a 45 degree angle and still be able to move it around in the X axis with the mouse – This problem I cant seem to figure out the camera follows perfectly but its on the same height was the character itself.

    Thanks again

    Reply
  11. Hey, firstly great job with this prefab! Just had a few queries. What part of your code do i disable to not make your camera an orbit cam bur rather something similar to the smooth follow cam.

    Reply

Leave a reply to Bloodstorm Cancel reply