// Update is called once per frame void Update() { if (visibility.alphaDown != 0) { return; } if (requireGameStarted && !SNet_Manager.instance.gameStatus.iS) { return; } if (hold) { if (Input.GetButtonDown(buttonName)) { if (InputBlocker.isBlocked()) { return; } targetPanel.Open(); } if (Input.GetButtonUp(buttonName)) { if (InputBlocker.isBlocked()) { return; } targetPanel.Open(false); } } else { if (Input.GetButtonDown(buttonName)) { if (InputBlocker.isBlocked()) { return; } targetPanel.Open(!targetPanel.activeSelf); if (targetPanel.activeSelf && hideMe) { visibility.Open(false); } } } }
// Update is called once per frame void Update() { if (SNet_Controller.user != null && !SNet_Controller.user.isDead) { if (SNet_Controller.user.currentVehicle != null) { SetInteractable(null); if (Input.GetButtonDown("Interact") && !InputBlocker.isBlocked()) { SNet_Network.instance.Send_Message(new Interact(SNet_Controller.user.currentVehicle.identity), SNet_Network.currentHost); } } else { float thickness = 0.5f; RaycastHit hit; if (Physics.SphereCast(SNet_Controller.user.aimBone.position, thickness, MouseOrbitImproved.instance.transform.forward, out hit, 3, lm)) { switch (hit.collider.tag) { case "Vehicle": SetInteractable(hit.collider); infoText.text = "Enter the vehicle " + hit.collider.name; break; case "Item": SetInteractable(hit.collider); infoText.text = "Take " + hit.collider.GetComponent <Item>().item.prefabName; break; case "NPC": SetInteractable(hit.collider); infoText.text = "Open Shop"; break; } } else { SetInteractable(null); } if (interactable != null && Input.GetButtonDown("Interact")) { SNet_Network.instance.Send_Message(new Interact(interactable.GetComponent <SNet_Identity>().identity), SNet_Network.currentHost); } } } panel_Interact.SetActive(interactable != null); }
// Update is called once per frame private void FixedUpdate() { if (Client.Instance != null) { /* * If there is no players in vehicle, stop the vehicle * */ if (vehicle.p.Count == 0 || vehicle.p[0] == 0) { // No driver info.m = Mathf.Clamp(info.m - Time.deltaTime * 100, 0, 1 / maxMotorTorque); // Slow down gas info.a = 0; // Reset the steering info.b = 0; // Let's get land. } } /// SAMPLE HELICOPTER CODE IS STARTING /// /// Calculating ground distance for helicopter. float groundDist = HeightRaycaster.DistToGround(transform.position); if (isLocalVehicle) { /* * Only the driver can control the vehicle * * */ if (InputBlocker.isBlocked()) { // If we are on a UI Input. info.m = 0; info.a = 0; info.b = 0; } else { /* * USE THE HELICOPTER * */ float vert = Input.GetAxis("Vertical"); if (vert > 0 && groundDist > 1) // 1m is for waiting for rising. { info.m = maxMotorTorque * vert; } else { info.m = 0; } info.a = steeringPower * Input.GetAxis("Horizontal"); if (vert < 0) // descending { info.b = vert * risingPower / 2f; } else { info.b = Mathf.Abs(Input.GetAxis("Jump")) * risingPower; } } /* * THIS IS IMPORTANT, DRIVER MUST SYNC THE VEHICLE ON NETWORK. * */ if (sync < Time.time) { sync = Time.time + 0.1f; // Sync rate is ~10 times in one second. SNet_Network.instance.Send_Message(info); } /* * */ } identity.transform.Rotate(new Vector3(0, info.a * Time.deltaTime, 0)); Quaternion tRot = new Quaternion(); tRot.eulerAngles = new Vector3((info.m > 0) ? 5 : 0, transform.eulerAngles.y, -info.a); if (info.m > 0 || groundDist > distanceOffset) // Restore the axises { identity.transform.rotation = Quaternion.Slerp(transform.rotation, tRot, 0.05f); /* * THIS IS FOR ANIMATION * */ fanTop.Rotate(fanTopRotateAxis, 720 * Time.deltaTime); /* * */ } /// Move the helicopter. Vector3 force = transform.forward * info.m + Vector3.up * info.b; transform.position = identity.rbody.rBody.position + force * Time.deltaTime; /// SAMPLE HELICOPTER CODE IS ENDED /// /// Call this end of the update loop to update the passengers on vehicle. ControlPassengers(); }
void Update() { isGrounded = HeightRaycaster.isGrounded(transform.position); /// We send a ray towards from our position but y += 1.25f, if it hits something walkable, we cannot move. /// This is the slope fix. canMove = !Physics.Raycast(transform.position + Vector3.up * 1.25f, transform.forward, 2f, HeightRaycaster.ground); if (isDead) { return; // Dead players cannot use inputs. Free camera must be activated } if (currentVehicle != null && !freeOnSeat) { //We are on a vehicle, also we are not free on the current seat. return; } if (!isLocalPlayer) { /* For other players, aiming must be smooth. * Smooth Aim Sync * */ if (syncedAim != null) { aim.y = Mathf.Lerp(aim.y, syncedAim.y, 0.1f); } return; } if (InputBlocker.isBlocked()) { /// Input blocked because of UI InputField horizontal = 0; vertical = 0; Sync(); // Sync anyway. return; } if (currentVehicle == null) { // Even we are on a free set, we cannot move horizontal = Mathf.Lerp(horizontal, Input.GetAxis("Horizontal"), Time.deltaTime * 12); vertical = Mathf.Lerp(vertical, Input.GetAxis("Vertical"), Time.deltaTime * 12); if (Input.GetButtonDown("Jump") && nextJump < Time.time && (isGrounded || identity.rbody.rBody.velocity.magnitude < 0.1f) && canMove) {/// Simple jumping nextJump = Time.time + 1; identity.rbody.rBody.AddForce(transform.up * 5 + transform.forward * vertical * 2 * (identity.animator.animator.GetBool("Sprint") ? 2 : 1) + transform.right * horizontal * 1, ForceMode.VelocityChange); identity.rbody.nextUpdate = Time.time + 0.02f; } /* * Local move input. It will be synced, but not in every frame so we are doing this for local client. * */ identity.animator.animator.SetFloat("H", horizontal); identity.animator.animator.SetFloat("V", vertical); bool isMoving = (Mathf.Abs(horizontal) > 0.1f || Mathf.Abs(vertical) > 0.1f); identity.animator.animator.SetBool("M", isMoving); /* * */ // Spriting via Shift key. if (Input.GetKeyDown(KeyCode.LeftShift) && vertical > 0.1f) { identity.animator.SetBool("Sprint", true); } else if (Input.GetKeyUp(KeyCode.LeftShift) || vertical <= 0.1f) { // If we done with shift key, disable the sprint. if (identity.animator.animator.GetBool("Sprint")) { identity.animator.SetBool("Sprint", false); identity.animator.SetTrigger("ChangeItem"); // We must call this to leave the sprinting state on animator. } } } // Rotating our player will rotate the camera because camera is child of our transform. transform.Rotate(new Vector3(0, Input.GetAxis("Mouse X"), 0) * Time.deltaTime * sensivityX); // Set player aim by Mouse Y aim.y = Mathf.Clamp(aim.y - Input.GetAxis("Mouse Y") * sensivityY, -50, 45); if (MouseOrbitImproved.instance.target == null) { /* * Set the camera position by aim. * */ Camera.main.transform.localEulerAngles = new Vector3(aim.y, Camera.main.transform.localEulerAngles.y, Camera.main.transform.localEulerAngles.z); Vector3 cameraPos = Camera.main.transform.position; cameraPos.y = Camera.main.transform.parent.position.y + aim.y / 80; Camera.main.transform.position = cameraPos; } /* * Shooting request. * */ if (Input.GetButtonDown("Fire1")) { isShooting = true; } else if (Input.GetButtonUp("Fire1")) { isShooting = false; } if (isShooting && !identity.animator.animator.GetBool("Sprint") && !SNet_Manager.instance.panel_Lobby.activeSelf) { if (inventory.inv.ci == null) { return; } if (inventory.inv.ci.fireable && (inventory.inv.ci.ammo > 0 || !inventory.inv.ci.countable) && nextFire < Time.time) { nextFire = Time.time + inventory.inv.ci.fireRate; SNet_Network.instance.Send_Message(new PlayerInventory.St()); } } Sync(); // Always sync. }
public void Update() { if (Client.Instance != null) { if (vehicle.p.Count == 0 || vehicle.p[0] == 0) { // If there is no driver, this should stop. info.m = Mathf.Clamp(info.m - Time.deltaTime * 100, 0, maxMotorTorque); info.s = 0; info.b = 100; } } if (isLocalVehicle) { if (InputBlocker.isBlocked()) { // If we are on a UI Input. info.m = 0; info.s = 0; info.b = 100; } else { /* * USE IT FREELY * */ info.m = maxMotorTorque * Input.GetAxis("Vertical"); info.a = maxSteeringAngle * Input.GetAxis("Horizontal"); info.b = Mathf.Abs(Input.GetAxis("Jump")); if (info.b > 0.001) { info.b = maxMotorTorque; info.m = 0; } else { info.b = 0; } } if (sync < Time.time) { sync = Time.time + 0.1f; SNet_Network.instance.Send_Message(info); } } foreach (Dot_Truck truck_Info in truck_Infos) { if (truck_Info.steering) { float steer = ((truck_Info.reverseTurn) ? -1 : 1) * info.a; if (truck_Info.leftWheel != null) { truck_Info.leftWheel.steerAngle = steer; } if (truck_Info.rightWheel != null) { truck_Info.rightWheel.steerAngle = steer; } } if (truck_Info.motor) { if (truck_Info.leftWheel != null) { truck_Info.leftWheel.motorTorque = info.m; } if (truck_Info.rightWheel != null) { truck_Info.rightWheel.motorTorque = info.m; } } if (truck_Info.leftWheel != null) { truck_Info.leftWheel.brakeTorque = info.b; } if (truck_Info.rightWheel != null) { truck_Info.rightWheel.brakeTorque = info.b; } VisualizeWheel(truck_Info); } /// Steer the steering wheel if its not null if (steeringMesh != null) { steeringMesh.Steer(info.a); } ControlPassengers(); }