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

Jump script

Discussion in 'Scripting' started by Tredway, Feb 6, 2016.

  1. Tredway

    Tredway

    Joined:
    Feb 6, 2016
    Posts:
    2
    Hi guys I'm new to Unity so i need your help.
    I want to add jump and footsteps, jump sound script from FPSController which is included to Unity in Characters asset to my script below

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(Animator))]
    4. [RequireComponent(typeof(ConfigurableJoint))]
    5. [RequireComponent(typeof(PlayerMotor))]
    6. public class PlayerController : MonoBehaviour {
    7.  
    8.     [SerializeField]
    9.     private float speed = 5f;
    10.     [SerializeField]
    11.     private float lookSensitivity = 3f;
    12.  
    13.  
    14.     private PlayerMotor motor;
    15.     private ConfigurableJoint joint;
    16.     private Animator animator;
    17.  
    18.  
    19.  
    20.  
    21.     void Start ()
    22.     {
    23.         motor = GetComponent<PlayerMotor>();
    24.         joint = GetComponent<ConfigurableJoint>();
    25.         animator = GetComponent<Animator>();
    26.  
    27.    
    28.     }
    29.  
    30.  
    31.     void Update ()
    32.     {
    33.         float _xMov = Input.GetAxis("Horizontal");
    34.         float _zMov = Input.GetAxis("Vertical");
    35.  
    36.         Vector3 _movHorizontal = transform.right * _xMov;
    37.         Vector3 _movVertical = transform.forward * _zMov;
    38.  
    39.         Vector3 _velocity = (_movHorizontal + _movVertical) * speed;
    40.  
    41.         animator.SetFloat("ForwardVelocity", _zMov);
    42.  
    43.         motor.Move(_velocity);
    44.  
    45.         float _yRot = Input.GetAxisRaw("Mouse X");
    46.  
    47.         Vector3 _rotation = new Vector3(0f, _yRot, 0f) * lookSensitivity;
    48.  
    49.         motor.Rotate(_rotation);
    50.  
    51.         float _xRot = Input.GetAxisRaw("Mouse Y");
    52.  
    53.         float _cameraRotationX = _xRot * lookSensitivity;
    54.  
    55.         motor.RotateCamera(_cameraRotationX);
    56.  
    57.     }
    58.      
    59. }
    60.  
    All of my "work" is based on tutorials so I need some time to understand it on my own. I hope you can help me with this and I will be much thankful if you can explane how to do it :)

    (Anyway sorry for my bad English)
     
  2. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    For sounds to play you need AudioSource Component and AudioClips (sound files) you will be playing. First add them as public variables and set them in inspector.
    Then when you would need to set AudioSource.clip to AudioClip that should be playing and call .Play() (be sure to check if it isn't isPlaying as you most likely don't want to replay footsteps clip until it has ended).
     
  3. Tredway

    Tredway

    Joined:
    Feb 6, 2016
    Posts:
    2
    I'm seriously newbie in unity i just try copy and paste from one script to another there's no errors but still not working


    Code (CSharp):
    1.  
    2. using System;
    3. using UnityEngine;
    4. using UnityStandardAssets.CrossPlatformInput;
    5. using UnityStandardAssets.Utility;
    6. using Random = UnityEngine.Random;
    7.  
    8. namespace UnityStandardAssets.Characters.FirstPerson
    9. {
    10. [RequireComponent(typeof(Animator))]
    11. [RequireComponent(typeof(ConfigurableJoint))]
    12. [RequireComponent(typeof(PlayerMotor))]
    13. [RequireComponent(typeof (CharacterController))]
    14. [RequireComponent(typeof (AudioSource))]
    15. public class PlayerController : MonoBehaviour {
    16.  
    17.     [SerializeField]
    18.     private float speed = 5f;
    19.     [SerializeField]
    20.     private float lookSensitivity = 3f;
    21.  
    22.  
    23.     [SerializeField] private bool m_IsWalking;
    24.     [SerializeField] private float m_WalkSpeed;
    25.     [SerializeField] private float m_RunSpeed;
    26.     [SerializeField] [Range(0f, 1f)] private float m_RunstepLenghten;
    27.     [SerializeField] private float m_JumpSpeed;
    28.     [SerializeField] private float m_StickToGroundForce;
    29.     [SerializeField] private float m_GravityMultiplier;
    30.     [SerializeField] private MouseLook m_MouseLook;
    31.     [SerializeField] private bool m_UseFovKick;
    32.     [SerializeField] private FOVKick m_FovKick = new FOVKick();
    33.     [SerializeField] private bool m_UseHeadBob;
    34.     [SerializeField] private CurveControlledBob m_HeadBob = new CurveControlledBob();
    35.     [SerializeField] private LerpControlledBob m_JumpBob = new LerpControlledBob();
    36.     [SerializeField] private float m_StepInterval;
    37.     [SerializeField] private AudioClip[] m_FootstepSounds;    // an array of footstep sounds that will be randomly selected from.
    38.     [SerializeField] private AudioClip m_JumpSound;           // the sound played when character leaves the ground.
    39.     [SerializeField] private AudioClip m_LandSound;           // the sound played when character touches back on ground.
    40.  
    41.  
    42.  
    43.     // Component caching
    44.     private PlayerMotor motor;
    45.     private ConfigurableJoint joint;
    46.     private Animator animator;
    47.  
    48.  
    49.     private Camera m_Camera;
    50.     private bool m_Jump;
    51.     private float m_YRotation;
    52.     private Vector2 m_Input;
    53.     private Vector3 m_MoveDir = Vector3.zero;
    54.     private CharacterController m_CharacterController;
    55.     private CollisionFlags m_CollisionFlags;
    56.     private bool m_PreviouslyGrounded;
    57.     private Vector3 m_OriginalCameraPosition;
    58.     private float m_StepCycle;
    59.     private float m_NextStep;
    60.     private bool m_Jumping;
    61.     private AudioSource m_AudioSource;
    62.  
    63.  
    64.  
    65.     void Start ()
    66.     {
    67.         motor = GetComponent<PlayerMotor>();
    68.         joint = GetComponent<ConfigurableJoint>();
    69.         animator = GetComponent<Animator>();
    70.  
    71.         m_CharacterController = GetComponent<CharacterController>();
    72.         m_Camera = Camera.main;
    73.         m_OriginalCameraPosition = m_Camera.transform.localPosition;
    74.         m_FovKick.Setup(m_Camera);
    75.         m_HeadBob.Setup(m_Camera, m_StepInterval);
    76.         m_StepCycle = 0f;
    77.         m_NextStep = m_StepCycle/2f;
    78.         m_Jumping = false;
    79.         m_AudioSource = GetComponent<AudioSource>();
    80.         m_MouseLook.Init(transform , m_Camera.transform);
    81.    
    82.     }
    83.  
    84.  
    85.     void Update ()
    86.     {
    87.         //Calculate movement velocity as a 3D vector
    88.         float _xMov = Input.GetAxis("Horizontal");
    89.         float _zMov = Input.GetAxis("Vertical");
    90.  
    91.         Vector3 _movHorizontal = transform.right * _xMov;
    92.         Vector3 _movVertical = transform.forward * _zMov;
    93.  
    94.         // Final movement vector
    95.         Vector3 _velocity = (_movHorizontal + _movVertical) * speed;
    96.  
    97.         // Animate movement
    98.         animator.SetFloat("ForwardVelocity", _zMov);
    99.  
    100.         //Apply movement
    101.         motor.Move(_velocity);
    102.         PlayFootStepAudio();
    103.  
    104.         //Calculate rotation as a 3D vector (turning around)
    105.         float _yRot = Input.GetAxisRaw("Mouse X");
    106.  
    107.         Vector3 _rotation = new Vector3(0f, _yRot, 0f) * lookSensitivity;
    108.  
    109.         //Apply rotation
    110.         motor.Rotate(_rotation);
    111.  
    112.         //Calculate camera rotation as a 3D vector (turning around)
    113.         float _xRot = Input.GetAxisRaw("Mouse Y");
    114.  
    115.         float _cameraRotationX = _xRot * lookSensitivity;
    116.  
    117.         //Apply camera rotation
    118.         motor.RotateCamera(_cameraRotationX);
    119.  
    120.  
    121.         // the jump state needs to read here to make sure it is not missed
    122.         if (!m_Jump)
    123.         {
    124.             m_Jump = CrossPlatformInputManager.GetButtonDown("Jump");
    125.  
    126.         }
    127.  
    128.         if (!m_PreviouslyGrounded && m_CharacterController.isGrounded)
    129.         {
    130.             StartCoroutine(m_JumpBob.DoBobCycle());
    131.             PlayLandingSound();
    132.             m_MoveDir.y = 0f;
    133.             m_Jumping = false;
    134.         }
    135.         if (!m_CharacterController.isGrounded && !m_Jumping && m_PreviouslyGrounded)
    136.         {
    137.             m_MoveDir.y = 0f;
    138.         }
    139.  
    140.         m_PreviouslyGrounded = m_CharacterController.isGrounded;
    141.     }
    142.  
    143.  
    144.     private void PlayJumpSound()
    145.     {
    146.         m_AudioSource.clip = m_JumpSound;
    147.         m_AudioSource.Play();
    148.     }
    149.  
    150.     private void PlayLandingSound()
    151.     {
    152.         m_AudioSource.clip = m_LandSound;
    153.         m_AudioSource.Play();
    154.         m_NextStep = m_StepCycle + .5f;
    155.     }
    156.     private void PlayFootStepAudio()
    157.     {
    158.         if (!m_CharacterController.isGrounded)
    159.         {
    160.             return;
    161.         }
    162.         // pick & play a random footstep sound from the array,
    163.         // excluding sound at index 0
    164.         int n = Random.Range(1, m_FootstepSounds.Length);
    165.         m_AudioSource.clip = m_FootstepSounds[n];
    166.         m_AudioSource.PlayOneShot(m_AudioSource.clip);
    167.         // move picked sound to index 0 so it's not picked next time
    168.         m_FootstepSounds[n] = m_FootstepSounds[0];
    169.         m_FootstepSounds[0] = m_AudioSource.clip;
    170.     }
    171.  
    172.     void FixedUpdate()
    173.     {
    174.  
    175.  
    176.         if (m_CharacterController.isGrounded)
    177.         {
    178.             m_MoveDir.y = -m_StickToGroundForce;
    179.  
    180.             if (m_Jump)
    181.             {
    182.                 m_MoveDir.y = m_JumpSpeed;
    183.                 PlayJumpSound();
    184.                 m_Jump = false;
    185.                 m_Jumping = true;
    186.             }
    187.         }
    188.         else
    189.         {
    190.             m_MoveDir += Physics.gravity*m_GravityMultiplier*Time.fixedDeltaTime;
    191.         }  
    192.  
    193.     }
    194.        
    195. }
    196.  
    197. }
    198.  
     
  4. Teravisor

    Teravisor

    Joined:
    Dec 29, 2014
    Posts:
    654
    1. Are m_JumpSound, m_LandSound, m_FootstepSounds set to anything? Because I don't see any place they are set in or any way for them to be set.
    2. In PlayFootStepAudio() you don't check if some footstep clip is already playing. So it would cause to restart it over and over before it has chance to play.