Search Unity

crazy leaning issue!

Discussion in 'Scripting' started by Dsavage13, Nov 26, 2015.

  1. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    so i am making a lean script for my game...simple right? it seems to work, lets see, ou hit q or e, the camera tilts left or right. oh ok. i see.
    but wait.... THE CHARACTER ZOOMS ACROSS THE MAP IN THE DIRECTION OPPOSITE THE LEAN!

    I mean if i was going to have a fiddler crab as my player character this would be great. but i would like a human as the player.. that being said... any ideas???

    heres the script:

    Code (CSharp):
    1. // leaning code dosent actually lean out, just turns camera
    2.  
    3.         float targetZ = (transform.rotation.eulerAngles.z > 25f ? 360f : 0f);
    4.  
    5.         if (Input.GetKey ("q"))
    6.         {
    7.             targetZ = (transform.rotation.eulerAngles.z > 25f ? 360f + leanangle : leanangle);
    8.         }
    9.  
    10.         else if (Input.GetKey ("e"))
    11.         {
    12.             targetZ = (transform.rotation.eulerAngles.z > 25f ? 360f - leanangle : -leanangle);
    13.         }
    14.  
    15.         if (transform.localRotation.eulerAngles.z != targetZ)
    16.         {
    17.             float angle = Mathf.Lerp(transform.rotation.eulerAngles.z, targetZ, leanspeed * Time.deltaTime);
    18.             transform.rotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, angle);
    19.         }
    20.  

    any advice would be appreciated. i cant figure it out! lol.
     
  2. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    ...anyone?
     
  3. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    ...no one? whered the help go on these forums...
     
  4. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Do you have a GIF showing what's going on?
     
  5. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    know how i can do that? lol not too good with video capturing
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Nothing here should explicitly move your transform. Start looking at the other scripts attached to the same GameObject. There may be some weird interaction going on.

    For example you are tilting a character controller that is using transform.up for gravity, and by rotating it you start falling sideways.
     
  7. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    interesting... maybe my movement code?

    Code (CSharp):
    1.         //movement
    2.         float forwardspeed = Input.GetAxis("Vertical") * movementspeed;
    3.         float sidespeed = Input.GetAxis("Horizontal") * movementspeed;
    4.      
    5.         verticalVelocity += Physics.gravity.y * Time.deltaTime*2;
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    How does this code actually touch transform.position?
     
  9. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Use Gifcam.
     
  10. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    heres the rest of it, i forgot why but its separated
    Code (CSharp):
    1.         float lastHeight = charController.height;
    2.         charController.height = Mathf.Lerp (charController.height, h, Time.deltaTime*7);
    3.         pos.x = Player.position.x;
    4.         pos.y = Player.position.y + (charController.height - lastHeight) / 2;
    5.         pos.z = Player.position.z;
    6.         Player.position = pos;
    7.         Vector3 speed = new Vector3( sidespeed, verticalVelocity, forwardspeed );
    8.        
    9.         speed = transform.rotation * speed;
    10.        
    11.        
    12.         charController.Move ( speed * Time.deltaTime);

    maybe its in here
     
  11. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Try playing with + or - on your Player.position values when you're setting a new Player.position.
     
  12. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Try commenting this out. It seems your speed is directly affected by your rotation. Commenting it out will cause other issues, but if it stops crab movement, then you know where your issue is.

    Code (CSharp):
    1. speed = transform.rotation * speed;
     
  13. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    that did it. i commented that out, only bad thing was the w,a,s,d controls were backwards/messed up. any ideas on what i can do?
     
  14. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    That means you'll need to play around with + or - in order to "reverse" the "reversed movements" you already have.

    Or do (x * -1), where x is your movement values.
     
  15. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    ah, ok let me try that.
     
  16. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    ...yeah i have no idea where to do that... lol
     
  17. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Multiple options. This is where I would start

    Code (CSharp):
    1.         Vector3 speed = new Vector3( sidespeed, 0, forwardspeed );
    2.         speed = transform.rotation * speed;
    3.         speed.y = verticalSpeed;
    4.      
    5.         charController.Move ( speed * Time.deltaTime);
    Another alternative would be to apply the rotation in your lean script to a different object.
     
  18. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    i was going for that, but what other object is there? i want to eventually have it all animated and rigged so in multiplayer, you can see the character lean and have hitboxes, etc
     
  19. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    your code snippet worked great! it disabled my crouch,. but it still worked great! heres what ive got:
    Code (CSharp):
    1. //movement
    2.         float forwardspeed = Input.GetAxis("Vertical") * movementspeed;
    3.         float sidespeed = Input.GetAxis("Horizontal") * movementspeed;
    4.         verticalVelocity += Physics.gravity.y * Time.deltaTime*2;
    5.         float lastHeight = charController.height;
    6.         charController.height = Mathf.Lerp (charController.height, h, Time.deltaTime*7);
    7.         pos.x = Player.position.x;
    8.         pos.y = Player.position.y + (charController.height - lastHeight) / 2;
    9.         pos.z = Player.position.z;
    10.         Player.position = pos;
    11.         Vector3 speed = new Vector3( sidespeed, 0, forwardspeed );
    12.         speed = transform.rotation * speed;
    13.         speed.y = verticalVelocity;
    14.        
    15.         charController.Move ( speed * Time.deltaTime);
     
  20. Dsavage13

    Dsavage13

    Joined:
    Nov 26, 2015
    Posts:
    76
    ive refined it and ive got it working perfectly! i disabled the mouse movement while leaning because it causes issues, and i still need to figure out crouching. im using charHeight for it, but it dosent crouch now. any ideas?