- Home /
4 way direction movement using a Character Controller.
Dear reader,
I'm making a 3d platformer with a 2d sprite character using Sprite Manager 2. I have a working 4-way movement script, using simple [transform.Translate], However the collisions and such are just not as good as with a Character Controller.
This is the script I'm using atm:
var speed : int = 4;
var mySprite : PackedSprite;
var jumpspd : int = 20;
function Start () {
var mySprite : PackedSprite = gameObject.GetComponent("PackedSprite");
}
function Update () {
if(Input.GetButtonDown("Run")){
speed = (speed*1.5);
}
else if(Input.GetButtonUp("Run")){
speed = (speed*0.75);
}
if(Input.GetButton("left")){
transform.Translate(-speed*Time.deltaTime,0,0);
mySprite.DoAnim(2);
}
else if(Input.GetButton("right")){
transform.Translate(speed*Time.deltaTime,0,0);
mySprite.DoAnim(3);
}
else if(Input.GetButton("up")){
transform.Translate(0,0,speed*Time.deltaTime);
mySprite.DoAnim(1);
}
else if(Input.GetButton("down")){
transform.Translate(0,0,-speed*Time.deltaTime);
mySprite.DoAnim(0);
}
else if(Input.GetButtonUp("left")){
mySprite.DoAnim(6);
}
else if(Input.GetButtonUp("right")){
mySprite.DoAnim(7);
}
else if(Input.GetButtonUp("up")){
mySprite.DoAnim(5);
}
else if(Input.GetButtonUp("down")){
mySprite.DoAnim(4);
}
if(Input.GetButtonUp("Jump")){
rigidbody.AddForce(0,jumpspd,0);
}
}
I would like to make the same 4-way movement, but using the Character Controller. As you can see. for every one of the 4 directions I need to play a different animation (using sprite manager 2). And also there needs to be an idle animation for every direction, once the corresponding button has been released.
So preferably I would have the movement split up into four different keys, like I have right now, instead of the getAxis thing. Not sure if thats possible when using a character controller tho. Probably inefficient anyways.
Any help is appreciated!
Answer by Meater6 · Jul 16, 2011 at 05:44 AM
The character controller I think is the most useful and efficient component for characters. One thing you can make more efficient is to use the Input.GetAxis, which is much easier and simpler.
transform.Translate is one of the worst ways (I think) to move any physical object. For character controllers, you have Move(), by far better.
You can add directions depending on your input, for example:
Direction.z > 0 do anim(1)
Direction.z < 0 do anim(0)
Direction.x > 0 do anim(3)
Direction.x < 0 do anim(2)
It really is that simple. Hope this helps.
Your answer
Follow this Question
Related Questions
Character floats on backwards walk 0 Answers
Jaggie, uneven speed with basic movement script. 0 Answers
Move in the direction its facing C# 2 Answers
Make the player face his movement direction 10 Answers
How to make character face towards movement direction? 4 Answers