je cherche à changer le pitch (hauteur) du son du moteur de mon véhicule en fonction de sa vitesse sur l'axe X. je précise que c'est un projet Unity 2D au cas où ce serait la cause du non-fonctionnement.
Voici le script qui ne marche pas:
Code : Tout sélectionner
using System.Collections;
using UnityEngine;
public class Moteur : MonoBehaviour {
public float topspeed = 100f;
private float currentSpeed = 0f;
private float pitch = 0f;
AudioSource audio;
Rigidbody2D rigidbody2D;
// Use this for initialization
void Start () {
audio = GetComponent <AudioSource> ();
rigidbody2D = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void Update () {
currentSpeed = rigidbody2D.velocity.X * 3.6f;
pitch = currentSpeed / topspeed;
audio.pitch = pitch;
}
}