Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
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
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

iTween Path Editor: ignore Y position in Vector3 1 Answer

How can I get an object to intercept a moving object's path? 3 Answers

how to move along path with user controll 0 Answers

Make Character move along a curved path at a constant speed 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges