Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Flying problems + Can't walk backwards

Discussion in 'Scripting' started by sgemmen9, Oct 29, 2015.

  1. sgemmen9

    sgemmen9

    Joined:
    Jul 24, 2015
    Posts:
    17
    Hi, so two problems. I cannot walk backwards and when I try and fly, I fly on my belly. Suggestions? This is the code I use from a free unity asset:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerControl : MonoBehaviour
    6. {
    7.  
    8.     public float walkSpeed = 0.15f;
    9.     public float runSpeed = 1.0f;
    10.     public float sprintSpeed = 2.0f;
    11.     public float flySpeed = 4.0f;
    12.  
    13.     public float turnSmoothing = 3.0f;
    14.     public float aimTurnSmoothing = 15.0f;
    15.     public float speedDampTime = 0.1f;
    16.  
    17.     public float jumpHeight = 5.0f;
    18.     public float jumpCooldown = 1.0f;
    19.  
    20.     private float timeToNextJump = 0;
    21.  
    22.     private float speed;
    23.  
    24.     private Vector3 lastDirection;
    25.  
    26.     private Animator anim;
    27.     private int speedFloat;
    28.     private int jumpBool;
    29.     private int hFloat;
    30.     private int vFloat;
    31.     private int aimBool;
    32.     private int flyBool;
    33.     private int groundedBool;
    34.     private Transform cameraTransform;
    35.  
    36.     private float h;
    37.     private float v;
    38.  
    39.     private bool aim;
    40.  
    41.     private bool run;
    42.     private bool sprint;
    43.  
    44.     private bool isMoving;
    45.  
    46.     // fly
    47.     private bool fly = false;
    48.     private float distToGround;
    49.     private float sprintFactor;
    50.  
    51.     void Awake()
    52.     {
    53.         anim = GetComponent<Animator> ();
    54.         cameraTransform = Camera.main.transform;
    55.  
    56.         speedFloat = Animator.StringToHash("Speed");
    57.         jumpBool = Animator.StringToHash("Jump");
    58.         hFloat = Animator.StringToHash("H");
    59.         vFloat = Animator.StringToHash("V");
    60.         aimBool = Animator.StringToHash("Aim");
    61.         // fly
    62.         flyBool = Animator.StringToHash ("Fly");
    63.         groundedBool = Animator.StringToHash("Grounded");
    64.         distToGround = GetComponent<Collider>().bounds.extents.y;
    65.         sprintFactor = sprintSpeed / runSpeed;
    66.     }
    67.  
    68.     bool IsGrounded() {
    69.         return Physics.Raycast(transform.position, -Vector3.up, distToGround + 0.1f);
    70.     }
    71.  
    72.     void Update()
    73.     {
    74.         // fly
    75.         if(Input.GetButtonDown ("Fly"))
    76.             fly = !fly;
    77.         aim = Input.GetButton("Aim");
    78.         h = Input.GetAxis("Horizontal");
    79.         v = Input.GetAxis("Vertical");
    80.         run = Input.GetButton ("Run");
    81.         sprint = Input.GetButton ("Sprint");
    82.         isMoving = Mathf.Abs(h) > 0.1 || Mathf.Abs(v) > 0.1;
    83.     }
    84.  
    85.     void FixedUpdate()
    86.     {
    87.         anim.SetBool (aimBool, IsAiming());
    88.         anim.SetFloat(hFloat, h);
    89.         anim.SetFloat(vFloat, v);
    90.      
    91.         // Fly
    92.         anim.SetBool (flyBool, fly);
    93.         GetComponent<Rigidbody>().useGravity = !fly;
    94.         anim.SetBool (groundedBool, IsGrounded ());
    95.         if(fly)
    96.             FlyManagement(h,v);
    97.  
    98.         else
    99.         {
    100.             MovementManagement (h, v, run, sprint);
    101.             JumpManagement ();
    102.         }
    103.     }
    104.  
    105.     // fly
    106.     void FlyManagement(float horizontal, float vertical)
    107.     {
    108.         Vector3 direction = Rotating(horizontal, vertical);
    109.         GetComponent<Rigidbody>().AddForce(direction * flySpeed * 100 * (sprint?sprintFactor:1));
    110.     }
    111.  
    112.     void JumpManagement()
    113.     {
    114.         if (GetComponent<Rigidbody>().velocity.y < 10) // already jumped
    115.         {
    116.             anim.SetBool (jumpBool, false);
    117.             if(timeToNextJump > 0)
    118.                 timeToNextJump -= Time.deltaTime;
    119.         }
    120.         if (Input.GetButtonDown ("Jump"))
    121.         {
    122.             anim.SetBool(jumpBool, true);
    123.             if(speed > 0 && timeToNextJump <= 0 && !aim)
    124.             {
    125.                 GetComponent<Rigidbody>().velocity = new Vector3(0, jumpHeight, 0);
    126.                 timeToNextJump = jumpCooldown;
    127.             }
    128.         }
    129.     }
    130.  
    131.     void MovementManagement(float horizontal, float vertical, bool running, bool sprinting)
    132.     {
    133.         Rotating(horizontal, vertical);
    134.  
    135.         if(isMoving)
    136.         {
    137.             if(sprinting)
    138.             {
    139.                 speed = sprintSpeed;
    140.             }
    141.             else if (running)
    142.             {
    143.                 speed = runSpeed;
    144.             }
    145.             else
    146.             {
    147.                 speed = walkSpeed;
    148.             }
    149.  
    150.             anim.SetFloat(speedFloat, speed, speedDampTime, Time.deltaTime);
    151.         }
    152.         else
    153.         {
    154.             speed = 0f;
    155.             anim.SetFloat(speedFloat, 0f);
    156.         }
    157.         GetComponent<Rigidbody>().AddForce(Vector3.forward*speed);
    158.     }
    159.  
    160.     Vector3 Rotating(float horizontal, float vertical)
    161.     {
    162.         Vector3 forward = cameraTransform.TransformDirection(Vector3.forward);
    163.         if (!fly)
    164.             forward.y = 0.0f;
    165.         forward = forward.normalized;
    166.  
    167.         Vector3 right = new Vector3(forward.z, 0, -forward.x);
    168.  
    169.         Vector3 targetDirection;
    170.  
    171.         float finalTurnSmoothing;
    172.  
    173.         if(IsAiming())
    174.         {
    175.             targetDirection = forward;
    176.             finalTurnSmoothing = aimTurnSmoothing;
    177.         }
    178.         else
    179.         {
    180.             targetDirection = forward * vertical + right * horizontal;
    181.             finalTurnSmoothing = turnSmoothing;
    182.         }
    183.  
    184.         if((isMoving && targetDirection != Vector3.zero) || IsAiming())
    185.         {
    186.             Quaternion targetRotation = Quaternion.LookRotation (targetDirection, Vector3.up);
    187.             // fly
    188.             if (fly)
    189.                 targetRotation *= Quaternion.Euler (90, 0, 0);
    190.  
    191.             Quaternion newRotation = Quaternion.Slerp(GetComponent<Rigidbody>().rotation, targetRotation, finalTurnSmoothing * Time.deltaTime);
    192.             GetComponent<Rigidbody>().MoveRotation (newRotation);
    193.             lastDirection = targetDirection;
    194.         }
    195.         //idle - fly or grounded
    196.         if(!(Mathf.Abs(h) > 0.9 || Mathf.Abs(v) > 0.9))
    197.         {
    198.             Repositioning();
    199.         }
    200.  
    201.         return targetDirection;
    202.     }  
    203.  
    204.     private void Repositioning()
    205.     {
    206.         Vector3 repositioning = lastDirection;
    207.         if(repositioning != Vector3.zero)
    208.         {
    209.             repositioning.y = 0;
    210.             Quaternion targetRotation = Quaternion.LookRotation (repositioning, Vector3.up);
    211.             Quaternion newRotation = Quaternion.Slerp(GetComponent<Rigidbody>().rotation, targetRotation, turnSmoothing * Time.deltaTime);
    212.             GetComponent<Rigidbody>().MoveRotation (newRotation);
    213.         }
    214.     }
    215.  
    216.     public bool IsFlying()
    217.     {
    218.         return fly;
    219.     }
    220.  
    221.     public bool IsAiming()
    222.     {
    223.         return aim && !fly;
    224.     }
    225.  
    226.     public bool isSprinting()
    227.     {
    228.         return sprint && !aim && (isMoving);
    229.     }
    230. }
    231.  
     
    Last edited: Oct 31, 2015
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Put your codes into code tags, it helps other people understand, what your code does
     
  3. sgemmen9

    sgemmen9

    Joined:
    Jul 24, 2015
    Posts:
    17
    I would like to but this is a free asset I downloaded and I dont understand it much either lol.
     
  4. RiokuTheSlayer

    RiokuTheSlayer

    Joined:
    Aug 22, 2013
    Posts:
    356
    That has nothing to do with it.

    Read this, and update your post, please.
     
  5. sgemmen9

    sgemmen9

    Joined:
    Jul 24, 2015
    Posts:
    17
    Thanks for the link! Hopefully this helps.