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
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.
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;
}
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"));
}}}
Where is the bool 'driving' being called. is that in use here?
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,
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
@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.
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
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 )
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.
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.
Your answer
Follow this Question
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