- Home /
Wall collisions for a top-down game
Hi! I'm trying to make a game like the old-school top-down Zelda games, but I can't figure out how to setup the collision on my character. I'm using a regular cube just to practice with, but I plan to make my game in 2D.
I've already tried rigidbodies on my character. It stops it from going any further, but it makes it really jumpy when I hit something and keep pressing on the same button. Colliders I'm not exactly sure how to code it so I can have nice smooth stopping like in Zelda. Which is best for my situation and can anyone please give me some starter code (preferably JavaScript)? I've spent 3 days researching this topic on Google, Unity Forums, Unity Answers, and Unity Wiki. I would really appreciate any help! Thanks so much!
BTW here is the original code for my main character var speed : float = 5.0;
function Update ()
{
var transH : float = Input.GetAxis("1P Horizontal") * speed * Time.deltaTime;
var transV : float = Input.GetAxis("1P Vertical") * speed * Time.deltaTime;
transform.Translate(transH, transV, 0);
}
Answer by Dan Miller · Feb 12, 2013 at 02:26 AM
Try adding a Character Controller to your player. Then change your above code to the following
function Update ()
{
var transH : float = Input.GetAxis("1P Horizontal") * speed * Time.deltaTime;
var transV : float = Input.GetAxis("1P Vertical") * speed * Time.deltaTime;
GetComponent(CharacterController).Move(transH, transV, 0.0f);
}
Then by having just a box collider on the walls with Is Trigger unchecked should give you the collision results you are looking for.
Unity gives me an error on your code stating:
The best overload for the method 'UnityEngine.CharacterController.$$anonymous$$ove(UnityEngine.Vector3)' is not compatible with the argument list '(float, float, int)'.
BTW sorry for the late response. I've been insanely busy this week
putting GetComponent(CharacterController).$$anonymous$$ove(transH, transV, 0.0f);
should fix that error. I've edited and corrected the original post.
I fixed the problem, but I'm still not getting any collisions
do the walls you wish to collide with have box colliders on them and is the 'is Trigger' box on them unchecked?