Exemplo n.º 1
0
    void Update()
    {
        timer += Time.deltaTime;

        if (useTerrain == true)
        {
            hitSurfaceName = TerrainSurface.GetMainTexture(transform.position);
        }
        speed   = Vector3.Distance(lastPos, this.transform.position) / Time.deltaTime;
        lastPos = this.transform.position;

        if (grounded && speed > 0.2f && timer > interval)
        {
            if (useTerrain == true)
            {
                foreach (FootStepMaterial material in materialList)
                {
                    if (material.materialName == hitSurfaceName)
                    {
                        sounds = material.soundsToPlay;
                    }
                }
            }
            else
            {
                foreach (FootStepMaterial material in materialList)
                {
                    if (material.materialName == standingMaterial || material.materialName + " (Instance)" == standingMaterial)
                    {
                        sounds = material.soundsToPlay;
                    }
                }
            }
        }
    }
Exemplo n.º 2
0
 AudioClip GetClip(TFPInfo info)
 {
     if (Physics.SphereCast(transform.position + (Vector3.up * info.controller.radius), info.controller.radius, Vector3.down, out groundTypeCheck, info.crouchHeadHitLayerMask.value))
     {
         TerrainCollider hitTerrain = groundTypeCheck.transform.GetComponent <TerrainCollider>();
         MeshRenderer    hitMesh    = groundTypeCheck.transform.GetComponent <MeshRenderer>();
         Texture2D       hitTexture;
         if (hitTerrain != null)
         {
             hitTexture = TerrainSurface.GetMainTexture(groundTypeCheck.transform.GetComponent <Terrain>(), transform.position);
         }
         else if (hitMesh != null)
         {
             hitTexture = hitMesh.material.mainTexture as Texture2D;
         }
         else
         {
             return(defaultFootsteps.footSounds[Random.Range(0, defaultFootsteps.footSounds.Length)]);
         }
         foreach (FootstepGroup fsGroup in footsteps)
         {
             foreach (Texture2D tex in fsGroup.textures)
             {
                 if (hitTexture == tex)
                 {
                     return(fsGroup.footSounds[Random.Range(0, fsGroup.footSounds.Length)]);
                 }
             }
         }
     }
     return(defaultFootsteps.footSounds[Random.Range(0, defaultFootsteps.footSounds.Length)]);
 }
Exemplo n.º 3
0
    void PlayTerrainSound(Terrain t, Vector3 hitPoint)
    {
        if (audioS == null)
        {
            Debug.LogError("PlayTerrainSound -- We have no audio source to play the sound from.");
            return;
        }

        if (sc == null)
        {
            Debug.LogError("PlayTerrainSound -- No sound manager!!!");
            return;
        }

        if (textureTypes.Length > 0)
        {
            int textureIndex = TerrainSurface.GetMainTexture(hitPoint);

            foreach (TextureType type in textureTypes)
            {
                if (type.footstepSounds.Length == 0)
                {
                    return;
                }

                foreach (Texture tex in type.textures)
                {
                    if (t.terrainData.splatPrototypes[textureIndex].texture == tex)
                    {
                        sc.PlaySound(audioS, type.footstepSounds [Random.Range(0, type.footstepSounds.Length)], true, 1, 1.2f);
                    }
                }
            }
        }
    }
Exemplo n.º 4
0
 //Check if hit object is found within the hit dictionary
 //if it is call "hitObject"
 public void RunHitDictionaryCheck(GameObject hitObj, RaycastHit hit)
 {
     //hit a terrain
     if (hitObj.tag == "Terrain")
     {
         string hitSurfaceName = TerrainSurface.GetMainTexture(transform.position);
         foreach (HitVisuals hitDict in hitDictionary)
         {
             if (hitSurfaceName == hitDict.materialName || hitSurfaceName == hitDict.materialName + " (Instance)")
             {
                 hitObject(hitDict, hit);
                 return;
             }
         }
     }
     //hit something other than a terrain
     else if (hitObj.GetComponent <MeshRenderer> ())
     {
         string hitSurfaceName = hitObj.GetComponent <MeshRenderer> ().material.name;
         foreach (HitVisuals hitDict in hitDictionary)
         {
             if (hitSurfaceName == hitDict.materialName || hitSurfaceName == hitDict.materialName + " (Instance)")
             {
                 hitObject(hitDict, hit);
                 return;
             }
         }
     }
 }
    private void PlayFootstepSoundWhenOnTerrain(RaycastHit hit)
    {
        if (!_controller.isGrounded)
        {
            return;              // if the player is not grounded, then do nothing
        }
        if (!hit.collider.gameObject.GetComponent <Terrain>())
        {
            return;
        }

        bool existance = false;         //Used to check the existance of the surface
        int  i         = TerrainSurface.GetMainTexture(transform.position);

        for (int j = 0; j < surfacesOnTerrain.Length; ++j)
        {
            if (j == i)
            {
                existance = true;                       // if index i of terrain main texture also exists in the terrain surfaces array
                break;
            }
        }
        if (existance)
        {
            if (surfacesOnTerrain [i].surfaceFootStepSounds != null)
            {
                _currentSurface = surfacesOnTerrain [i];
                n2 = Random.Range(0, surfacesOnTerrain [i].surfaceFootStepSounds.Length - 1);
                _audioSource.clip = surfacesOnTerrain [i].surfaceFootStepSounds [n2];
            }
            else
            {
                Debug.LogError("No Footsteps Sound Set For " + surfacesOnTerrain [i].name);
            }
        }
        else
        {
            if (playDefaultFootstepsSoundForUndefinedSurfaces)
            {
                if (defaultFootstepSounds != null)
                {
                    n3 = Random.Range(0, defaultFootstepSounds.Length - 1);
                    _audioSource.clip = defaultFootstepSounds [n3];
                    _currentSurface   = null;
                }
                else
                {
                    Debug.LogError("No Default Landing Sound Set!");
                }
            }
        }

        _audioSource.PlayOneShot(_audioSource.clip, audioVolume);
    }
Exemplo n.º 6
0
    IEnumerator PlayFootstep()
    {
        isFootstepPlaying = true;

        int textureIdx = TerrainSurface.GetMainTexture(transform.position);

        audio.clip = footstepSounds[textureIdx];
        audio.Play();

        yield return(new WaitForSeconds(.6f));

        isFootstepPlaying = false;
    }
    private void SetLandingSoundOnTerrain(RaycastHit hit)
    {
        if (!_controller.isGrounded)
        {
            return;              // if the player is not grounded, then do nothing
        }
        if (!hit.collider.gameObject.GetComponent <Terrain>())
        {
            return;
        }
        bool existance = false;         //Used to check the existance of the surface
        int  i         = TerrainSurface.GetMainTexture(transform.position);

        for (int j = 0; j < surfacesOnTerrain.Length; ++j)
        {
            if (j == i)
            {
                existance = true;                       // if index i of terrain main texture also exists in the terrain surfaces array
                break;
            }
        }
        if (existance)
        {
            if (surfacesOnTerrain [i].landingSound != null)
            {
                _controller.LandingSound = surfacesOnTerrain [i].landingSound;
            }
            else
            {
                Debug.LogError("No Landing Sound Set For " + surfacesOnTerrain [i].name);
            }
        }
        else
        {
            if (playDefaultFootstepsSoundForUndefinedSurfaces)
            {
                if (defaultLandingSound != null)
                {
                    _controller.LandingSound = defaultLandingSound;
                }
                else
                {
                    Debug.LogError("No Default Landing Sound Set!");
                }
            }
        }
    }
    public override void OnInspectorGUI()
    {
        GroundSurfaceMaster surfaceMaster = FindObjectOfType <GroundSurfaceMaster>();

        targetScript = (TerrainSurface)target;
        Undo.RecordObject(targetScript, "Terrain Surface Change");

        if (targetScript.GetComponent <Terrain>().terrainData)
        {
            terDat = targetScript.GetComponent <Terrain>().terrainData;
        }

        EditorGUILayout.LabelField("Textures and Surface Types:", EditorStyles.boldLabel);

        surfaceNames = new string[surfaceMaster.surfaceTypes.Length];

        for (int i = 0; i < surfaceNames.Length; i++)
        {
            surfaceNames[i] = surfaceMaster.surfaceTypes[i].name;
        }

        if (targetScript.surfaceTypes.Length > 0)
        {
            for (int j = 0; j < targetScript.surfaceTypes.Length; j++)
            {
                DrawTerrainInfo(terDat, j);
            }
        }
        else
        {
            EditorGUI.indentLevel++;
            EditorGUILayout.LabelField("<No terrain textures found>");
        }

        if (GUI.changed)
        {
            EditorUtility.SetDirty(targetScript);
        }
    }
Exemplo n.º 9
0
    private WheelHit GetGroundInfos(ref WheelCollider wheelCol,
                                    ref string groundTag,
                                    ref int groundTextureIndex)
    {
        // Default values
        groundTag          = "InTheAir";
        groundTextureIndex = -1;
        // Query ground by ray shoot on the front left wheel collider
        WheelHit wheelHit;

        wheelCol.GetGroundHit(out wheelHit);
        // If not in the air query collider
        if (wheelHit.collider)
        {
            groundTag = wheelHit.collider.tag;
            if (wheelHit.collider.CompareTag("Terrain"))
            {
                groundTextureIndex = TerrainSurface.GetMainTexture(transform.position);
            }
        }
        return(wheelHit);
    }
    //Get all possible positions that a player can spawn
    void GetPossiblePositions(Terrain terrain)
    {
        terrainLength = (int)terrain.terrainData.size.x;
        terrainWidth  = (int)terrain.terrainData.size.z;
        terrainPosX   = (int)terrain.transform.position.x;
        terrainPosZ   = (int)terrain.transform.position.z;

        for (int x = 0; x < terrainLength; x++)
        {
            for (int z = 0; z < terrainWidth; z++)
            {
                Vector3 checkPos = new Vector3(terrainPosX + x, 0, terrainPosZ + z);
                int     textureIndexAtCheckPos = TerrainSurface.GetMainTexture(checkPos, terrain);

                if (textureIndexAtCheckPos == terrainTextureToSpawnPlayer)
                {
                    possibleSpawnLocations.Add(checkPos);
                    possibleSpawnLocationHeight.Add(new Vector3(0, terrain.SampleHeight(checkPos), 0));
                }
            }
        }
    }
Exemplo n.º 11
0
    public override void OnInspectorGUI()
    {
        GroundSurfaceMaster surfaceMaster = FindObjectOfType<GroundSurfaceMaster>();
        targetScript = (TerrainSurface)target;
        Undo.RecordObject(targetScript, "Terrain Surface Change");

        if (targetScript.GetComponent<Terrain>().terrainData)
        {
            terDat = targetScript.GetComponent<Terrain>().terrainData;
        }

        EditorGUILayout.LabelField("Textures and Surface Types:", EditorStyles.boldLabel);

        surfaceNames = new string[surfaceMaster.surfaceTypes.Length];

        for (int i = 0; i < surfaceNames.Length; i++)
        {
            surfaceNames[i] = surfaceMaster.surfaceTypes[i].name;
        }

        if (targetScript.surfaceTypes.Length > 0)
        {
            for (int j = 0; j < targetScript.surfaceTypes.Length; j++)
            {
                DrawTerrainInfo(terDat, j);
            }
        }
        else
        {
            EditorGUI.indentLevel ++;
            EditorGUILayout.LabelField("<No terrain textures found>");
        }

        if (GUI.changed)
        {
            EditorUtility.SetDirty(targetScript);
        }
    }
Exemplo n.º 12
0
    void Frictions()
    {
        WheelHit GroundHit;

        wheelCollider.GetGroundHit(out GroundHit);
        bool contacted = false;

        for (int i = 0; i < physicsFrictions.Length; i++)
        {
            if (GroundHit.point != Vector3.zero && GroundHit.collider.sharedMaterial == physicsFrictions[i].groundMaterial)
            {
                contacted = true;

                forwardFrictionCurve.stiffness  = physicsFrictions[i].forwardStiffness;
                sidewaysFrictionCurve.stiffness = (physicsFrictions[i].sidewaysStiffness * tractionHelpedSidewaysStiffness);

                if (RCC_Settings.Instance.behaviorType == RCC_Settings.BehaviorType.Drift)
                {
                    Drift(Mathf.Abs(GroundHit.forwardSlip));
                }

                wheelCollider.forwardFriction  = forwardFrictionCurve;
                wheelCollider.sidewaysFriction = sidewaysFrictionCurve;

                wheelCollider.wheelDampingRate = physicsFrictions[i].damp;

                if (!RCC_Settings.Instance.dontUseAnyParticleEffects)
                {
                    emission = allWheelParticles[i].emission;
                }

                audioClip = physicsFrictions[i].groundSound;

                if (wheelSlipAmountSideways > physicsFrictions[i].slip || wheelSlipAmountForward > physicsFrictions[i].slip)
                {
                    emission.enabled = true;
                }
                else
                {
                    emission.enabled = false;
                }
            }
        }

        if (!contacted && physicsMaterials.useTerrainSplatMapForGroundFrictions)
        {
            for (int k = 0; k < physicsMaterials.terrainSplatMapIndex.Length; k++)
            {
                if (GroundHit.point != Vector3.zero && GroundHit.collider.sharedMaterial == physicsMaterials.terrainPhysicMaterial)
                {
                    if (TerrainSurface.GetTextureMix(transform.position) != null && TerrainSurface.GetTextureMix(transform.position)[k] > .5f)
                    {
                        contacted = true;

                        forwardFrictionCurve.stiffness  = physicsFrictions[physicsMaterials.terrainSplatMapIndex[k]].forwardStiffness;
                        sidewaysFrictionCurve.stiffness = (physicsFrictions[physicsMaterials.terrainSplatMapIndex[k]].sidewaysStiffness * tractionHelpedSidewaysStiffness);

                        if (RCC_Settings.Instance.behaviorType == RCC_Settings.BehaviorType.Drift)
                        {
                            Drift(Mathf.Abs(GroundHit.forwardSlip));
                        }

                        wheelCollider.forwardFriction  = forwardFrictionCurve;
                        wheelCollider.sidewaysFriction = sidewaysFrictionCurve;

                        wheelCollider.wheelDampingRate = physicsFrictions[physicsMaterials.terrainSplatMapIndex[k]].damp;

                        if (!RCC_Settings.Instance.dontUseAnyParticleEffects)
                        {
                            emission = allWheelParticles[physicsMaterials.terrainSplatMapIndex[k]].emission;
                        }

                        audioClip = physicsFrictions[physicsMaterials.terrainSplatMapIndex[k]].groundSound;

                        if (wheelSlipAmountSideways > physicsFrictions[physicsMaterials.terrainSplatMapIndex[k]].slip || wheelSlipAmountForward > physicsFrictions[physicsMaterials.terrainSplatMapIndex[k]].slip)
                        {
                            emission.enabled = true;
                        }
                        else
                        {
                            emission.enabled = false;
                        }
                    }
                }
            }
        }

        if (!contacted)
        {
            forwardFrictionCurve.stiffness  = orgForwardStiffness;
            sidewaysFrictionCurve.stiffness = orgSidewaysStiffness * tractionHelpedSidewaysStiffness;

            if (RCC_Settings.Instance.behaviorType == RCC_Settings.BehaviorType.Drift)
            {
                Drift(Mathf.Abs(GroundHit.forwardSlip));
            }

            wheelCollider.forwardFriction  = forwardFrictionCurve;
            wheelCollider.sidewaysFriction = sidewaysFrictionCurve;

            wheelCollider.wheelDampingRate = physicsFrictions[0].damp;

            if (!RCC_Settings.Instance.dontUseAnyParticleEffects)
            {
                emission = allWheelParticles[0].emission;
            }

            audioClip = physicsFrictions[0].groundSound;

            if (wheelSlipAmountSideways > physicsFrictions[0].slip || wheelSlipAmountForward > physicsFrictions[0].slip)
            {
                emission.enabled = true;
            }
            else
            {
                emission.enabled = false;
            }
        }

        if (!RCC_Settings.Instance.dontUseAnyParticleEffects)
        {
            for (int i = 0; i < allWheelParticles.Count; i++)
            {
                if (wheelSlipAmountSideways > startSlipValue || wheelSlipAmountForward > startSlipValue)
                {
                }
                else
                {
                    emission         = allWheelParticles[i].emission;
                    emission.enabled = false;
                }
            }
        }
    }
Exemplo n.º 13
0
    // Setting ground frictions to wheel frictions.
    void Frictions()
    {
        // First, we are getting groundhit data.
        WheelHit GroundHit;

        wheelCollider.GetGroundHit(out GroundHit);

        // Contacted any physic material in Configurable Ground Materials yet?
        bool contacted = false;

        for (int i = 0; i < physicsFrictions.Length; i++)
        {
            if (GroundHit.point != Vector3.zero && GroundHit.collider.sharedMaterial == physicsFrictions[i].groundMaterial)
            {
                contacted = true;

                // Setting wheel stiffness to ground physic material stiffness.
                forwardFrictionCurve.stiffness  = physicsFrictions[i].forwardStiffness;
                sidewaysFrictionCurve.stiffness = (physicsFrictions[i].sidewaysStiffness * tractionHelpedSidewaysStiffness);

                // Setting new friction curves to wheels.
                wheelCollider.forwardFriction  = forwardFrictionCurve;
                wheelCollider.sidewaysFriction = sidewaysFrictionCurve;

                // Also damp too.
                wheelCollider.wheelDampingRate = physicsFrictions[i].damp;

                // If dontUseAnyParticleEffects bool is not selected in RCC_Settings, set emission to ground physic material smoke.
                if (!RCCSettings.dontUseAnyParticleEffects)
                {
                    emission = allWheelParticles[i].emission;
                }

                // Set audioclip to ground physic material sound.
                audioClip = physicsFrictions[i].groundSound;

                // If wheel slip is bigger than ground physic material slip, enable particles. Otherwise, disable particles.
                if (!RCCSettings.dontUseAnyParticleEffects)
                {
                    if (wheelSlipAmountSideways > physicsFrictions [i].slip || wheelSlipAmountForward > physicsFrictions [i].slip)
                    {
                        emission.enabled = true;
                    }
                    else
                    {
                        emission.enabled = false;
                    }
                }
            }
        }

        // If ground pyhsic material is not one of the ground material in Configurable Ground Materials, check if we are on terrain collider...
        if (!contacted && physicsMaterials.useTerrainSplatMapForGroundFrictions)
        {
            for (int k = 0; k < physicsMaterials.terrainSplatMapIndex.Length; k++)
            {
                // If current ground is terrain collider...
                if (GroundHit.point != Vector3.zero && GroundHit.collider.sharedMaterial == physicsMaterials.terrainPhysicMaterial)
                {
                    // Getting current exact position by splatmap of the terrain.
                    if (TerrainSurface.GetTextureMix(transform.position) != null && TerrainSurface.GetTextureMix(transform.position)[k] > .5f)
                    {
                        contacted = true;

                        // Setting wheel stiffness to ground physic material stiffness.
                        forwardFrictionCurve.stiffness  = physicsFrictions[physicsMaterials.terrainSplatMapIndex[k]].forwardStiffness;
                        sidewaysFrictionCurve.stiffness = (physicsFrictions[physicsMaterials.terrainSplatMapIndex[k]].sidewaysStiffness * tractionHelpedSidewaysStiffness);

                        // Setting new friction curves to wheels.
                        wheelCollider.forwardFriction  = forwardFrictionCurve;
                        wheelCollider.sidewaysFriction = sidewaysFrictionCurve;

                        // Also damp too.
                        wheelCollider.wheelDampingRate = physicsFrictions[physicsMaterials.terrainSplatMapIndex[k]].damp;

                        // If dontUseAnyParticleEffects bool is not selected in RCC_Settings, set emission to ground physic material smoke.
                        if (!RCCSettings.dontUseAnyParticleEffects)
                        {
                            emission = allWheelParticles[physicsMaterials.terrainSplatMapIndex[k]].emission;
                        }

                        // Set audioclip to ground physic material sound.
                        audioClip = physicsFrictions[physicsMaterials.terrainSplatMapIndex[k]].groundSound;

                        // If wheel slip is bigger than ground physic material slip, enable particles. Otherwise, disable particles.
                        if (!RCCSettings.dontUseAnyParticleEffects)
                        {
                            if (wheelSlipAmountSideways > physicsFrictions [physicsMaterials.terrainSplatMapIndex [k]].slip || wheelSlipAmountForward > physicsFrictions [physicsMaterials.terrainSplatMapIndex [k]].slip)
                            {
                                emission.enabled = true;
                            }
                            else
                            {
                                emission.enabled = false;
                            }
                        }
                    }
                }
            }
        }

        // If wheel still not contacted any of ground material in Configurable Ground Materials, set it to original default values.
        if (!contacted)
        {
            // Setting default stiffness to ground physic material stiffness.
            forwardFrictionCurve.stiffness  = orgForwardStiffness;
            sidewaysFrictionCurve.stiffness = orgSidewaysStiffness * tractionHelpedSidewaysStiffness;

            // Setting default friction curves to wheels.
            wheelCollider.forwardFriction  = forwardFrictionCurve;
            wheelCollider.sidewaysFriction = sidewaysFrictionCurve;

            // Also default damp too.
            wheelCollider.wheelDampingRate = physicsFrictions[0].damp;

            // If dontUseAnyParticleEffects bool is not selected in RCC_Settings, set emission to ground physic material smoke to default one.
            if (!RCCSettings.dontUseAnyParticleEffects)
            {
                emission = allWheelParticles[0].emission;
            }

            // Set audioclip to ground physic material sound to default one.
            audioClip = physicsFrictions[0].groundSound;

            // If wheel slip is bigger than ground physic material slip, enable particles. Otherwise, disable particles.
            if (!RCCSettings.dontUseAnyParticleEffects)
            {
                if (wheelSlipAmountSideways > physicsFrictions [0].slip || wheelSlipAmountForward > physicsFrictions [0].slip)
                {
                    emission.enabled = true;
                }
                else
                {
                    emission.enabled = false;
                }
            }
        }

        // Last check if wheel has enabled smoke particles. If slip is smaller than target slip value, disable all of them.
        if (!RCCSettings.dontUseAnyParticleEffects)
        {
            for (int i = 0; i < allWheelParticles.Count; i++)
            {
                if (wheelSlipAmountSideways > startSlipValue || wheelSlipAmountForward > startSlipValue)
                {
                }
                else
                {
                    emission         = allWheelParticles [i].emission;
                    emission.enabled = false;
                }
            }
        }
    }
Exemplo n.º 14
0
    // Update is called once per frame
    void Update()
    {
        //if player is moving
        if (rigid.velocity.magnitude > 0.1)
        {
            //if running or walking
            if (animator.GetCurrentAnimatorStateInfo(0).IsName("Locomotion"))
            {
                if (Terrain.activeTerrain != null)
                {
                    surfaceIndex = TerrainSurface.GetMainTexture(transform.position);
                }
                else
                {
                    surfaceIndex = 3; //make stone the default sound
                }

                //while in the wild and above water
                if (!inWater)
                {
                    if (surfaceIndex == 0) //is grass
                    {
                        footsteps.clip = grass;
                    }

                    if (surfaceIndex == 1) //is sand
                    {
                        footsteps.clip = sand;
                    }

                    if (surfaceIndex == 2) //is dirt
                    {
                        footsteps.clip = dirt;
                    }

                    if (surfaceIndex == 3) //is stone
                    {
                        footsteps.clip = stone;
                    }

                    if (surfaceIndex == 4) //is stone path
                    {
                        footsteps.clip = stone;
                    }
                }
                else //must be in the water (might want to update later to ensure he's not swimming)
                {
                    //but if in dungeon you're not
                    if (SceneManager.GetActiveScene().buildIndex == 3)
                    {
                        footsteps.clip = stone;
                    }
                    else
                    {
                        footsteps.clip = water;
                    }
                }

                //play sound if not playing
                if (!footsteps.isPlaying)
                {
                    footsteps.Play();
                }
            }
            else
            {
                //no longer in motion stop footsteps
                if (footsteps.isPlaying)
                {
                    footsteps.Stop();
                }
            }
        }
        else
        {
            //no longer in motion stop footsteps
            if (footsteps.isPlaying)
            {
                footsteps.Stop();
            }
        }

        if (transform.position.y > waterLevel)
        {
            inWater = false;
        }
    }
Exemplo n.º 15
0
    void GetWheelContact()
    {
        float castDist = Mathf.Max(suspensionParent.suspensionDistance * Mathf.Max(0.001f, suspensionParent.targetCompression) + actualRadius, 0.001f);

        RaycastHit[] wheelHits = Physics.RaycastAll(suspensionParent.maxCompressPoint, suspensionParent.springDirection, castDist, GlobalControl.wheelCastMaskStatic);
        RaycastHit   hit;
        int          hitIndex = 0;
        bool         validHit = false;
        float        hitDist  = Mathf.Infinity;

        if (connected)
        {
            //Loop through raycast hits to find closest one
            for (int i = 0; i < wheelHits.Length; i++)
            {
                if (!wheelHits[i].transform.IsChildOf(vp.tr) && wheelHits[i].distance < hitDist)
                {
                    hitIndex = i;
                    hitDist  = wheelHits[i].distance;
                    validHit = true;
                }
            }
        }
        else
        {
            validHit = false;
        }

        //Set contact point variables
        if (validHit)
        {
            hit = wheelHits[hitIndex];

            if (!grounded && impactSnd && ((tireHitClips.Length > 0 && !popped) || (rimHitClip && popped)))
            {
                impactSnd.PlayOneShot(popped ? rimHitClip : tireHitClips[Mathf.RoundToInt(Random.Range(0, tireHitClips.Length - 1))], Mathf.Clamp01(airTime * airTime));
                impactSnd.pitch = Mathf.Clamp(airTime * 0.2f + 0.8f, 0.8f, 1);
            }

            grounded = true;
            contactPoint.distance         = hit.distance - actualRadius;
            contactPoint.point            = hit.point + localVel * Time.fixedDeltaTime;
            contactPoint.grounded         = true;
            contactPoint.normal           = hit.normal;
            contactPoint.relativeVelocity = tr.InverseTransformDirection(localVel);
            contactPoint.col = hit.collider;

            if (hit.collider.attachedRigidbody)
            {
                contactVelocity = hit.collider.attachedRigidbody.GetPointVelocity(contactPoint.point);
                contactPoint.relativeVelocity -= tr.InverseTransformDirection(contactVelocity);
            }
            else
            {
                contactVelocity = Vector3.zero;
            }

            GroundSurfaceInstance curSurface = hit.collider.GetComponent <GroundSurfaceInstance>();
            TerrainSurface        curTerrain = hit.collider.GetComponent <TerrainSurface>();

            if (curSurface)
            {
                contactPoint.surfaceFriction = curSurface.friction;
                contactPoint.surfaceType     = curSurface.surfaceType;
            }
            else if (curTerrain)
            {
                contactPoint.surfaceType     = curTerrain.GetDominantSurfaceTypeAtPoint(contactPoint.point);
                contactPoint.surfaceFriction = curTerrain.GetFriction(contactPoint.surfaceType);
            }
            else
            {
                contactPoint.surfaceFriction = hit.collider.material.dynamicFriction * 2;
                contactPoint.surfaceType     = 0;
            }

            if (contactPoint.col.CompareTag("Pop Tire") && canPop && airLeakTime == -1 && !popped)
            {
                Deflate();
            }
        }
        else
        {
            grounded = false;
            contactPoint.distance         = suspensionParent.suspensionDistance;
            contactPoint.point            = Vector3.zero;
            contactPoint.grounded         = false;
            contactPoint.normal           = upDir;
            contactPoint.relativeVelocity = Vector3.zero;
            contactPoint.col             = null;
            contactVelocity              = Vector3.zero;
            contactPoint.surfaceFriction = 0;
            contactPoint.surfaceType     = 0;
        }
    }
Exemplo n.º 16
0
    void PlayFootStepSoundKeyFrame()
    {
        if (noSounds == true)
        {
            return;
        }
        GameObject collision = GetCollider();

        if (collision != null)
        {
            if (debugSurface == true)
            {
                Debug.Log("Tag: " + collision.tag + " Name: " + collision.name);
            }
            if (collision.tag == "Terrain" || useTerrain == true)
            {
                hitSurfaceName = TerrainSurface.GetMainTexture(transform.position);
                if (debugSurface == true)
                {
                    Debug.Log("Surface: " + hitSurfaceName);
                }
                foreach (KeyMaterialList material in materialList)
                {
                    if (hitSurfaceName == material.name)
                    {
                        if (useOverrideVolume)
                        {
                            audioSource.volume = overrideVolume;
                        }
                        else
                        {
                            audioSource.volume = material.volume;
                        }
                        audioSource.clip = material.footstepSounds [UnityEngine.Random.Range(0, material.footstepSounds.Length)];
                        audioSource.Play();
                        return;
                    }
                }
            }
            else
            {
                foreach (KeyMaterialList material in materialList)
                {
                    if (collision.GetComponent <MeshRenderer> ())
                    {
                        hitSurfaceName = collision.GetComponent <MeshRenderer> ().material.name;
                        if (debugSurface == true)
                        {
                            Debug.Log("Material Name: " + hitSurfaceName);
                        }
                        if (useTerrain == false && (hitSurfaceName == material.name || hitSurfaceName == material.name + " (Instance)" &&
                                                    audioSource.isPlaying == false))
                        {
                            if (useOverrideVolume)
                            {
                                audioSource.volume = overrideVolume;
                            }
                            else
                            {
                                audioSource.volume = material.volume;
                            }
                            audioSource.clip = material.footstepSounds [UnityEngine.Random.Range(0, material.footstepSounds.Length)];
                            audioSource.Play();
                            return;
                        }
                    }
                    else
                    {
                        return;
                    }
                }
            }
        }
    }
    void  FixedUpdate()
    {
        if (skidmarks)
        {
            WheelHit GroundHit;
            wheelCollider.GetGroundHit(out GroundHit);

            wheelSlipAmountSideways = Mathf.Abs(GroundHit.sidewaysSlip);
            wheelSlipAmountForward  = Mathf.Abs(GroundHit.forwardSlip);

            if (wheelSlipAmountSideways > startSlipValue || wheelSlipAmountForward > .5f)
            {
                Vector3 skidPoint = GroundHit.point + 2f * (carRigid.velocity) * Time.deltaTime;

                if (carRigid.velocity.magnitude > 1f)
                {
                    lastSkidmark = skidmarks.AddSkidMark(skidPoint, GroundHit.normal, (wheelSlipAmountSideways / 2f) + (wheelSlipAmountForward / 2.5f), lastSkidmark);
                }
                else
                {
                    lastSkidmark = -1;
                }
            }

            else
            {
                lastSkidmark = -1;
            }
        }

        RaycastHit hit;

        if (Physics.Raycast(transform.position, -transform.up, out hit))
        {
            if (carController.UseTerrainSplatMapForGroundPhysic && hit.transform.gameObject.GetComponent <TerrainCollider>())
            {
                if (TerrainSurface.GetTextureMix(transform.position)[0] > .5f)
                {
                    SetWheelStiffnessByGroundPhysic(1f);
                }
                else if (TerrainSurface.GetTextureMix(transform.position)[1] > .5f)
                {
                    SetWheelStiffnessByGroundPhysic(2f);
                }
                else if (TerrainSurface.GetTextureMix(transform.position)[2] > .5f)
                {
                    SetWheelStiffnessByGroundPhysic(3f);
                }
                return;
            }

            if (hit.collider.material.name == grassPhysicsMaterial.name + " (Instance)")
            {
                SetWheelStiffnessByGroundPhysic(3f);
            }
            else if (hit.collider.material.name == sandPhysicsMaterial.name + " (Instance)")
            {
                SetWheelStiffnessByGroundPhysic(2f);
            }
            else
            {
                SetWheelStiffnessByGroundPhysic(1f);
            }
        }
    }
Exemplo n.º 18
0
    private void Shot()
    {
        StartCoroutine(playMuzzleFlash());
        StartCoroutine(playGunShotSound());
        Collider coll;
        Vector3  ray;

        if (overrideFiringPoint)
        {
            ray = overrideFiringPoint.position;
        }
        else
        {
            ray = transform.position;
        }
        //make less accurate
        GameObject enemy = GameObject.FindGameObjectWithTag("Player");

        if (enemy != null)
        {
            Vector3 direction = (enemy.transform.position - transform.position);
            direction.x += UnityEngine.Random.Range(-accuracy, accuracy);
            direction.y += UnityEngine.Random.Range(-accuracy, accuracy);
            direction.z += UnityEngine.Random.Range(-accuracy, accuracy);
            RaycastHit hit;
            if (debugRay == true)
            {
                Debug.DrawRay(ray, direction, Color.red, 2.0f);
            }
            //Hit something!
            if (Physics.Raycast(ray, direction, out hit, raycastDistance))
            {
                if (debugRayHit == true)
                {
                    Debug.Log("GAMEOBJECT NAME: " + hit.collider.gameObject.name + ", TAG: " + hit.collider.gameObject.tag);
                    if (hit.collider.gameObject.GetComponent <MeshRenderer> ())
                    {
                        Debug.Log("MESH: " + hit.collider.gameObject.GetComponent <MeshRenderer> ().name);
                    }
                }
                coll = hit.collider;
                //hit an object with health
                if (coll.gameObject.GetComponent <Health> ())
                {
                    coll.gameObject.GetComponent <Health> ().ApplyDamage(damage);
                }
                else if (coll.gameObject.transform.root.GetComponent <Health> ())
                {
                    coll.gameObject.transform.root.GetComponent <Health> ().ApplyDamage(damage);
                }
                foreach (HitVisuals hitObj in hitDictionary)
                {
                    if (coll.gameObject.tag == hitObj.tag)
                    {
                        hitObject(hit, hitObj);
                        return;
                    }
                }
                //hit a terrain
                if (coll.gameObject.tag == "Terrain")
                {
                    string hitSurfaceName = TerrainSurface.GetMainTexture(transform.position);
                    foreach (HitVisuals hitPoint in hitDictionary)
                    {
                        if (hitSurfaceName == hitPoint.materialName)
                        {
                            hitObject(hit, hitPoint);
                            return;
                        }
                    }
                }
                //hit something other than a terrain
                else if (coll.gameObject.GetComponent <MeshRenderer> ())
                {
                    string hitSurfaceName = coll.gameObject.GetComponent <MeshRenderer> ().material.name;
                    foreach (HitVisuals hitPoint in hitDictionary)
                    {
                        if (hitSurfaceName == hitPoint.materialName)
                        {
                            hitObject(hit, hitPoint);
                            return;
                        }
                    }
                }
            }
        }
    }