void CreateCarWaypointAfter() { GameObject waypointObject = new GameObject("CarWaypoint " + waypointRoot.childCount, typeof(CarWaypoint)); waypointObject.transform.SetParent(waypointRoot, false); CarWaypoint newWaypoint = waypointObject.GetComponent <CarWaypoint>(); CarWaypoint selectedWaypoint = Selection.activeGameObject.GetComponent <CarWaypoint>(); waypointObject.transform.position = selectedWaypoint.transform.position; waypointObject.transform.forward = selectedWaypoint.transform.forward; newWaypoint.previousWaypoint = selectedWaypoint; if (selectedWaypoint.nextWaypoint != null) { selectedWaypoint.nextWaypoint.previousWaypoint = newWaypoint; newWaypoint.nextWaypoint = selectedWaypoint.nextWaypoint; } selectedWaypoint.nextWaypoint = newWaypoint; newWaypoint.transform.SetSiblingIndex(selectedWaypoint.transform.GetSiblingIndex()); Selection.activeGameObject = newWaypoint.gameObject; }
void Start() { waypointsGO = GameObject.FindGameObjectsWithTag("Waypoint"); TrafficPerson = 0; TrafficCars = 0; //gameObjects = GameObject.FindGameObjectsWithTag("Untagged"); /* * int temp8 = 0; * int temp9 = 0; * * foreach (GameObject gameObject in gameObjects) { * if (gameObject.layer == 8) * { * waypointsGO[temp8] = gameObject; * temp8++; * } * else if (gameObject.layer == 9) { * carWaypointsGO[temp9] = gameObject; * temp9++; * } * } */ if (waypointsGO != null) { foreach (GameObject gameObject in waypointsGO) { Waypoint temp = gameObject.GetComponent <Waypoint>(); if (temp != null && temp.isSpawner == true) { waypointsStruct str = new waypointsStruct(); str.waypoint = temp; str.transform = gameObject.GetComponent <Transform>(); str.isActive = false; waypoints.Add(str); } } } carWaypointsGO = GameObject.FindGameObjectsWithTag("CarWaypoint"); if (carWaypointsGO != null) { foreach (GameObject gameObject in carWaypointsGO) { CarWaypoint temp = gameObject.GetComponent <CarWaypoint>(); if (temp != null && temp.isSpawner == true) { carWaypointsStruct str = new carWaypointsStruct(); str.carWaypoint = temp; str.transform = gameObject.GetComponent <Transform>(); str.isActive = false; carWaypoints.Add(str); } } } }
public static void OnDrawSceneGizmo(CarWaypoint waypoint, GizmoType gizmoType) { if (waypoint.isSpawner == false) { if ((gizmoType & GizmoType.Selected) != 0) { Gizmos.color = Color.yellow; } else { Gizmos.color = Color.yellow * 0.5f; } } else { Gizmos.color = Color.green; } Gizmos.DrawSphere(waypoint.transform.position, .1f); Gizmos.color = Color.white; Gizmos.DrawLine(waypoint.transform.position + (waypoint.transform.right * waypoint.width / 2f), waypoint.transform.position - (waypoint.transform.right * waypoint.width / 2f)); if (waypoint.previousWaypoint != null) { Gizmos.color = Color.blue; Vector3 offset = waypoint.transform.right * waypoint.width / 2f; Vector3 offsetTo = waypoint.previousWaypoint.transform.right * waypoint.previousWaypoint.width / 2f; Gizmos.DrawLine(waypoint.transform.position + offset, waypoint.previousWaypoint.transform.position + offsetTo); } if (waypoint.nextWaypoint != null) { Gizmos.color = Color.green; Vector3 offset = waypoint.transform.right * -waypoint.width / 2f; Vector3 offsetTo = waypoint.nextWaypoint.transform.right * -waypoint.nextWaypoint.width / 2f; Gizmos.DrawLine(waypoint.transform.position + offset, waypoint.nextWaypoint.transform.position + offsetTo); } if (waypoint.branches != null) { foreach (CarWaypoint branch in waypoint.branches) { Gizmos.color = Color.black; Gizmos.DrawLine(waypoint.transform.position, branch.transform.position); } } }
void RemoveCarWaypoint() { CarWaypoint selectedWaypoint = Selection.activeGameObject.GetComponent <CarWaypoint>(); if (selectedWaypoint.nextWaypoint != null) { selectedWaypoint.nextWaypoint.previousWaypoint = selectedWaypoint.previousWaypoint; } if (selectedWaypoint.previousWaypoint != null) { selectedWaypoint.previousWaypoint.nextWaypoint = selectedWaypoint.nextWaypoint; Selection.activeGameObject = selectedWaypoint.previousWaypoint.gameObject; } DestroyImmediate(selectedWaypoint.gameObject); }
void CreateCarBranch() { GameObject waypointObject = new GameObject("CarWaypoint " + waypointRoot.childCount, typeof(CarWaypoint)); waypointObject.transform.SetParent(waypointRoot, false); CarWaypoint waypoint = waypointObject.GetComponent <CarWaypoint>(); CarWaypoint branchedFrom = Selection.activeGameObject.GetComponent <CarWaypoint>(); branchedFrom.branches.Add(waypoint); waypoint.transform.position = branchedFrom.transform.position; waypoint.transform.forward = branchedFrom.transform.forward; Selection.activeGameObject = waypoint.gameObject; }
void CreateCarWaypoint() { GameObject waypointObject = new GameObject("CarWaypoint " + waypointRoot.childCount, typeof(CarWaypoint)); waypointObject.transform.SetParent(waypointRoot, false); CarWaypoint waypoint = waypointObject.GetComponent <CarWaypoint>(); if (waypointRoot.childCount > 1) { waypoint.previousWaypoint = waypointRoot.GetChild(waypointRoot.childCount - 2).GetComponent <CarWaypoint>(); waypoint.previousWaypoint.nextWaypoint = waypoint; waypoint.transform.position = waypoint.previousWaypoint.transform.position; waypoint.transform.forward = waypoint.previousWaypoint.transform.forward; } Selection.activeObject = waypoint.gameObject; }
void Update() { if (isDriveble == true) { if (isActive == true) { angle = inputController.hAxis * wheelAngle; FL_collider.steerAngle = angle; FR_collider.steerAngle = angle; FLtr.localEulerAngles = new Vector3(0, angle, 0); FRtr.localEulerAngles = new Vector3(0, 180 + angle, 0); float vAxis = inputController.vAxis; //rpm = RR_collider.rpm; UpadateWheelPose(FL_collider, FLtr); UpadateWheelPose(FR_collider, FRtr); UpadateWheelPose(RL_collider, RLtr); UpadateWheelPose(RR_collider, RRtr); if (inputController.shiftButton == true) { FL_collider.brakeTorque = wheelBrake; FR_collider.brakeTorque = wheelBrake; RL_collider.brakeTorque = wheelBrake; RR_collider.brakeTorque = wheelBrake; } else { FL_collider.brakeTorque = 0; FR_collider.brakeTorque = 0; RL_collider.brakeTorque = 0; RR_collider.brakeTorque = 0; } torque = vAxis * wheelPower; RL_collider.motorTorque = torque; RR_collider.motorTorque = torque; } else { RL_collider.motorTorque = 0; RR_collider.motorTorque = 0; } } else { Vector3 destinationDirection = destination - transform.position; destinationDirection.y = 0; float destinationDistance = destinationDirection.magnitude; float angle = Vector3.SignedAngle(destinationDirection, transform.forward, transform.up) * -1; if (Mathf.Abs(angle) > wheelAngle) { if (angle > 0) { angle = wheelAngle; } else { angle = -wheelAngle; } } // Bit shift the index of the layer (8) to get a bit mask //int layerMask = 1 << 8; // This would cast rays only against colliders in layer 8. // But instead we want to collide against everything except layer 8. The ~ operator does this, it inverts a bitmask. //layerMask = ~layerMask; RaycastHit hit; //Vector3 from = new Vector3(transform.position.x, transform.position.y + 1f, transform.position.z+5f); //Vector3 to = transform.TransformDirection(Vector3.forward); //to.y = to.y + 1f; //to.z = to.z + 5f; Vector3 up = new Vector3(0, 1, 0); Ray ray = new Ray(transform.position + up, transform.forward); ray.direction = destinationDirection; float RCD = rayCastDistance + carRigidbody.velocity.magnitude * 0.5f; // Does the ray intersect any objects excluding the player layer if (Physics.Raycast(ray, out hit, RCD)) { Debug.DrawRay(transform.position + up, ray.direction.normalized * hit.distance, Color.yellow); //Debug.Log("Did Hit"); rayCastActive = true; } else { rayCastActive = false; Debug.DrawRay(transform.position + up, ray.direction.normalized * RCD, Color.white); //Debug.Log("Did not Hit"); } if (reachedDestination == false && currentWaypoint.isReadyToGo == true && rayCastActive == false) { if (destinationDistance >= stopDistance) //&& reachedDestination == false) { reachedDestination = false; /*if (Vector3.Angle(destinationDirection, transform.forward) <= wheelAngle) * { * * targetRotation = Quaternion.LookRotation(destinationDirection); * * * * } * * FLtr.transform.rotation = Quaternion.RotateTowards(FLtr.transform.rotation, targetRotation, 200 * Time.deltaTime); * FRtr.transform.rotation = FLtr.transform.rotation; * FRtr.eulerAngles = new Vector3(0, 180 + FRtr.eulerAngles.y, 0); * * FL_collider.steerAngle = FLtr.transform.localEulerAngles.y; * FR_collider.steerAngle = FRtr.transform.localEulerAngles.y; */ FL_collider.steerAngle = angle; FR_collider.steerAngle = angle; FLtr.localEulerAngles = new Vector3(0, angle, 0); FRtr.localEulerAngles = new Vector3(0, 180 + angle, 0); if (carRigidbody.velocity.magnitude > maxSpeed || reachedDestination == true) { FL_collider.brakeTorque = wheelBrake; FR_collider.brakeTorque = wheelBrake; RL_collider.brakeTorque = wheelBrake; RR_collider.brakeTorque = wheelBrake; RL_collider.motorTorque = 0; RR_collider.motorTorque = 0; } else { FL_collider.brakeTorque = 0; FR_collider.brakeTorque = 0; RL_collider.brakeTorque = 0; RR_collider.brakeTorque = 0; RL_collider.motorTorque = wheelPower; RR_collider.motorTorque = wheelPower; } /* * //float angle = Vector3.Angle(destinationDirection,transform.forward); * angle = targetRotation.eulerAngles.y; * * print(angle); * * if (Mathf.Abs(angle) > wheelAngle) * { * if (angle >= 180) { * angle = wheelAngle; * } * else { * angle = -wheelAngle; * } * * * } * FL_collider.steerAngle = angle; * FR_collider.steerAngle = angle; * * * * //FLtr.localEulerAngles = new Vector3(0, angle, 0); * //FRtr.localEulerAngles = new Vector3(0, 180 + angle, 0); * */ // transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime); // transform.Translate(Vector3.forward * movementSpeed * Time.deltaTime); } else { reachedDestination = true; bool shouldBranch = false; if (currentWaypoint.branches != null && currentWaypoint.branches.Count > 0) { shouldBranch = Random.Range(0f, 1f) <= currentWaypoint.branchRatio ? true : false; } if (shouldBranch) { currentWaypoint = currentWaypoint.branches[Random.Range(0, currentWaypoint.branches.Count - 1)]; SetDestination(currentWaypoint.GetPosition()); maxSpeed = currentWaypoint.maxSpeed; } else { if (currentWaypoint.nextWaypoint != null) { currentWaypoint = currentWaypoint.nextWaypoint; SetDestination(currentWaypoint.GetPosition()); maxSpeed = currentWaypoint.maxSpeed; } } } } else { FL_collider.brakeTorque = wheelBrake; FR_collider.brakeTorque = wheelBrake; RL_collider.brakeTorque = wheelBrake; RR_collider.brakeTorque = wheelBrake; RL_collider.motorTorque = 0; RR_collider.motorTorque = 0; } } }
/* void ActivTrue() { * isActive = true; * } * * void ActivFalse() * { * isActive = false; * * }*/ public void CurrentWaypoint(CarWaypoint waypoint) { currentWaypoint = waypoint; SetDestination(currentWaypoint.GetPosition()); }