- Home /
have 3rd person make a side jump
Hi All,
I am trying to make my player side jump since a few days. For the normal jump I use the classic:
if (Input.GetButtonDown("Jump"))
rigidbody.velocity = new Vector3(0, 10, 0);
but I cant find any way to have him jump and move side ways (left or right)
I tried affecting Velocity, AddForce, AddRelativeForce
,... using Vector3.right, (float x, float y, float z)
,... but whatever I try, he never moves side ways with those.
If any of you would have a hint on how to do this, it would greatly help me.
Thanks
Answer by vexe · Sep 12, 2013 at 04:35 PM
I assume that you're not using the standard character controller to learn and implement everything on your own. Check out this series from Quilly where he makes a character controller from scratch, he covers a lot of things in really great detail, I'm sure you will resolve your problem there. Helped me a lot, hope it does the same for you :)
EDIT: OK so well Quilly didn't cut it for you, that's why we're here :)
It was easier than I thought, I did it with a rigidbody
attached to my player, it worked really nice for me. Here you go:
// This enum is for convenience, you can go without it if you want, I like to stay organized :)
public enum DodgeDirection { Right, Left, Forward, Backward }
// This vector is essential if you want to have variants dodging amounts/forces, it represents how much the player will go to the side, up and forward
// left: -x, right: +x, up: +y, down: -y, forward: +z, backward: -z. Don't worry you'll understand in a moment
// if you don't want to have different forces applied to your player when he dodges left/right, forward/backward, just get rid of this vector and use a float variable instead
public Vector3 dodge = new Vector3(5, 5, 5);
// This is the dodging method, you just give it a direction and it will handle the rest using the `dodge` vector we previously defined.
public void Dodge(DodgeDirection dir)
{
switch (dir)
{
case DodgeDirection.Right:
rigidbody.AddForce(_transform.right * dodge.x + _transform.up * dodge.y, ForceMode.Impulse);
break;
case DodgeDirection.Left:
rigidbody.AddForce(-_transform.right * dodge.x + _transform.up * dodge.y, ForceMode.Impulse);
break;
case DodgeDirection.Forward:
rigidbody.AddForce(_transform.forward * dodge.z + _transform.up * dodge.y, ForceMode.Impulse);
break;
case DodgeDirection.Backward:
rigidbody.AddForce(-_transform.forward * dodge.z + _transform.up * dodge.y, ForceMode.Impulse);
break;
}
}
// Since we're gonna be dealing with a rigidbody and forces, we might as well use FixedUpdate
// I used 'l', 'j', 'i' and 'k' to dodge left, right, forward and backward (respectively)
// If I were you and using WASD controls, I would dodge left by double tapping 'A', right by double tapping 'D' etc. (With a threshold I define)
void FixedUpdate()
{
if (Input.GetKeyDown("l"))
Dodge(DodgeDirection.Right, dodge);
if (Input.GetKeyDown("j"))
Dodge(DodgeDirection.Left, dodge);
if (Input.GetKeyDown("i"))
Dodge(DodgeDirection.Forward, dodge);
if (Input.GetKeyDown("k"))
Dodge(DodgeDirection.Backward, dodge);
}
Couple of notes:
In my test environment, I was using a capsule collider for my player so I had to constrain all the rotations in the
rigidbody
component, because since I'm adding force, the capsule would get knocked out and thrown to the ground (kinda cool effect if you want fighting in your game) - So no rotation from therigidbody
, I handled rotation myself.I tried all the possible types of forces, only
Impulse
andVelocityChange
seemed to do the trick, whileForce
andAcceleration
didn't seem to have any effect.Wondering about
_transform
? - I always like to cache mytransform
component if I'm gonna be using it a lot, because when you dotransform
, you're actually callingGetComponent<Transform>()
- which isn't nice to call so many times. (I usually cache inStart
orAwake
)Make sure you handle the rotation of your player well, let me know if you have trouble with that - You could use Quilly's code for that, works like charm.
Let me know how it goes, hope I helped :)
thanks, I'll definitly check it out! Indeed, beeing new in coding & in unity, I find it much more interesting to code everything from scratch than to take premade scripts
ok, just 'eated' all of those videos already! but I'm afraid it didnt answer my question. Would any one have an idea on how to implement this?
What do you mean? - with Quilly's character controller, you can't jump the way you want? - why don't you just jump and go side ways? - or that's not how you want it. You wanna be able to press like 'd' and then space, and your character would make a jump just to the right? is that what you want?
Actually, my goal would be to make a dodge movement.
I have no problem with the normal Jump and basic movements.
indeed your last sentence explains kind of what I need.
if player press jump + side (left or right) he would make a similar jump action with an additional side movement. $$anonymous$$y problem is that I cant use translate as I would like to make this dodge with a mouse click+side button. Once these buttons are pressed, this movement should go untill the end of this move, event if the buttons are released. but translate would only work during the click is done, and click is done just 'during' one frame
I'm sorry if I wasn't clear enough before (English is not my natural language)
Thanks alot! some verry interesting triks (for example _transfrom that i've read elsewhere, but did not understood the advantage before)
before 'simply' copying your hole code, and to test it (not that I dont trust you ;-) ) I just tried with:
if (Input.Get$$anonymous$$ouseButtonDown(2)){ rigidbody.AddForce(transform.right 8 + transform.up 8 , Force$$anonymous$$ode.Impulse);}
nothing else was changed to my code
But I actually get the same result I did before posting this question; only y movement is applied but the .right movement is not affected
realtime EDIT: Oh my god... I feal incredibly ashamed! I frozed my rigidBody X & Z Position some time ago... unticked it; tested... SOLVED...
I was using this to prevent my player sliding down hills if Idle. Guess I'll have to code that.
So from the beggining, I tried lots of code and forgot completely about that! Though I still thought that coding movement overrided that "freeze Poistion"
at least I learned a few verry intersting things, and understood once again to triple check everything!
Hope this discution will still give hints for others that me at least
thanks again for your pressious help
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Extend simple movement script (C#) 1 Answer
Can't get character to Jump on the Y Axis (C#) 2 Answers
[C#] Jump on slopes 1 Answer
I cant make my character jump ?,Why can't he jump ? 0 Answers