Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

character slows down when looking down or up

Discussion in 'Scripting' started by Mister-D, Mar 29, 2015.

  1. Mister-D

    Mister-D

    Joined:
    Dec 6, 2011
    Posts:
    1,694
    my character slows down when looking up or down with a orbit camera script, this is because the forward vector is too small when looking down or up i think, is there some way to bypass this?
    heres my script :


    Code (JavaScript):
    1. #pragma strict
    2.  
    3.  
    4.  
    5. var canJump = true;
    6. var jumpHeight = 2.0;
    7. var jumpinterval : float = 1.5;
    8. private var nextjump : float = 1.2;
    9. var target : Transform;
    10. var offset : float;
    11. var speed : float = 4;
    12. var runspeed : float = 8;
    13. private var canaim : boolean = true;
    14. var maxVelocityChange = 10.0;
    15. private var moveAmount : float;
    16. var smoothSpeed : float = 2;
    17.  
    18. public var gravity : float = 25;
    19. public var rotateSpeed : float = 8.0;
    20. public var dampTime : float = 3;
    21. public var mask : LayerMask;
    22. public var downcastrange : float =1.2;
    23. private var horizontalSpeed : float;
    24. var climbladder : boolean;
    25. var climbspeed : float =2;
    26. var grounded : boolean;
    27. var myaudiosource : AudioSource;
    28. var ladderposition : Vector3;
    29.  
    30. var velocityChange : Vector3;
    31. var ladderRotation : Quaternion;
    32. var climbDirection : Vector3;
    33. var running : boolean = false;
    34.  
    35. private var originalSpeed : float;
    36.  
    37. var isaiming : boolean = false;
    38.  
    39. private var forward : Vector3 = Vector3.forward;
    40. private var moveDirection : Vector3 = Vector3.zero;
    41. private var right : Vector3;
    42. var canrun : boolean = true;
    43. private var canjump : boolean = false;
    44. private var isjumping : boolean = false;
    45.  
    46. private var usegravity : boolean = true;
    47. private var movespeed : float;
    48. private var verticalspeed : float;
    49.  
    50. function Awake()
    51. {
    52.      GetComponent.<Rigidbody>().freezeRotation = true;
    53.     GetComponent.<Rigidbody>().useGravity = false;
    54. }
    55. function Start()
    56. {
    57.      originalSpeed = speed;
    58.    
    59. }
    60. function FixedUpdate()
    61. {
    62.      
    63.      
    64.       forward = GetComponent.<Camera>().main.transform.forward;
    65.    
    66.      right = new Vector3(forward.z, 0, -forward.x);
    67.      var hor = Input.GetAxisRaw("Horizontal");
    68.      var ver = Input.GetAxisRaw("Vertical");
    69.    
    70.      var targetDirection : Vector3 = (hor * right) + (ver * forward);
    71.    
    72.    
    73.    
    74.      var velocity = GetComponent.<Rigidbody>().velocity;
    75.      var horizontalvelocity = new Vector3(velocity.x,0,velocity.z);
    76.      var horizontalspeed = horizontalvelocity.magnitude;
    77.      var animator = GetComponent(Animator);
    78.      //animator.SetBool("grounded",grounded);
    79.      animator.SetFloat("speed",horizontalspeed,dampTime,0.2);
    80.    
    81.     //animator.SetBool("aim",false);
    82.     if (targetDirection != Vector3.zero && !climbladder)
    83.     {
    84.         var lookrotation = Quaternion.LookRotation(targetDirection,Vector3.up);
    85.         lookrotation.x = 0;
    86.           lookrotation.z = 0;
    87.         transform.rotation = Quaternion.Lerp(transform.rotation,lookrotation,Time.deltaTime * rotateSpeed);
    88.        
    89.     }
    90.    
    91.     animator.SetFloat("speed",horizontalspeed, dampTime,0.5);
    92.    
    93.    
    94.      if (climbladder )
    95.      {
    96.            
    97.               if (!grounded)
    98.               {
    99.                  climbDirection = forward * ver;
    100.                  climbDirection = transform.TransformDirection(climbDirection);
    101.            
    102.                  climbDirection.z = Mathf.Clamp01(climbDirection.z);
    103.                  verticalspeed = 0;
    104.            
    105.                  climbDirection *= climbspeed;
    106.                  transform.rotation = Quaternion.Slerp(transform.rotation, ladderRotation, 0.1f);
    107.                          
    108.                  usegravity = false;
    109.                  if (Input.GetButton("Fire3"))
    110.                  {
    111.                      var pushoffdirection : Vector3 = new Vector3(0,-0.2,-2);
    112.                      pushoffdirection = transform.TransformDirection(pushoffdirection);
    113.                      moveDirection = pushoffdirection;
    114.                      usegravity = true;
    115.                  }
    116.              }
    117.            
    118.              else
    119.              {
    120.                  usegravity = true;
    121.                
    122.              }
    123.        
    124.      }
    125.    
    126.      else
    127.      {
    128.          usegravity = true;
    129.        
    130.        
    131.      }
    132.      var targetVelocity = targetDirection;
    133.        
    134.      targetVelocity *= speed;
    135.    
    136.    
    137.      if(grounded)
    138.      {
    139.        
    140.         // Jump
    141.         if (Input.GetButton("Jump") && Time.time > nextjump)
    142.         {
    143.             nextjump = Time.time + jumpinterval;
    144.             GetComponent.<Rigidbody>().velocity = Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
    145.             //animator.SetBool("jump",true);
    146.         }
    147.         else
    148.          {
    149.              //animator.SetBool("jump",false);
    150.            
    151.          }  
    152.     }
    153.    
    154.      
    155.        
    156.    
    157.    
    158.    
    159.     var velocityChange = (targetVelocity - velocity);
    160.     velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
    161.     velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
    162.     velocityChange.y = 0;
    163.     GetComponent.<Rigidbody>().AddForce(velocityChange, ForceMode.VelocityChange);
    164.    
    165.     testground();
    166.     if (usegravity)
    167.     {
    168.         GetComponent.<Rigidbody>().AddForce(Vector3 (0, -gravity * GetComponent.<Rigidbody>().mass, 0));
    169.      }
    170.    
    171.    
    172. }
    173. function testground()
    174. {
    175.            
    176.       yield WaitForSeconds(0.3f);
    177.       var hit : RaycastHit;
    178.      
    179.       if(Physics.Raycast(transform.position + Vector3.up, Vector3.down, hit, downcastrange , mask ))
    180.       {
    181.            grounded = true;
    182.       }    
    183.       else
    184.       {
    185.            grounded = false;
    186.       }
    187.          
    188.          
    189. }
    190.  
    191. function CalculateJumpVerticalSpeed ()
    192. {
    193.     // From the jump height and gravity we deduce the upwards speed
    194.     // for the character to reach at the apex.
    195.     return Mathf.Sqrt(2 * jumpHeight * gravity);
    196. }
    197.  
     
  2. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    Yes, instead of taking the camera's forward Vector, use a reference Transform, which will take the camera's y euler axis as a reference, but not the x. So when camera looks down or up, it won't be affected by that, and it's forward vector will be just like camera is looking forward.
    Code (CSharp):
    1. private Transform t_Reference;
    2. public Transform t_Camera;
    3. void Awake()
    4. {
    5. t_Reference = new GameObject().transform; // create a new game object to use as a reference.
    6. }
    7.  
    8. void Update()
    9. {
    10. t_Reference.eulerAngles = new Vector3(0, t_Camera.eulerAngles.y, 0);
    11.  
    12. // from now on, your t_Reference.forward vector is your camera's forward vector, except it is not affected by camera's behaviour of looking up or down.
    13. }
     
    ranjansikand and born2lose like this.
  3. Mister-D

    Mister-D

    Joined:
    Dec 6, 2011
    Posts:
    1,694
    works perfectly, tnx for taking the time :)
     
    lineupthesky likes this.