Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // gives us vector to direction of target
- Vector3 inverseVect = Input.mousePosition;
- // calculate angle by which you have to rotate
- // Note -: This angle is calculated every Frame of FixedUpdate
- float rotationAngle = Mathf.Atan2(inverseVect.x, inverseVect.z) * Mathf.Rad2Deg;
- // Now calculate rotationVelocity to be applied every frame
- Vector3 rotationVelocity = (Vector3.up * rotationAngle) * rotationSpeed * Time.deltaTime;
- // Calaculate his delta velocity i.e required - current
- Vector3 deltavel = (rotationVelocity - rb.angularVelocity);
- // Apply the force to rotate
- rb.AddTorque(deltavel, ForceMode.Impulse);
- //поворот физикой за мышкой в пустом пространстве
- //(если смотреть на мышь)get right position of mouse if the camera is Perspective
- //подробнее goo.gl/Fc6icj
- Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition);
- float midPoint = (transform.position - Camera.main.transform.position).magnitude * 0.5f;
- Vector3 targetPosition = (mouseRay.origin + mouseRay.direction * midPoint);
- //get position of target object(если смотреть на объект)
- //Vector3 targetPosition = targetObject.transform.position;
- //gives us vector to direction of target
- Vector3 inverseVect = transform.InverseTransformPoint(targetPosition);
- //calculate angle by which you have to rotate.Note: this angle is calculated every Frame of FixedUpdate
- float rotationAngle = Mathf.Atan2(inverseVect.x, inverseVect.z) * Mathf.Rad2Deg;
- //Now calculate rotationVelocity to be applied every frame
- Vector3 rotationVelocity = (Vector3.up * rotationAngle) * rotationSpeed * Time.deltaTime;
- //Calaculate his delta velocity i.e required - current
- Vector3 deltavel = (rotationVelocity - rb.angularVelocity);
- //Apply the force to rotate
- rb.AddTorque(deltavel, ForceMode.Impulse);
- //служ. инфо
- Debug.DrawLine(transform.position, targetPosition, Color.magenta);
- print("targetPosition: " + targetPosition +
- " targetObject.transform.position: "+ targetObject.transform.position +
- " transform.position: "+ transform.position);
Add Comment
Please, Sign In to add comment