protected override void OnUpdate() { int i = 0; Entities.WithAll <Transform, CameraTarget>().ForEach((Entity targetEntity, Transform targetTransform) => { //Only want to do first entity with CaemraTarget if (i > 0) { return; } i++; //Get Target Entity from priority var dt = Time.deltaTime; var cameraTransform = Camera.main.transform; var cameraTargetData = EntityManager.GetSharedComponentData <CameraTarget>(targetEntity).data; //Get Camera Socekt targetTransform = targetTransform.Find("CameraSocket"); //Rotate { inputX += GInput.GetAxisRaw(GAxis.RIGHTHORIZONTAL) * dt * cameraTargetData.rotationSpeed; inputY += GInput.GetAxisRaw(GAxis.RIGHTVERTICAL) * dt * cameraTargetData.rotationSpeed; inputY = Mathf.Clamp(inputY, cameraTargetData.minRotatonY, cameraTargetData.maxRotationY); cameraTransform.eulerAngles = new Vector3(inputY, inputX); } //Zoom { if (!currentDistances.ContainsKey(targetEntity.Index)) { currentDistances.Add(targetEntity.Index, cameraTargetData.defaultDistance); } // currentDistances[targetEntity.Index] += Input.GetAxis("Mouse ScrollWheel") * cameraTargetData.zoomSpeed * dt; currentDistances[targetEntity.Index] = Mathf.Clamp(currentDistances[targetEntity.Index], cameraTargetData.minDistance, cameraTargetData.maxDistance); } //Move To Default Position cameraTransform.position = (targetTransform.position) - cameraTransform.forward * currentDistances[targetEntity.Index]; //Collision { if (!cameraTargetData.doCollision) { return; } ClipPlanePoints nearClipPlanePoints = GetCameraClipPlanePoints(); DetectCollision(ref nearClipPlanePoints, targetTransform); //Move To Position based on collision cameraTransform.position = (targetTransform.position) - cameraTransform.forward * ((nearClipPlanePoints.didCollide) ? nearClipPlanePoints.hitDistance : currentDistances[targetEntity.Index]); } }); }
private void UpdateAsPlayer(Entity entity, ref ActorInput actorInput) { if (!EntityManager.HasComponent(entity, typeof(ActorPlayer))) { return; } //Get Player Input { //AXIS actorInput.movement.x = GInput.GetAxisRaw(GAxis.LEFTHORIZONTAL); actorInput.movement.z = GInput.GetAxisRaw(GAxis.LEFTVERTICAL); //Reset actorInput.actionToDo = 0; //BUTTONS if (GInput.GetButtonDown(GButton.RIGHT)) { actorInput.actionToDo = 1; } if (GInput.GetButtonDown(GButton.BOTTOM)) { actorInput.actionToDo = 2; } if (GInput.GetButtonDown(GButton.LEFT)) { actorInput.actionToDo = 3; } if (GInput.GetButtonDown(GButton.L3)) { actorInput.crouch = (byte)(actorInput.crouch == 1 ? 0 : 1); } actorInput.sprint = (byte)(GInput.GetButton(GButton.TOP) ? 1 : 0); actorInput.strafe = (byte)(GInput.GetButton(GButton.L2) ? 1 : 0); } //Convert Actor Movement to Camera { var cameraForward = (Camera.main.transform.TransformDirection(Vector3.forward).normalized); cameraForward.y = 0; var camreaRight = new Vector3(cameraForward.z, 0, -cameraForward.x); actorInput.movement = actorInput.movement.x * camreaRight + actorInput.movement.z * cameraForward; actorInput.movement.Normalize(); } }