void Update() { //press space to decouple all trucks from yourself and eachother if (Input.GetKeyDown(KeyCode.Space)) { foreach (GameObject link in linked) { if (link != null) { TruckMove truckMove = link.GetComponent <TruckMove>(); if (truckMove != null) { truckMove.RemoveAllConnections(train); } } } linked[0] = null; linked[1] = null; } }
public void RemoveAllConnections(GameObject caller) { print("name: " + truck.name); if (linked[0] != null) { print(linked[0].name); } else { print("Null"); } if (linked[1] != null) { print(linked[1].name); } else { print("Null"); } for (int i = 0; i <= 1; i++) { if (linked[i] != null) { TruckMove localTruck = linked[i].GetComponent <TruckMove>(); linked[i] = null; if (localTruck == caller) { } if (localTruck != null && localTruck != caller && localTruck != truck && localTruck != train) { localTruck.RemoveAllConnections(truck); } } } linked[0] = null; linked[1] = null; }
void FixedUpdate() { // Move left/right with <- and -> or 'a' and 'd' direction = Input.GetAxis("Horizontal"); if (direction != 0) { // Movement Vector3 horizonalPosition = transform.position + new Vector3(direction * smooth, 0); for (float i = -smooth; i <= smooth; i++) { Vector3 rayPosition = horizonalPosition + new Vector3(0, 0, i); Ray rayMove = new Ray(rayPosition, Vector3.down); RaycastHit hitInfoMove, hitInfoTruck; if (Physics.Raycast(rayMove, out hitInfoMove, 25f, layerMask)) { Vector3 angle = rayPosition - transform.position; angle.Normalize(); //if we hit a truck then couple it Ray rayTruck = new Ray(rayPosition, angle); if (Physics.Raycast(rayTruck, out hitInfoTruck, 7f)) { if (hitInfoTruck.collider.gameObject.tag == "Truck") { GameObject truck = hitInfoTruck.collider.gameObject; TruckMove truckmove = truck.GetComponent <TruckMove>(); if (direction < 0) { linked[0] = truck; } else { linked[1] = truck; } truckmove.AddConnection(train, direction); } } else { transform.position = rayPosition; break; } } } // Rotation Ray ray = new Ray(transform.position, Vector3.down); RaycastHit hitInfoRotate; if (Physics.Raycast(ray, out hitInfoRotate, 100f, layerMask)) { Quaternion newRotation = Quaternion.LookRotation(hitInfoRotate.transform.right, hitInfoRotate.transform.up); float yComponent = newRotation.eulerAngles.y; if (Mathf.Abs(yComponent - transform.rotation.eulerAngles.y) >= 180) { yComponent = yComponent - 180; } //this if statement may be unecessary if (yComponent >= 360) { yComponent = yComponent - 360; } newRotation = Quaternion.Euler(newRotation.eulerAngles.x, yComponent, newRotation.eulerAngles.z); transform.rotation = newRotation; } } }
private void FixedUpdate() { //movement //vert is w/s //horiz is a/d if (linked[0] != null || linked[1] != null) { direction = train.GetDirection(); if (direction != 0) { Vector3 horizonalPosition = transform.position + new Vector3(direction * smooth, 0); for (float i = -smooth; i <= smooth; i++) { Vector3 rayPosition = horizonalPosition + new Vector3(0, 0, i); Ray rayMove = new Ray(rayPosition, Vector3.down); RaycastHit hitInfoMove, hitInfoTruck; if (Physics.Raycast(rayMove, out hitInfoMove, 25f, layerMask)) { Vector3 angle = rayPosition - transform.position; angle.Normalize(); //creating new coupling if collide Ray rayTruck = new Ray(rayPosition, angle); if (Physics.Raycast(rayTruck, out hitInfoTruck, 7f)) { if (hitInfoTruck.collider.gameObject.tag == "Truck") { GameObject newTruck = hitInfoTruck.collider.gameObject; TruckMove truckmove = newTruck.GetComponent <TruckMove>(); if (direction < 0) { linked[0] = newTruck; } else { linked[1] = newTruck; } truckmove.AddConnection(truck, direction); } } else { transform.position = rayPosition; break; } } } // Rotation Ray ray = new Ray(transform.position, Vector3.down); RaycastHit hitInfoRotate; if (Physics.Raycast(ray, out hitInfoRotate, 100f, layerMask)) { Quaternion newRotation = Quaternion.LookRotation(hitInfoRotate.transform.right, hitInfoRotate.transform.up); float yComponent = newRotation.eulerAngles.y; if (Mathf.Abs(yComponent - transform.rotation.eulerAngles.y) >= 180) { float temp = yComponent - 180; yComponent = yComponent - 180; } //this if statement may be unecessary if (yComponent >= 360) { yComponent = yComponent - 360; } newRotation = Quaternion.Euler(newRotation.eulerAngles.x, yComponent, newRotation.eulerAngles.z); transform.rotation = newRotation; } } } }