Search Unity

add force for a character controller

Discussion in 'Scripting' started by amit1532, Jul 8, 2011.

  1. amit1532

    amit1532

    Joined:
    Jul 19, 2010
    Posts:
    305
    hey, im trying to make a 2d platformer and im using a character controller.
    so far the movement and the isGrounded and the slope limit is what makes me use a Character controller player and not a rigidbody.
    but, when i want the player to get a knock back when hits enemy, i cant do it. because ill need to call the knock back movement every frame. so i will need to make another Vector2 and another Move() function. instead of just doing:
    rigidbody.AddForce(x, y, 0)... which is just one frame calling and it do the job.
    the player have many skills that adds forces which will end up in 30 move() functions..
    is there any way to call AddForce from a rigidbody while im using Character Controller as my main movement tool?
    or is there another way to do it with a Character Controller only?
    because i cant get isGrounded properly on a rigidbody (if you touch a wall, it will think its a floor and let you jump)
    also the slope limit is awesome and on a rigidbody i wont have thos things.
    any help?
     
  2. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    I would do something like this:

    Code (csharp):
    1.  
    2. //this script goes on the player
    3. public var speed : float = 15.0;
    4. public var duration : float = 0.25;
    5.  
    6. private var controller : CharacterController;
    7.  
    8. function Start(){
    9.     controller = GetComponent(CharacterController);
    10. }
    11.  
    12. function Knockback(direction : Vector3){
    13.     var startTime = Time.time;
    14.     while(Time.time < (startTime + duration)){
    15.         controller.SimpleMove(direction*speed);
    16.         yield;
    17.     }
    18. }
    19.  
     
  3. scratch250

    scratch250

    Joined:
    Mar 28, 2011
    Posts:
    91
    im not an expert :) but cant you just make an coroutine where you apply the force in a limted time and then call it under FixedUpdate/Update :)
     
  4. PrimeDerektive

    PrimeDerektive

    Joined:
    Dec 13, 2009
    Posts:
    3,090
    See above ;)
     
  5. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    as legend did... is valid..

    what i personally do is to bake and interval you can imagine this is really easy and functional....

    just create an empty
    add an animation to it .. this animation is the path that your character must to follow..
    set your character as a child of that empty,
    play the empty animation.. what do we have? a moving character according the empty path :)
     
  6. amit1532

    amit1532

    Joined:
    Jul 19, 2010
    Posts:
    305
    thanks legend it works!
    you have changed my way to script now lol
     
  7. argenzio777

    argenzio777

    Joined:
    Dec 18, 2011
    Posts:
    7
    How to call this function ?
    It give me error.
    This is my script when player is hit by enemy ( script is on enemy weapon).

    function OnTriggerEnter (hit : Collider)
    {
    if(hit.gameObject.tag == "Player")
    {
    hit.gameObject.GetComponent(Knockback);
    hit.SendMessage("Knockback", 100.0);
    }


    The error say that the value of send message Knockback are wrong....
     
  8. TaintedNobodies

    TaintedNobodies

    Joined:
    Feb 21, 2010
    Posts:
    76
    in the function that is suppose to receive the send message for knock back, do you have a variable for the function like

    function Knockbakc ( damage : float){
    // your code here?

    }
     
  9. Pilot

    Pilot

    Joined:
    Oct 13, 2011
    Posts:
    20
    I second the OnTriggerEnter question. If I would like the player to be the one "knocking back" other character controller NPC's would I achieve this with something like this?

     
    Last edited: Feb 2, 2012
  10. Chizambers

    Chizambers

    Joined:
    Jan 5, 2012
    Posts:
    7
    It also works with a transform.Translate. Was trying to figure out something similar and this is what I ended up with that worked.

    Code: