Hello, I’m new to unity, I’ve been using it the past few days.
I’ve looked up a few tutorials and browsed these forums a bit over the past few days. I also normally have the scripting manual open while doing code, but I have a few questions regarding good practices when coding (and any other good practices regarding workflow, or unity in general would be lovely)
So I’m running Javascript, as I have used Action Script in the past to make a (small) game, and Javascript doesn’t scare me like C# does 
I have built a basic 2D scene involving an orthographic camera and a cube and a capsule. The base of my script has come from TheLorax’s tutorial here: http://forum.unity3d.com/threads/26785-Unity-Jump-Start-Video-Tutorials
Here is the code
#pragma strict
var playerSpeed : int;
function Start () {
}
function Update () {
var amtToMoveH = (playerSpeed * Input.GetAxis("Horizontal")) * Time.deltaTime;
transform.Translate(Vector3.right * amtToMoveH);
var amtToMoveV = (playerSpeed * Input.GetAxis("Vertical")) * Time.deltaTime;
transform.Translate(Vector3.up * amtToMoveV);
}
Question Time:
- I can get the cube (player) to move just fine using transform.translate. Is this the most effective way of moving 2D objects?
- Feel free to correct me if you feel like I could code any part of this better (even though all I have is movement at the moment). Even if it looks like what I have ‘will do’. I want to be doing things right.
- Presuming I can provide the animation, How Would I go about animating the player for each direction he walks? (Ideally I would like to use Sprites, but any advice on this would be great, I can model texture and animate in 3D)
- Can I just delete that function Start?
- In the final version of my game, I would like the player to be able to both push blocks around, but also be able to be knocked around by external forces, how would this be set up?
In general I’m a bit confused by collisions in unity, I set up my little demo scene so my cube can collide with another block. This all works fine (although jittery). I can push the block, or if I set the block to is kinematic, it acts as a fine wall.
Is this the standard? Should I instead be using script to deal with collisions?
To remove the jitteryness of the collisions, I have read that I should be using FixedUpdate?
My issue is that I’ve not actually got any information regarding collisions in my code at all. I just applied rigidbodies to both my colliding objects.
Sorry If I have explained things vague, it’s almost 2 in the morning here, and I want sleep. I will be checking back on this thread often, and can easily provide more information, or screenshots etc if there are patient people out there willing to give me a hand.
I also plan on breaking down the 2D tutorial Unity provides here: Unity 2D tools for game dev – evolved for optimal graphics performance
And checking how they handle collisions.
As you can see, I’ve given it a go myself, but would really like to make sure I’m doing things efficiently / properly.
Thank you in advance to anyone who can give me a few pointers.
Hi Elarith
I will just answer on question 1 for you :
If the gameObject requires physics engine , you need to use rigedbodies and the methods for this class like AddForce / AddTorque etc… inside Fixed Update. When using the Fixed update function you dont have to use deltaTime
If the gameObject uses the Unity CharacterController you need to use the Move function of that class , and try only call it once in a frame. Ive used this in a update function with deltaTime
Of coarse while having rigedbodies and charactercontrollers attached it is still possible to call translation methods on the transform , but doing so will mess around with internal collision calculations according to the docs , unexpected behaviour is mentioned I think
I’m just (re)learning collisions also in Unity. (and perhaps this applies to other game sdks).
If you have an object that you want other objects to react with, it will need a collider to let the 2nd object know its collision boundaries.
Colliders come in many shapes such as sphere, cube (box), capsule, or you can just specify ‘mesh’ to have the entire mesh define the collision area, but this is much slower.
If you want an object to be able to be influenced by physics, you’ll need to add a rigidbody.
Various properties of the rigidbody tell the physics engine how to handle the object, such as mass and drag.
If you want / need predictable movements, and want to handle the psudo-physics yourself, don’t use a rigidbody, and modify the transform position manually. To have physics control the movement, use a rigidbody, and apply forces to the rigidbody. A force is a vector representing both a 3D direction and a magnitude. Adding force causes the object to be ‘pushed’ in the specified direction. Physics will then take over and move the object for you and apply any friction or drag specified, and most likely the object will stop eventually.
All non-physics transform manipulation should be inside the ‘Update()’ function, while physics applied to rigidbodies should be handled in ‘FixedUpdate()’ function, as the physics engine uses this function to keep itself running at a fixed rate, and not necessarily at the same frame rate as your game.
As I’m just learning also, please correct any misconceptions I may have made.
Thanks for the help so far guys.
So as I understand, transform.translate is a perfectly acceptable way to move objects that aren’t being controlled by character controllers?
I’m trying to create the kind of view often seen in bomberman games, or the pokemon adventure screens, or Gameboy / SNES Legend of Zelda games.
Does anyone know the best way to have a player collide with, and not be able to pass through a wall?
Does anyone know of any working examples like this, or does anyone know of a standard way to do the above?
I figure once I understand the basics, I can then apply this to more advanced scenarios, or use psuedo physics, and code collisions that would physically push the player myself.
Again, I will be looking and the example 2D tutorial soon, to see how the handle collisions, but any more advice would be appreciated, just to make sure I’m on the right track.
Thanks for the help so far 
So the 2D tutorial is not a good example for me, as the player uses a character controller. I’m out of luck there, also, the entire scene doesn’t seem to work to well in recent versions of unity. The boxes and spaceship are not happy sitting still.
I’ve tried using transform.position.x and y instead of transform.translate, and I still get very jittery collisions.
My guess is I cannot rely on unity to handle collisions for me in this case, and I need to do it, or at least stabilise it through code.
I can actually fix the jitter by changing my movement code to the FixedUpdate function, but I have read that this is an inefficient way to run code, so I’m not sure what to do.
Thanks for your help again. I’ll try to do more research, I may post back here if I find anything, just incase anyone else is wondering.