Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Spacemarine658 · Sep 25, 2015 at 08:02 AM · c#car

Only make changes to one prefab instance

Ok so i have a car script on a car but when i drag another instance of it from the prefabs and get in the first car both move and ive looked into how to control one a time but no one seems to cover this issue and if you want ill post the code but it's really long lol

Comment
Add comment · Show 23
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
avatar image hexagonius · Sep 25, 2015 at 10:30 AM 0
Share

just a guess here. you're probably checking input in your class which means it's getting checked on every instance. the easiest approach would be to create a public bool which you check in update. If that's inactive, return. this way you can start toggling cars instances on and off in the inspector. a more in-game programmatic approach is up to you then.

avatar image Spacemarine658 hexagonius · Sep 26, 2015 at 02:12 AM 0
Share

I understand what your saying but im not understand how to implement this in my script lol here is my script maybe you can explain to me whats wrong with it

public class VehicleController : $$anonymous$$onoBehaviour

{

 public float maxTorque = 1000f;
 public float speed;
 GameObject player;
 public Transform centerOf$$anonymous$$ass;
 public WheelCollider[] wheelColliders = new WheelCollider[4];
 public Transform[] tire$$anonymous$$eshes = new Transform[4];
 private Rigidbody m_rigidbody;
 public bool inCar = false;
 public bool driving = false;
 float collisionSpeed;

 void Start()
 {
     player = GameObject.Find ("Player");
     m_rigidbody = gameObject.GetComponent<Rigidbody> ();
     m_rigidbody.centerOf$$anonymous$$ass = centerOf$$anonymous$$ass.localPosition;
 }
 void Update()
 {
     if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.H)) {
         if (((player.GetComponent<PlayerController> ().withinRange == true) || inCar == true)) {
             putPlayerInCar ();
         }
     }

     speed = (m_rigidbody.velocity.magnitude * 2.237f);

     if (collisionSpeed >= 40f) 
     {
         Destroy (player);
         Destroy (gameObject);
     }

     Update$$anonymous$$eshesPositions ();
 }

 void Update$$anonymous$$eshesPositions()
 {
     for (int i = 0; i < 4; i++) 
     {
         Quaternion quat;
         Vector3 pos;
         wheelColliders [i].GetWorldPose (out pos, out quat);

         tire$$anonymous$$eshes [i].position = pos;
         tire$$anonymous$$eshes [i].rotation = quat;
     }
 }

 void putPlayerInCar()
 {
     if (inCar == true)
         inCar = false;
     else
         inCar = true;
     
     if (inCar == true) {
         GameObject.Find ("$$anonymous$$ain Camera").GetComponent<SmoothFollow> ().target = gameObject.transform;
         player.GetComponent<$$anonymous$$eshRenderer> ().enabled = false;
         player.GetComponent<CapsuleCollider> ().enabled = false;
         player.GetComponent<Rigidbody> ().useGravity = false;
     } 
     else 
     {
         player.GetComponent<PlayerController> ().moveCameraToPlayer ();
         player.GetComponent<$$anonymous$$eshRenderer> ().enabled = true;
         player.GetComponent<CapsuleCollider> ().enabled = true;
         player.GetComponent<Rigidbody> ().useGravity = true;
         player.transform.position = (gameObject.transform.position + new Vector3(-4,1,0));
         player.transform.rotation = gameObject.transform.rotation;
     }
     player.GetComponent<PlayerController> ().enabled = !player.GetComponent<PlayerController> ().enabled;
 }
avatar image Spacemarine658 hexagonius · Sep 26, 2015 at 02:12 AM 0
Share
 void FixedUpdate()
 {
     if (inCar == true && 
         (gameObject.GetComponent<$$anonymous$$oduleDamageScript>().myEngineHealth == $$anonymous$$oduleDamageScript.EngineHealth.Full || 
             gameObject.GetComponent<$$anonymous$$oduleDamageScript>().myEngineHealth == $$anonymous$$oduleDamageScript.EngineHealth.Damaged)) 
     {
         float steer = Input.GetAxis ("Horizontal");
         float accelerate = Input.GetAxis ("Vertical");
         if (gameObject.GetComponent<$$anonymous$$oduleDamageScript> ().myEngineHealth == $$anonymous$$oduleDamageScript.EngineHealth.Damaged) 
         {
             accelerate = accelerate / 2;
         }
         float finalAngle = steer * 45f;
         wheelColliders [0].steerAngle = finalAngle;
         wheelColliders [1].steerAngle = finalAngle;

         if ((accelerate == 0) && Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Space)) {
             wheelColliders [0].brakeTorque = maxTorque * 10;
             wheelColliders [1].brakeTorque = maxTorque * 10;
             wheelColliders [2].brakeTorque = maxTorque * 10;
             wheelColliders [3].brakeTorque = maxTorque * 10;
         } else if (accelerate == 0 && (wheelColliders [0].rpm > 0 || wheelColliders [0].rpm < 0)) {
             wheelColliders [0].brakeTorque = maxTorque / 2;
             wheelColliders [1].brakeTorque = maxTorque / 2;
             wheelColliders [2].brakeTorque = maxTorque / 2;
             wheelColliders [3].brakeTorque = maxTorque / 2;
         } else {
             wheelColliders [0].brakeTorque = 0;
             wheelColliders [1].brakeTorque = 0;
             wheelColliders [2].brakeTorque = 0;
             wheelColliders [3].brakeTorque = 0;
         }
         for (int i = 0; i < 4; i++) {
             wheelColliders [i].motorTorque = accelerate * maxTorque;
         }
     }
 }
 void OnCollisionEnter(Collision collision)
 {
     collisionSpeed = collision.relativeVelocity.magnitude;
 }
 void OnGUI()
 {
     if (inCar == true) 
     {
         GUI.Box (new Rect (0, 0, 100, 50), " ");
         GUI.Label (new Rect (0, 0, 100, 20), (speed/2).ToString ("F0"));
         GUI.Label (new Rect (0, 15, 100, 20), (collisionSpeed).ToString ());
         GUI.Label (new Rect (0, 30, 100, 20), gameObject.GetComponent<$$anonymous$$oduleDamageScript> ().EngineHealthCounter.ToString ("F0"));

}}}

avatar image SpaceManDan Spacemarine658 · Sep 27, 2015 at 08:26 AM 1
Share

Where is the bool 'driving' being called. is that in use here?

Show more comments
Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by SpaceManDan · Oct 01, 2015 at 01:38 AM

First thing you'll need to do is remove the isNear variable we created in the car script. Also, remove the trigger collider objects we created on the cars. You will not need them anymore.

Then, remove the following code from the car script.

 if (Input.GetKeyDown("h") && isNear)
 {
      if (inCar == true)
          inCar = false;
      else
          inCar = true;
 }


Then add this code into the script that will controller your player. If you do not have a script on your player, create one. For example it could be called PlayerController. I believe you said you have a controller that moves forwards and backwards so far, so this should go into that script.

 private bool interactRayActive = true; //Use this to turn on the entire ray system.  For example set this to false if the player is inside of the vehicle so that the ray is disabled while driving. (saves on resources).

 private float interactLength = 5f;  //how far the ray will travel.
 private bool lookingAtCar;

 private GameObject playerHead; //The gameobject that will fire the ray.

 private void Start()
 {
     playerHead = GameObject.Find("PLAYER GAMEOBJECT NAME HERE");
 }

 private void Update()
 {
     if (interactRayActive)
     {
         RaycastHit interactRayHitInfo;
         Ray interactRay = new Ray(playerHead.transform.position, playerHead.transform.forward);
         bool interactRayHit = Physics.Raycast(interactRay, out interactRayHitInfo, interactLength);

         //Remove the comments from this if statment if you want to see the name of the object being hit.
         //if (interactRayHit)
         //{
         //    Debug.Log(interactRayHitInfo.transform.name);
         //}

         //This checks to see if the ray hit something.  If true (yes it hit something) and that thing is on layer "Layer #" then do the following
         if (interactRayHit && interactRayHitInfo.transform.gameObject.layer == PUTLAYERNUMBERHERE) //Be sure to create and set your vehicles to a layer
         {
             if (interactRayHitInfo.transform.tag == "Vehicle") //tag the vehicles with this tag.
             {
                 lookingAtCar = true;
             }
             if (interactRayHitInfo.transform.name != "Vehicle") // != means "NOT equal to"
             {
                 lookingAtCar = false;
             }

         }
         else if (interactRayHit == false) //if the ray is not hitting anything this stuff will happen.
         {
             lookingAtCar = false;
         }

         //Now use lookingAtCar the same way we did your trigger.
         if (lookingAtCar && Input.GetKeyDown(KeyCode.H))
         {
             interactRayHitInfo.transform.GetComponent<CARSCRIPTNAMEHERE>().inCar = true;
         }
     }
 }

Ok, cool. Let me know if you have trouble hooking this all up. Good luck buddy,

Comment
Add comment · Show 8 · Share
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
avatar image Spacemarine658 · Oct 03, 2015 at 02:40 PM 0
Share

sorry been sick im hooking it all up now idk if you get notifications if i comment(i dont lol) but on the raycast the second if says .name is that what you meant ins$$anonymous$$d of .tag like the first one? haven't test it out yet just noticed it lol

avatar image SpaceManDan Spacemarine658 · Oct 03, 2015 at 07:01 PM 0
Share

@Spacemarine658 I do get notifications. I didn't change any settings so I'm not sure what the deal is. I will target you when I comment so it sends them to ya.

And good eye. The second if should be .tag not .name.

avatar image Spacemarine658 SpaceManDan · Oct 03, 2015 at 11:25 PM 0
Share

ok lol i may jut be missing them lol and ok was just checking its not working right now but i think thats due to my entering car script more than anything lol

Show more comments
Show more comments
avatar image
0

Answer by Spacemarine658 · Sep 30, 2015 at 02:11 PM

Yes i do have a player with a player controller (i haven't added turning yet )

Comment
Add comment · Show 4 · Share
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
avatar image SpaceManDan · Sep 30, 2015 at 06:17 PM 0
Share

Oh god thank you. We needed a new comment so bad.

Ok so it is good you have a moving character but you should keep in $$anonymous$$d that the way I will make this ray will cause it to emanate from the center of the player object in a straight line where ever it is facing (the objects forward direction). If the car is too short or the player to tall the ray may go over it or under it. Hopefully this wont be an issue but Im letting you know just in case.

Ok, off to work but when I get home, I finally have all I need to make this work. Talk to you soon.

avatar image Spacemarine658 SpaceManDan · Oct 10, 2015 at 08:40 PM 0
Share

i figured it out lol

avatar image SpaceManDan Spacemarine658 · Oct 10, 2015 at 10:06 PM 1
Share

Oh hey dude. I got caught up with stuff and forgot to check in on you. Sorry I left you hanging. Life got busy on me.

Glad to hear you got it rolling. Feel free to ping me if you get stuck again with anything else. Spacemandan01 a t Gmail. Good luck buddy.

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

I can't fix these errors.. 1 Answer

changing rpm when shifting 1 Answer

How to fix this? C# 1 Answer

Car will only turn right (& ignored collders) 0 Answers

Problem in steering with wheelcolliders 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