Search Unity

howcan I apply force to jump on my character controller please

Discussion in 'Physics' started by iTextures, Aug 11, 2017.

  1. iTextures

    iTextures

    Joined:
    Apr 24, 2014
    Posts:
    65
    hi ..
    i download FREE this UNITY scene ...
    but when press fire1 the jump animation run, but not the character controller
    i leave a video below ....
    just want to know how todo it on .cs

    i have something like this :

    if (Input.GetButton("Fire1")) animator.SetBool("Jump", true);

    the code is perfect....but how to assign to the character controller with force please.
    any help will apreciate so much

     
  2. BoogieD

    BoogieD

    Joined:
    Jun 8, 2016
    Posts:
    236
    I suspect you want the dude to jump up onto the box. He has a capsule collider which is stuck against the box while he does his animation about his origin which the collider's position is based on. So you need to make the character's GameObject position move high enough so his collider will clear the edge of the box so it can land on top.
    You can only use force on a Rigidbody which the character or capsule don't have. You usually either move things with a controller input effecting positions or by applying force on a RigidBody letting the physics engine move things for you. It is not usual practice to use physics to move animated characters because it is difficult to have nimble input control along with the effects of the physics engine. When you run the animation you need to move the character's position by assigning a new Vector3 to the character's transform.position. (the animation is not making anything jump. it is just changing the mesh)
    Do a search on 'how to make a gameObject jump up in an arc'. for a neat arc jump or 'How to smooth jump a gameObject' and add that to your code. You can crudely make his position go up giving you the chance to input him forward onto the box while in the air by using the line of code below but a smooth jump is what you really need. Lerp a sine would probably work.
    Code (CSharp):
    1. float jumpAmount = 1f;
    2. gameObject.transform.position = new Vector3( gameObject.transform.position.x, gameObject.transform.position.y + jumpAmount, gameObject.transform.position.z);
     
    Last edited: Aug 16, 2017
  3. iTextures

    iTextures

    Joined:
    Apr 24, 2014
    Posts:
    65
    thanks a lot ...i will try
     
  4. BoogieD

    BoogieD

    Joined:
    Jun 8, 2016
    Posts:
    236
    I just had a look at one of my old projects. Have a look at 'First Person Controller' in 'Standard Assets' and use that as a controller with animation. It has a Rigidbody component along withe the Capsule Collider. I'm pretty sure it is able to jump, probably using force on the Rigidbody. There are a number of different ways to handle this so it just depends. I think it has some issues walking up slopes but all can be tweaked depending on requirements. It will be more complex than a simple set-up but ideal way to study code and how all these things work together.