Search Unity

Charactercontroller Move, In Air control?

Discussion in 'Scripting' started by Tiles, Aug 6, 2011.

  1. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    I`ve grabbed the move example from the Scripting Reference. The original script from the reference can be found here: http://unity3d.com/support/documentation/ScriptReference/CharacterController.Move.html

    Then i tried to modify it so that i have still control over X and Z when i am in air. But i fail miserably. The character does not jump anymore with my modifications. Seems that i have missed to understand some vital parts of it.

    This is my latest try to modify it, to get it to work with in air controls. Of course the wrong approach. As told, the jumping fails now:

    Code (csharp):
    1. var speed : float = 6.0;
    2. var jumpSpeed : float = 8.0;
    3. var gravity : float = 20.0;
    4. var rotateSpeedGrounded : float = 3; // Rotation speed at ground
    5.  
    6. private var moveDirection : Vector3 = Vector3.zero;
    7.  
    8. function Update() {
    9. var controller : CharacterController = GetComponent(CharacterController);
    10.         if (controller.isGrounded) {
    11.                 // We are grounded, so recalculate move direction directly from axes. Z is movement direction
    12.                 moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
    13.                 moveDirection = transform.TransformDirection(moveDirection);
    14.                 moveDirection *= speed;
    15.                 // Rotate around y - axis. This is to change direction
    16.                 transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeedGrounded, 0);
    17.                
    18.         if (Input.GetButton ("Jump")) {
    19.                 moveDirection.y = jumpSpeed;
    20.                 }
    21.                 }
    22. // ---------------------------------------------------- Does not jump anymore ----------------------------
    23.         else{
    24.                 // We are not grounded
    25.                 moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
    26.                 moveDirection = transform.TransformDirection(moveDirection);
    27.                 moveDirection *= speed;
    28.                 // Rotate around y - axis. This is to change direction
    29.                 transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeedGrounded, 0);
    30.                 }
    31. // ---------------------------------------------------------------------------------------------        
    32.         // Apply gravity
    33.                 moveDirection.y -= gravity * Time.deltaTime;
    34.         // Move the controller
    35.                 controller.Move(moveDirection * Time.deltaTime);
    36. }
    Where am i wrong? How do i modify the script so that i have some in air control when i jump?
     
  2. Mr.Smart

    Mr.Smart

    Joined:
    Aug 5, 2011
    Posts:
    54
    it`s calculate , your char on ground or not if he is on ground its mean he can jump if it isn't jump will not work
    Code (csharp):
    1.  
    2. if (controller.isGrounded) {
    3.                 // We are grounded, so recalculate move direction directly from axes. Z is movement direction
    4.                 moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
    5.                 moveDirection = transform.TransformDirection(moveDirection);
    6.                 moveDirection *= speed;
    7.                 // Rotate around y - axis. This is to change direction
    8.                 transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeedGrounded, 0);
    9.                
    10.         if (Input.GetButton ("Jump")) {
    11.                 moveDirection.y = jumpSpeed;
    12.                 }
    13.                 }
    14. // ---------------------------------------------------- Does not jump anymore
    15.  
     
  3. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    I`m a bit in trouble with your english, sorry. I don`t really understand what you mean.

    What i want to do is to jump. And i want to have movement control when i jump. Which is not the case with the original script.
     
  4. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    You don't need to add an else statement. Just take the movement code and move it outside of the 'controller.isGrounded' clause, so it will move all the time.
     
  5. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Ah, now i get it. Thanks folks :)

    Hm, but what if i want to have a bit different controls than the ones at ground? What do i need to do then?
     
  6. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Nope. This does not work. When i remove the isGrounded statement then i am back at my initial problem. The character does not jump anymore.
     
  7. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    One step further, but still not where i want to be. I was at least able to make the rotation part working. The movement part refuses to work though.

    Code (csharp):
    1. var speed : float = 6.0;
    2. var jumpSpeed : float = 8.0;
    3. var gravity : float = 20.0;
    4. var rotateSpeedGrounded : float = 3; // Rotation speed at ground
    5.  
    6. private var moveDirection : Vector3 = Vector3.zero;
    7.  
    8. function Update() {
    9.  
    10. var controller : CharacterController = GetComponent(CharacterController);
    11. if (controller.isGrounded) {
    12.         // We are grounded, so recalculate move direction directly from axes. Z is movement direction
    13.         moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
    14.         moveDirection = transform.TransformDirection(moveDirection);
    15.         moveDirection *= speed;
    16.         // Rotate around y - axis. This is to change direction
    17.         transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeedGrounded, 0);
    18.  
    19.     if (Input.GetButton ("Jump")) {
    20.         moveDirection.y = jumpSpeed;
    21.             //
    22.         }
    23.         }
    24.     //We are in air
    25.     if (!controller.isGrounded) {
    26.         // Rotate around y - axis. This is to change direction
    27.         transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeedGrounded*0.3, 0);
    28.         moveDirection.z = Input.GetAxis("Vertical"); //This does not work as thought.
    29.         }
    30.     // Apply gravity
    31.         moveDirection.y -= gravity * Time.deltaTime;
    32.     // Move the controller
    33.         controller.Move(moveDirection * Time.deltaTime);
    34. }
    moveDirection.z = Input.GetAxis("Vertical"); moves everywhere but the z direction of my sprite. And without the initial speed.
     
  8. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    I didn't say to remove the isGrounded statement - I said to move the moveDirection code *outside* of the isGrounded statement.
     
  9. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Thanks for the advice. In fact that`s what i did, and it doesn`t jump anymore then. This code does not work. Nor does it work when i put just parts of it outside the isGrounded statement. Which i also tried. I have so far put every line inside the original isGrounded statement outside of it, one by one, and in all possible combinations and in front and behind the statemen. No luck. The code from above is the closest to working as i could get.

    This one does not jump:

    Code (csharp):
    1. var speed : float = 6.0;
    2. var jumpSpeed : float = 8.0;
    3. var gravity : float = 20.0;
    4. var rotateSpeedGrounded : float = 3; // Rotation speed at ground
    5.  
    6. private var moveDirection : Vector3 = Vector3.zero;
    7.  
    8. function Update() {
    9.  
    10. var controller : CharacterController = GetComponent(CharacterController);
    11.  
    12.         moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
    13.         moveDirection = transform.TransformDirection(moveDirection);
    14.         moveDirection *= speed;
    15.        
    16.         // Rotate around y - axis. This is to change direction
    17.         transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeedGrounded, 0);
    18.        
    19.     if (controller.isGrounded) {
    20.         // We are grounded, so recalculate move direction directly from axes. Z is movement direction
    21.     if (Input.GetButton ("Jump")) {
    22.         moveDirection.y = jumpSpeed;
    23.             //
    24.         }
    25.         }
    26.     // Apply gravity
    27.         moveDirection.y -= gravity * Time.deltaTime;
    28.     // Move the controller
    29.         controller.Move(moveDirection * Time.deltaTime);
    30. }
     
  10. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    The above code should work -- are you trying to get him to jump while he's in the air as well?
     
  11. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Yeah, that`s amazing, isin`t it? It looks like it should work. But doesn`t :)

    Of course i don`t want to jump when in air. But i want to jump when at ground. And this refuses to work now. It shakes a bit, but does not lift off.
     
  12. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Back to the closest solution so far. As told, this one does nearly work, except the forward direction. When i am in air and press the forward button then my character moves sidewards. Randomly sidewards. Not aligned to the world, not aligned to the object, but very random, and somehow connected with the rotation. Why? And how can i get it to move forward instead?

    Code (csharp):
    1. var speed : float = 6.0;
    2. var jumpSpeed : float = 8.0;
    3. var gravity : float = 20.0;
    4. var rotateSpeedGrounded : float = 3; // Rotation speed at ground
    5.  
    6. private var moveDirection : Vector3 = Vector3.zero;
    7.  
    8. function Update() {
    9.  
    10. var controller : CharacterController = GetComponent(CharacterController);
    11. if (controller.isGrounded) {
    12.         // We are grounded, so recalculate move direction directly from axes. Z is movement direction
    13.         moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
    14.         moveDirection = transform.TransformDirection(moveDirection);
    15.         moveDirection *= speed;
    16.         // Rotate around y - axis. This is to change direction
    17.         transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeedGrounded, 0);
    18.  
    19.     if (Input.GetButton ("Jump")) {
    20.         moveDirection.y = jumpSpeed;
    21.             //
    22.         }
    23.         }
    24.     //We are in air
    25.     if (!controller.isGrounded) {
    26.         // Rotate around y - axis. This is to change direction
    27.         transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeedGrounded*0.3, 0);
    28.         moveDirection.z += Input.GetAxis("Vertical"); //This does not work as thought.
    29.         }
    30.     // Apply gravity
    31.         moveDirection.y -= gravity * Time.deltaTime;
    32.     // Move the controller
    33.         controller.Move(moveDirection * Time.deltaTime);
    34. }
     
  13. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Got it. Sometimes the solution is this simple that you simply overlook it :D

    I already use transform.Rotate. So i started some in air experiments with transform.Translate. And that was it. My player accellerates and decellerates as thought now when it is jumping and i press the vertical axis. I hope that this solution is a valid one, and doesn`t make trouble at a later point. But it looks really good so far :)

    Here the code:

    Code (csharp):
    1. var speed : float = 6.0;
    2. var jumpSpeed : float = 8.0;
    3. var gravity : float = 20.0;
    4. var rotateSpeedGrounded : float = 3; // Rotation speed at ground
    5.  
    6. var inAirSpeed: float = 2.0; // The movementspeed in air
    7. var inAirRotation : float = 0.8; // Rotation speed in air
    8.  
    9. private var moveDirection : Vector3 = Vector3.zero;
    10.  
    11. function Update() {
    12.  
    13. var controller : CharacterController = GetComponent(CharacterController);
    14.  
    15. // ---------------------------------------- At Ground -----------------------------------------
    16.         if (controller.isGrounded) {
    17.                         // We are grounded, so recalculate move direction directly from axes. Z is movement direction
    18.                         moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
    19.                         moveDirection = transform.TransformDirection(moveDirection);
    20.                         moveDirection *= speed;
    21.                         // Rotate around y - axis. This is to change direction
    22.                         transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeedGrounded, 0);
    23.  
    24.                 if (Input.GetButton ("Jump")) {
    25.                         moveDirection.y = jumpSpeed;
    26.                                 //
    27.                         }
    28.                         }
    29. // ---------------------------------------------------- In Air  ----------------------------
    30.         else{
    31.                 // We are not grounded. We can influence the movementspeed with the vertical axis
    32.                 transform.Translate(0, 0, Input.GetAxis("Vertical")* Time.deltaTime*inAirSpeed);
    33.                 // Rotate around y - axis. This is to change direction
    34.                 transform.Rotate(0, Input.GetAxis ("Horizontal") * inAirRotation, 0);
    35.                 }
    36. // ---------------------------------------------------------------------------------------------        
    37.         // Apply gravity
    38.                 moveDirection.y -= gravity * Time.deltaTime;
    39.         // Move the controller
    40.                 controller.Move(moveDirection * Time.deltaTime);
    41. }
     
  14. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    And that one`s another fail. Now i can jump through walls :(
     
  15. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    Yeah, that's why you don't want to use the Translate method - it doesn't take physics into account.

    What you're missing here is the difference between local and global space - Translate works because it will move you forward along the transform's local axis - however, using Move works in global space. So to get Move to work, you have to get the transform's local forward rather than the global Vector3.forward. You can do this using your transform's forward variable, like so:

    Code (csharp):
    1. controller.Move(transform.forward);
    Hope that makes sense.
     
  16. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Thanks FlamingHairball :)

    I get the idea behind. And it makes sense. I`ve long found out that it might be a local vs global space problem. But i am still totally lost to write a working solution, unfortunately. I miss the grammar so to speak :)
     
  17. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Another dead end. This one gives me control about acceleration and decceleration when i am in air. But now changing the direction doesn`t work anymore when i`m in air. The player rotates around itself now instead really change the direction. And that way i have two controller.Move statements in the code, which is a bad thing when i read the manual correct.
    Code (csharp):
    1. var speed : float = 6.0;
    2. var jumpSpeed : float = 8.0;
    3. var gravity : float = 20.0;
    4. var rotateSpeedGrounded : float = 3; // Rotation speed at ground
    5.  
    6. var inAirSpeed: float = 2.0; // The movementspeed in air
    7. var inAirRotation : float = 0.8; // Rotation speed in air
    8.  
    9. private var moveDirection : Vector3 = Vector3.zero;
    10.  
    11. function Update() {
    12.  
    13. var controller : CharacterController = GetComponent(CharacterController);
    14.  
    15. // ---------------------------------------- At Ground -----------------------------------------
    16.         if (controller.isGrounded) {
    17.                         // We are grounded, so recalculate move direction directly from axes. Z is movement direction
    18.  
    19.                         moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
    20.                         moveDirection = transform.TransformDirection(moveDirection);
    21.                         moveDirection *= speed;
    22.                         // Rotate around y - axis. This is to change direction
    23.                         transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeedGrounded, 0);
    24.  
    25.                 if (Input.GetButton ("Jump")) {
    26.                         moveDirection.y = jumpSpeed;
    27.                                 //
    28.                         }
    29.                         }
    30. // ---------------------------------------------------- In Air  ----------------------------
    31.         else{
    32.                 // We are not grounded. We can influence the movementspeed with the vertical axis
    33.                 controller.Move(transform.forward * Input.GetAxis("Vertical")/10*inAirSpeed);
    34.                 transform.Rotate(0, Input.GetAxis ("Horizontal") * inAirRotation, 0);
    35.                 }
    36. // ---------------------------------------------------------------------------------------------        
    37.         // Apply gravity
    38.                 moveDirection.y -= gravity * Time.deltaTime;
    39.                
    40.         // Move the controller
    41.                 controller.Move(moveDirection * Time.deltaTime);
    42. }
    HALP :(
     
  18. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    Don't worry about it! This is pretty complex stuff when you're just starting out - it takes quite a bit of time, effort, and practice to get to where you actually know what you're doing. :)
    You don't need two moves. Alter the moveDirection variable rather than directly calling Move().

    Code (csharp):
    1.  
    2. function Update(){
    3.      var cc = CharacterController;
    4.      var moveDirection : Vector3;
    5.      var rotation : Vector3;
    6.      if(cc.isGrounded{
    7.           moveDirection = transform.forward * speed * Input.GetAxis("Vertical"); //equivalent to calling transform.TransformDirection(Vector3.forward)
    8.           rotation = Vector3(0, Input.GetAxis("Horizontal") * rotateSpeedGrounded, 0);
    9.           if(Input.GetButton("Jump")) {
    10.                 moveDirection.y = jumpSpeed;
    11.           }
    12.      } else { //if we're not grounded
    13.            moveDirection = transform.forward * Input.GetAxis("Vertical") * inAirSpeed;
    14.            rotation = Vector3(0, Input.GetAxis("Horixontal") * inAirRotation, 0);
    15.      }
    16.      moveDirection.y -= gravity * Time.deltaTime;
    17.      
    18.      transform.Rotate(rotation);
    19.      cc.Move(moveDirection * Time.deltaTime;
    20. }
    21.  
    22.  
    This was typed in a browser, so you may have to muck with it a bit to get it to compile.
    If you have any questions about how or why it works, please post back and ask them here! If you're using code you don't understand, you won't be able to debug it if you have problems later on. :)
     
  19. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    A complete and hopefully working example to study, yay :D

    Thanks FlamingHairball. Will give it a go and try to get it to work. And will have a closer look to understand how it works. That`s indeed the most important part at it :)

    What`s a bit frustrating is the fact that there are so few useful examples around that deals with this stuff. Either there are just the single bits available, or the examples are this complex that you need weeks to strip it down to the parts you want to understand. I have the strange feeling that the charactercontroller is not really often used.
     
  20. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Hm, cannot get it to work. line 17 Cannot cast from source type to destination type.

    EDIT, okay, i got it to work by some small modifications. But it doesn`t jump anymore. We go in circles here, from one not working example to another not working example ...

    Code (csharp):
    1. var speed : float = 6.0;
    2. var jumpSpeed : float = 8.0;
    3. var gravity : float = 20.0;
    4. var rotateSpeedGrounded : float = 3; // Rotation speed at ground
    5.  
    6. var inAirSpeed: float = 2.0; // The movementspeed in air
    7. var inAirRotation : float = 0.8; // Rotation speed in air
    8.  
    9. private var moveDirection : Vector3 = Vector3.zero;
    10.  
    11. function Update(){
    12. var cc: CharacterController = GetComponent(CharacterController);
    13.      var moveDirection : Vector3;
    14.      var rotation : Vector3;
    15.      if(cc.isGrounded){
    16.           moveDirection = transform.forward * speed * Input.GetAxis("Vertical"); //equivalent to calling transform.TransformDirection(Vector3.forward)
    17.           rotation = Vector3(0, Input.GetAxis("Horizontal") * rotateSpeedGrounded, 0);
    18.           if(Input.GetButton("Jump")) {
    19.                 moveDirection.y = jumpSpeed;
    20.           }
    21.      } else { //if we're not grounded
    22.            moveDirection = transform.forward * Input.GetAxis("Vertical") * inAirSpeed;
    23.            rotation = Vector3(0, Input.GetAxis("Horizontal") * inAirRotation, 0);
    24.      }
    25.      moveDirection.y -= gravity * Time.deltaTime;
    26.      
    27.      transform.Rotate(rotation);
    28.      cc.Move(moveDirection * Time.deltaTime);
    29. }
     
    Last edited by a moderator: Aug 8, 2011
  21. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    Alright, two things to check:

    1) Check your Input settings. What button is assigned to "Jump"
    2) Check the script in the inspector. What is jumpSpeed set to?
     
  22. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Spacebar, and 8, as defined in the script by var jumpSpeed : float = 8.0; :)

    It starts to jump, but it does not lift off. Most probably because the jump value gets overwritten by the Vector 3 movement value in Y direction. That`s the same problem that occurs when you put the whole movement code outside of the isGrounded statement in the original script.

    I was meanwhile able to get the missing bit by community members from the german community, and to get the code to work. Not exactly in the way i wanted it, but good enough for the initial problem, to have some control while jumping. Making games is made of workarounds anyways :)

    The missing bit was this line here:

    Code (csharp):
    1. moveDirection += transform.TransformDirection(Vector3(Input.GetAxis ("Horizontal") * Time.deltaTime*inAirDrift, 0, Input.GetAxis("Vertical")* Time.deltaTime*inAirSpeed));
    This adds to the current moveDirection. And by that i can control forward and sideward when i am in air. This method puts transform.Rotate out of order though, like my own latest attempt too. transform.Rotate now only works local, not global. Means the character rotates around itself instead changing the direction. Well, i could combine the two methods, and rotate the character when he drifts sidewards. But it`s not really the same than really changing the direction. That`s why i have commented this method out in the code. It is still there though in case somebody wants to play around with it.

    The final code looks like this now:

    Code (csharp):
    1. var speed : float = 6.0;
    2. var jumpSpeed : float = 8.0;
    3. var gravity : float = 20.0;
    4. var rotateSpeedGrounded : float = 3; // Rotation speed at ground
    5.  
    6. var inAirSpeed: float = 2.0; // Adds or substracts to the movementspeed in air
    7. var inAirDrift : float = 0.5; // Adds Sidewards drift in air
    8. //var inAirRotation : float = 0.5; // Rotation speed in air. Uncomment this to let the player change the face direction in air
    9.  
    10. private var moveDirection : Vector3 = Vector3.zero;
    11.  
    12. function Update() {
    13.     var controller : CharacterController = GetComponent(CharacterController);
    14. // ---------------------------------------- At Ground -----------------------------------------
    15.         if (controller.isGrounded) {
    16.                         // We are grounded, so recalculate move direction directly from axes. Z is movement direction
    17.  
    18.                         moveDirection = Vector3(0, 0, Input.GetAxis("Vertical"));
    19.                         moveDirection = transform.TransformDirection(moveDirection);
    20.                         moveDirection *= speed;
    21.                         // Rotate around y - axis. This is to change direction
    22.                         transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeedGrounded, 0);
    23.  
    24.                 if (Input.GetButton ("Jump")) {
    25.                         moveDirection.y = jumpSpeed;
    26.             }
    27.             }
    28. // ---------------------------------------------------- In Air  ----------------------------
    29.         else{
    30.                 // We are not grounded. We can still influence the movement with the horizontal and vertical axis, but not so strong.
    31.                 moveDirection += transform.TransformDirection(Vector3(Input.GetAxis ("Horizontal") * Time.deltaTime*inAirDrift, 0, Input.GetAxis("Vertical")* Time.deltaTime*inAirSpeed));
    32.                 //transform.Rotate(0, Input.GetAxis ("Horizontal") * inAirRotation*0.5, 0); // Uncomment this to let the player change the face direction in air
    33.         }
    34. // ---------------------------------------------------------------------------------------------        
    35.         // Apply gravity
    36.                 moveDirection.y -= gravity * Time.deltaTime;
    37.         // Move the controller
    38.                 controller.Move(moveDirection * Time.deltaTime);
    39. }
    I wish this example would be in the manual or the wiki. This would`ve saved me lots of headache!

    Either there is just examples for the single bits. Or full featured examples where you need weeks to isolate the parts that you want to learn about, when you are even able to isolate the parts. Hm, is there a special procedure to put such a script onto the wiki? I have the feeling that i am not the only one who would benefit from this solution :)

    Anyways, thanks for all the help :)
     
    Last edited by a moderator: Aug 9, 2011
  23. flaminghairball

    flaminghairball

    Joined:
    Jun 12, 2008
    Posts:
    868
    If I remember correctly, there's a tutorial on making a third person platformer that has a player script that supports in-air movement, along with some other nice stuff like double jumps.
     
  24. Tiles

    Tiles

    Joined:
    Feb 5, 2010
    Posts:
    2,481
    Thanks for the hint to the lerpz tutorial. It`s indeed nice to have such an advanced project as a tutorial to study, that`s true. And this tutorial has teached me a lot, i`ve gone through it. But it`s already too much at once, unfortunately. 500 lines of code for just the player script is a completely newbie killer.

    I even tried to adopt it, tried to find my way through it. I had my success moments. I was able to manage to add one of my own animations. But i had to give up in the end. The playerscript of this tutorial is this packed with stuff, and the whole movement engine goes through this much functions and values and even external scripts that it is absolutely impossible to find out what does what. Same goes for the scripts at the Unify Wikipage. All nice movement scripts, i tried to study it. But it`s already too much for a beginner like me. That`s why i started to write my own playerscript at all. I was totally lost, and search for the basics here :)
     
  25. MANISH KUMAR

    MANISH KUMAR

    Joined:
    Nov 22, 2010
    Posts:
    57
    public class CharacterMovement : MonoBehaviour {

    // Use this for initialization
    public CharacterController controller;
    public float speed;
    public float rotationspeed;
    public float jump_height;
    private Vector3 movedirection=new Vector3(0,0,0);
    public float gravity=20.0f;
    public float maxFallSpeed;
    public bool ismoving;
    public float hangtime=0.0f;
    public Vector3 inAirVelocity=new Vector3(0.0f,0.0f,0.0f);
    public float inAirControlAcceleration;
    public float air_factor;
    public bool isjumping=false;
    public float lastYSpeed=0.0f;
    public bool grounded = false;
    void Start () {



    }

    // Update is called once per frame
    void Update () {
    controller=GetComponent<CharacterController>();
    //ApplyGravity();
    float x=Input.GetAxis("Horizontal");
    float z=Input.GetAxis("Vertical");
    ismoving=(x!=0.0f || z!=0.0f);



    if(controller.isGrounded)
    {
    movedirection=new Vector3(Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical"));
    movedirection=transform.TransformDirection(movedirection);
    movedirection*=speed;
    transform.Rotate(0,Input.GetAxis("Horizontal")*rotationspeed,0);
    hangtime=0.0f;
    inAirVelocity=Vector3.zero;
    if(Input.GetButtonDown("Jump"))
    {
    Debug.Log("Simple Jump");
    movedirection.y=CalculateJumpVerticalSpeed(jump_height);
    }
    ismoving=false;
    }
    else
    { movedirection = new Vector3(Input.GetAxis("Horizontal"), lastYSpeed, Input.GetAxis("Vertical"));

    movedirection = transform.TransformDirection(movedirection);



    // Remove speed factor from influencing Y value

    movedirection.x *= speed;

    movedirection.z *= speed;

    }
    movedirection.y -= gravity * Time.deltaTime;
    lastYSpeed = movedirection.y;

    var flags = controller.Move(movedirection* Time.deltaTime);
    grounded = (flags CollisionFlags.CollidedBelow) != 0;

    }
    void ApplyGravity () {
    if (controller.isGrounded)
    movedirection.y = -gravity * Time.deltaTime;
    else
    movedirection.y -= gravity * Time.deltaTime;

    // Make sure we don't fall any faster than maxFallSpeed. This gives our character a terminal velocity.
    movedirection.y = Mathf.Max (movedirection.y, -maxFallSpeed);
    }
    float CalculateJumpVerticalSpeed ( float targetJumpHeight)
    {
    return Mathf.Sqrt (2 * targetJumpHeight * gravity);
    }

    void DidJump () {
    animation.Play ("jump");
    animation.PlayQueued ("jumpFall");
    }
    }







    ////////////////////
    hERE IN THIS CODE
    *************
    { movedirection = new Vector3(Input.GetAxis("Horizontal"), lastYSpeed, Input.GetAxis("Vertical"));

    movedirection = transform.TransformDirection(movedirection);



    // Remove speed factor from influencing Y value

    movedirection.x *= speed;

    movedirection.z *= speed;

    }
    ///////////
    you insert simply rotation code and it's work Thanks for this........
     
  26. iuripujol

    iuripujol

    Joined:
    Nov 26, 2013
    Posts:
    10
    It is a lot easier than you thing...Here is the solution

    1. float v = Input.GetAxis ("Vertical");
    2. float h = Input.GetAxis ("Horizontal");
    3. float Angle = Mathf.Atan2 (h,v) * Mathf.Rad2Deg;
    4. Vector3 m = transform.forward * new Vector3 (h, 0, v).magnitude;
    5. transform.eulerAngles = new Vector3 (0, cam.eulerAngles.y + Angle, 0);//use external rotation
    6. if (cc.isGrounded) {
    7. move = m ;
    8. move *= Input.GetKey (KeyCode.LeftShift) ? 1 : .5f;
    9. if (Input.GetKey (KeyCode.Space))
    10. move.y = JumpSpeed;
    11. } else {
    12. move = new Vector3(m.x, move.y, m.z);
    13. }
    14. move.y -= 3 * Time.deltaTime;//gravity
    15. cc.Move (move * MoveSpeed * Time.deltaTime);
     
  27. iuripujol

    iuripujol

    Joined:
    Nov 26, 2013
    Posts:
    10
    Final solution for 3rd Person character:

    float v = Input.GetAxis ("Vertical");
    float h = Input.GetAxis ("Horizontal");

    Vector3 m = Quaternion.Euler(0, cam.eulerAngles.y, 0) * new Vector3 (h, 0, v).normalized;//move direction
    m *= Input.GetKey(KeyCode.LeftShift) ? 1 : .5f;
    Vector3 dir = transform.InverseTransformDirection (m);
    float Turn = Mathf.Atan2 (dir.x, dir.z);
    transform.Rotate (0, Turn * 20, 0);//handle rotation

    if (cc.isGrounded) {
    move = m;

    if (Input.GetKey (KeyCode.Space))
    move.y = JumpSpeed;
    } else {
    move = new Vector3(m.x, move.y, m.z);
    }

    move.y -= 3 * Time.deltaTime;//gravity

    cc.Move (move * MoveSpeed * Time.deltaTime);
     
    Last edited: Jun 20, 2018