- Home /
Question by
patrick13 · Apr 21, 2014 at 09:45 AM ·
movementpathcarsintelligence
IA vehicle: trajectory according to path of MAYA + index
Hello I use a script to move my vehicles. To do the route, I have to make this one stage by stage. I created the path by adding manually marks. These are tags in a way, when we begin the game, the vehicle follows the route.
In the script below, I have to give the "nomChemin" Then vehicle follows the way: "void NextIndex"
Can you modify the script so that the vehicle follows the path of MAYA and begins in the IndexDeDepartVehicule (index for start of vehicule)?
Thank you
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CarScript : MonoBehaviour {
public string nomChemin;
public float vitesseKmh;
float vitesse;
float sp;
List <Transform> chemin;
float range = 0.5f,ratio;
Transform _transform,rayPoint;
int index;
bool stopCar,emergencyStop,noStopZone;
Rigidbody _rigidbody;
float acceleration;
int layerVehicule = 1 << 8;
void Start(){
_rigidbody = rigidbody;
_rigidbody.centerOfMass += new Vector3(0f, 0f, 1.0f);
vitesse = vitesseKmh/3.6f;
sp = vitesse;
acceleration = 10;
rayPoint = transform.Find ("RaycastPoint");
_transform = transform;
ControlScript sc =GameObject.Find("Controle").GetComponent<ControlScript>();
chemin = new List<Transform> (sc.GetPath(nomChemin));
ratio = 0;
stopCar = emergencyStop=noStopZone = false;
layerVehicule= ~layerVehicule;
}
void Update()
{
RaycastHit hit;
float distance = 5.0f;
if(!stopCar){
if(vitesse<sp)vitesse+=Time.deltaTime*acceleration;
else if (vitesse > sp)vitesse -=Time.deltaTime*acceleration;
}else if(stopCar){
if(vitesse>0)
vitesse -= Time.deltaTime*acceleration;
else
vitesse=0;
}
if(Physics.Raycast(rayPoint.position,_transform.forward,out hit,distance)){
vitesse = (sp * (hit.distance/distance));
}
Vector3 dir = _transform.position - chemin[index].position;
if (dir.sqrMagnitude > range)
{
Move(chemin[index].position);
}
else NextIndex();
}
void Move(Vector3 target)
{
_transform.Translate(0,0,Time.deltaTime*vitesse);
if(ratio<1){
ratio += Time.deltaTime*0.1f;
var newRotation = Quaternion.LookRotation(target - _transform.position);
_transform.rotation = Quaternion.Lerp(_transform.rotation,newRotation,ratio);
}
}
/*void NextIndex()
{
if (++index == chemin.Count) index = 0;
ratio = 0;
}*/
void NextIndex()
{
if (++index >= chemin.Count) {
index = 0;
transform.position = chemin[index].position;
}else{
if (++index == chemin.Count) index = 0;
ratio = 0;
}
}
public void StopCar(bool b){
if(!noStopZone)
stopCar = b;
}
public void SetSpeed(float f){
sp = f;
}
public float GetSpeed(){
return _rigidbody.velocity.magnitude;
}
public void EmergencyStop(){
if(emergencyStop){
stopCar = true;
acceleration = 20.0f;
StartCoroutine (SetAcceleration());
}
}
IEnumerator SetAcceleration(){
if(vitesse != 0)yield return null;
acceleration = 10.0f;
emergencyStop = false;
}
public void SetEmergencyStop(bool b){
emergencyStop = b;
}
public void SetNoStop(bool b){
noStopZone = b;
}
public void Reset(){
stopCar = false;
noStopZone=false;
emergencyStop= false;
}
}
Comment