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; } } } }