Help Needed with Unity Tutorial
Hi Guys
I've started working through a book that teaches the basics of Unity, but have run into a piece of code that does not seem to work. I have copied it word for word from the book, but no luck. I was hoping someone could spot my mistake (hopefully not something totally embarrassing). It is a movement controller, javascript script that I am attaching to a GameObject with a CharacterController.
I've started working through a book that teaches the basics of Unity, but have run into a piece of code that does not seem to work. I have copied it word for word from the book, but no luck. I was hoping someone could spot my mistake (hopefully not something totally embarrassing). It is a movement controller, javascript script that I am attaching to a GameObject with a CharacterController.
var rollSpeed = 6.0; var fastRollSpeed=2.0; var JumpSpeed=8.0; var gravity = 20.0; var rotateSpeed=4.0; var duckSpeed=0.5; private var moveDirection=Vector3.zero; private var grounded:boolean=false; private var moveHorz=0.0; private var normalHeight=2.0; private var duckHeight=1.0; private var rotateDirection=Vector3.zero; var isControllable: boolean = true; var controller: CharacterController; controller=GetComponent(CharacterController); function FixedUpdate(){ if(!isControllable) Input.ResetInputAxes(); else{ if(grounded){ moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxi s("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= rollSpeed; controller.height = normalHeight; controller.center.y = controller.height/2; moveHorz = Input.GetAxis("Horizontal"); if(moveHorz > 0) // positive values are right negative are left 0 is forward and back rotateDirection = new Vector3(0,1,0); //turn right else if(moveHorz < 0) rotateDirection = new Vector3(0,-1,0); //turn left else rotateDirection = new Vector3(0,0,0); if(Input.GetButton ("Jump")) { moveDirection.y = JumpSpeed; // y position is changed to jumpspeed variable } if(Input.GetButton("Boost")){// boost speed is current speed X boost variable moveDirection *= fastRollSpeed; } if(Input.GetButton("Duck")){ controller.height=duckHeight; controller.center.y = controller.height/2 + .25; moveDirection *= duckSpeed;} moveDirection.y -= gravity * Time.deltaTime;// multiplying by time delta is doing stuff by the second // Getting the controller to move and stay with character var flags = controller.Move(moveDirection * Time.deltaTime); controller.transform.Rotate(rotateDirection * Time.deltaTime, rotateSpeed); grounded= ((flags & CollisionFlags.CollidedBelow)!=0); }// end of if grounded ----------------------------------------------------------- }}
Comments
Some context would be nice. :)
Though I'm not sure if that came from pasting the code here. Could it be that?
@Elyaradine - Thanks again for being willing to assist. Didn't see the the error list in the console last night when I tried to compile for the very first time in Unity. Learning new software can be a bitch, especially if you make noob errors like this, but then again how else does one learn. Thanks again :)
e.g
Although I'm pretty sure you can get away with some basic variable typing even with the strict pragma. Something like:
Should run without giving a compiler error. Casting unity related variables like gameobjects and transforms will throw errors
Re: error highlighting - I'm not sure if there's a way to get that. I'm sure some of the guys using Visual Studio here will know, but I'm still using MonoDevelop (bundled with Unity), which is probably a bad idea because it does drive me nuts sometimes. MD highlights certain errors but not others. Otherwise, I just check what the console says. You can normally double click the error in the console and it'll take you to the offending line in MonoDevelop.
Anyway, hope it helps!
@Manikin - I found the error list under MonoDevelop's view options, but for some reason errors listed in the Unity console line do not appear as errors in the editor? Anyway, I am also more of a C# guy, but the book i'm working through has only Javascript examples. I thought I would work through the book and then translate all of the scripts into C# afterwards. Should be good practice and i'm getting a bit of Javascript experience at the same time.
but for some reason the bombs are just falling straight trought the player and not firing the trigger. What can be the problem? Any help will be appreciated. Also, if other items (enemies) enter the trigger the player does get destroyed?
If you want to check for different kinds of objects (eg. enemy hitting player, or bullet hitting player), one of the most common practices - as far as I'm aware - is to assign your enemy prefabs an "Enemy" tag and your bombs a "Bullet" tag, or some other similar naming. Then you can check for the type in your OnTriggerEnter. So for example:
Hope that helps!
If I'm understanding your question correctly (and I only know it in terms of C#), you could reference the collider with
With the collider example, it's just a shortcut of (in C#)
I wouldn't say you should never do it though -- there are some cases where I have a few instances of the same material happening simultaneously (usually for fx, where I might animate a material property, but the fx object is destroyed after a few frames anyway)... but for the most part you'd want to use renderer.sharedMaterial instead of renderer.material.
Is there any way to explicitly kill the material in that case, since the GC clearly doesn't do it?
They will get cleaned up when the scene changes, or with call to Resources.UnloadUnusedAssets.
Long answer:
Google and reference documents. Unity has a really well documented API. You can find a lot of useful stuff there.
http://docs.unity3d.com/Documentation/ScriptReference/Screen-showCursor.html
Anyway, can you maybe post the method and call you make? It sounds like it should be working. You can maybe try to put it in the Awake() method of your camera. Are you running the game in the editor? If you are try to make a build and see if it still happens there.
Thanks again for helping.
I wouldn't worry about putting it in the awake function thought. If it's because it's in the editor is probably because the game window in the editor doesn't have focus when you run it, and as soon as you click it gains focus and stops rendering the cursor. It should be fine as is.
I have a script that allows my FPS player to pick up a cube and hold it at a distance before the main camera, and it moves with the player (parented to camera). To get the cube to rotate with the player it's isKinematic is switched to True. This works fine. However the cube now ignores physics and is able to move through walls (stretched out cubes) while being held. If I add a Rigidbody to the walls, the held cube no longer moves through them, but then the walls get pushed around when the cube bumps into them. I've tried switching of the gravity on the wall's rigidbody and making them really heavy, but that had some comical effects. I tried moving the wall down into the plane, but that had even more hilarious effects. I tried google, but was unsuccesfull. If anyone can help out a noob again it would be much appreciated.
Here's the script i'm using:
Edit: This would at least stop the walls from moving, but it might behave oddly.
or I need a way that the wall's collider will still collide with a kinematic object but not be moved by it
So by making your carried cube's isKinematic property true you are essentially telling it to ignore the collisions it has with the walls. Because the rigidbody on the wall's isKinematic property is false, it is affected by the collision. If you lock the position and rotation it shouldn't move though. If you want Unity to handle the collisions you can't turn on isKinematic.
What do you want the behaviour to be on the block/object being carried when it hits a wall? Should it stop against the wall as well as preventing the player from moving in that direction? What is the behaviour you are looking for?
I'm working on a physics type game, and the cubes are used to trigger events as well as for stacking in order to reach higher areas. So it is frustrating when you try to stack the cubes next to a wall if they are able to go through the wall or alternatively move the wall. It is really a barrier to my game being fun, so I really hope I can fix this.
please help if you can
Else I think it might be because you call the gunFireAnimation first in the code it is preceding the other animation. So then just use this at the bottom animation.Play() :
If I'm wrong then cool story :P I'm still trying to get a 2D game up haha
This is not working. The trigger fires and the gameobjects receives the message, but the scripts are not enabled. Any help with this would really be appreciated.
Edit: Make sure the scripts you are trying to enable are on the same gameObject as the script with the method. otherwise you will need a reference to where the scripts are located.