Caméra à la World of Warcraft
Pour la gestion de la caméra dans une vue à la 3ème personne, Unity est fournit avec un script sympa mais limité nommé SmoothFollow. Le problème est que la caméra ne se replace pas si un obstacle est situé entre le personnage et elle.
En cherchant un peu sur le forum officiel, j’ai trouvé une classe créée par omoymenya et qui permet d’avoir une caméra à la WoW avec gestion de la souris, idéale pour tout MMO ou jeu d’aventure.
Pour s’en servir, créez un script C# nommé WowCamera.cs avec le code ci-dessous, placez le script sur la caméra et dans la variable Target, placez votre personnage en drag and drop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | using UnityEngine; using System.Collections; public class WowCamera : MonoBehaviour { public Transform target; public float targetHeight = 1.7f; public float distance = 5.0f; public float offsetFromWall = 0.1f; public float maxDistance = 20; public float minDistance = .6f; public float xSpeed = 200.0f; public float ySpeed = 200.0f; public float targetSpeed = 5.0f; public int yMinLimit = -80; public int yMaxLimit = 80; public int zoomRate = 40; public float rotationDampening = 3.0f; public float zoomDampening = 5.0f; public LayerMask collisionLayers = -1; private float xDeg = 0.0f; private float yDeg = 0.0f; private float currentDistance; private float desiredDistance; private float correctedDistance; void Start () { Vector3 angles = transform.eulerAngles; xDeg = angles.x; yDeg = angles.y; currentDistance = distance; desiredDistance = distance; correctedDistance = distance; // Make the rigid body not change rotation if (rigidbody) rigidbody.freezeRotation = true; } void Update() { //Move the Player with left & right button press together if(Input.GetMouseButton(1)&&Input.GetMouseButton(0)) { float targetRotationAngle = target.eulerAngles.y; float currentRotationAngle = transform.eulerAngles.y; xDeg = Mathf.LerpAngle (currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime); target.transform.Rotate(0,Input.GetAxis ("Mouse X") * xSpeed * 0.02f,0); xDeg += Input.GetAxis ("Mouse X") * targetSpeed * 0.02f; target.transform.Translate(Vector3.forward * targetSpeed * Time.deltaTime); } } /** * Camera logic on LateUpdate to only update after all character movement logic has been handled. */ void LateUpdate () { Vector3 vTargetOffset; // Don't do anything if target is not defined if (!target) return; // If either mouse buttons are down, let the mouse govern camera position if (Input.GetMouseButton(0)) { xDeg += Input.GetAxis ("Mouse X") * xSpeed * 0.02f; yDeg -= Input.GetAxis ("Mouse Y") * ySpeed * 0.02f; } //Reset the camera angle and Rotate the Target Around the World! else if (Input.GetMouseButton(1)) { float targetRotationAngle = target.eulerAngles.y; float currentRotationAngle = transform.eulerAngles.y; xDeg = Mathf.LerpAngle (currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime); target.transform.Rotate(0,Input.GetAxis ("Mouse X") * xSpeed * 0.02f,0); xDeg += Input.GetAxis ("Mouse X") * xSpeed * 0.02f; } // otherwise, ease behind the target if any of the directional keys are pressed else if (Input.GetAxis("Vertical") != 0 || Input.GetAxis("Horizontal") != 0) { float targetRotationAngle = target.eulerAngles.y; float currentRotationAngle = transform.eulerAngles.y; xDeg = Mathf.LerpAngle (currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime); } yDeg = ClampAngle (yDeg, yMinLimit, yMaxLimit); // set camera rotation Quaternion rotation = Quaternion.Euler (yDeg, xDeg, 0); // calculate the desired distance desiredDistance -= Input.GetAxis ("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs (desiredDistance); desiredDistance = Mathf.Clamp (desiredDistance, minDistance, maxDistance); correctedDistance = desiredDistance; // calculate desired camera position vTargetOffset = new Vector3 (0, -targetHeight, 0); Vector3 position = target.position - (rotation * Vector3.forward * desiredDistance + vTargetOffset); // check for collision using the true target's desired registration point as set by user using height RaycastHit collisionHit; Vector3 trueTargetPosition = new Vector3 (target.position.x, target.position.y + targetHeight, target.position.z); // if there was a collision, correct the camera position and calculate the corrected distance bool isCorrected = false; if (Physics.Linecast (trueTargetPosition, position, out collisionHit, collisionLayers.value)) { // calculate the distance from the original estimated position to the collision location, // subtracting out a safety "offset" distance from the object we hit. The offset will help // keep the camera from being right on top of the surface we hit, which usually shows up as // the surface geometry getting partially clipped by the camera's front clipping plane. correctedDistance = Vector3.Distance (trueTargetPosition, collisionHit.point) - offsetFromWall; isCorrected = true; } // For smoothing, lerp distance only if either distance wasn't corrected, or correctedDistance is more than currentDistance currentDistance = !isCorrected || correctedDistance > currentDistance ? Mathf.Lerp (currentDistance, correctedDistance, Time.deltaTime * zoomDampening) : correctedDistance; // keep within legal limits currentDistance = Mathf.Clamp (currentDistance, minDistance, maxDistance); // recalculate position based on the new currentDistance position = target.position - (rotation * Vector3.forward * currentDistance + vTargetOffset); transform.rotation = rotation; transform.position = position; } private static float ClampAngle (float angle, float min, float max) { if (angle < -360) angle += 360; if (angle > 360) angle -= 360; return Mathf.Clamp (angle, min, max); } } |
Salut Kris64. Tous d’abord bravo pour ce site et l’initiative, c’est une tres bonne idée.
Je voulais juste dire que les guillemets foirent lors de la retranscription du code.
Sinon encore merci pour ce site, et bonne continuation (d’après ton accent et ton pseudo on est surement voisin)
Matt
J’ai mis une nouvelle box pour les scripts, tu peux réessayer ça marche 🙂
Ou sont les variables?
Chu un noob ^^
Toues les lignes commençant par public et private
merci mais peux-tu (si je peux me permettre de te tutoyer ^^)faire un tuto pour créer un perso qui bouge a la 3eme personne , le faire attaquer une cible etc.
sauf si tu n’as pas le temps ^^(je pense ne pas etre le seul a vouloir)
Merci beaucoup
Nous sommes en train de faire un MMORPG
Sa va etre dur ^^
Je suis completement perdu a la wowcamera et pour le MMO
plzz aidez moi
je craque ^^
tu glisses le script sur ton perso et à la variable target, tu assigne ton perso en drag and drop, c’est tout:)
bien sympa ce petit script 🙂 par contre apres faut expliquer au character controller qu’on est plus sur les axes de base mais les recalculer en fonction de la position de la cam
Merci bien mais pourrais-tu poster un tuto pour créer un début de MMORPG s’il te plait car je pense ne pas etre le seul a vouloir. ^^
merci
Bonjour, moi aussi je n’ai pas bien compris comment créer un MMORPG a la 3eme personne (camera: vue a la WOW)et je trouve l’idée de « billon » très convaincante et je suggérais de faire un petit tuto pour faire un début de MMORPG.
Merci
Si c’est un tuto expliquant les échanges client serveur, c’est non, trop compliqué pour l’instant, si c’est un perso animé à la 3ème personne, ça va venir 😉
Merci beaucoup car cela servira a plus d’un et bonne continuation ^^
PS: chui a l’école ^^
[hs] « billon » je le connais celui la… [/hs]
Salut
C’est un excellent script 🙂
j’ai besoin d’un peut d’aide pour me mettre au fil … la manipulation correspond exactement a ce que je veut mais avec la souris et celle que je veut et avec le iphone input. Comme ici =>http://www.youtube.com/watch?v=Tr14pF4MJqA exactement a 6min 15 sec …
Y’a t’il des simple modification a faite comme input.GetAxis(« mouseX ») par une entré iphone 🙁
merci
Hello merci pour ton script ! je suis en trein de suivre ton tuto sur la gestion des personnages !
Mais quand je copie colle le script, sur MainCamera je n’est aucune variable ! j’ai que WowCamera (script) !
Je m’apperçoi aussi que dans la console ENORMEMENt d’erreur comme un oublie de ; a un ligne alros que il y en a un !
Je suis perdu !
HELP
Hey voila un pack que jai fait ,
Camera
mouvement
nager
effet camera en dessous de leau
qui ressemble beaucoup a wow
demo disponible
seulement 5$
PLUS DINFO ICI :
http://forum.unity3d.com/threads/121494-Released-WoW-Character-Movement-Camera
bonjour
c’est quoi drag and drop
sry, my french is a little bit rusty… just wanted to say: great script! this is a nice base to start from ! merci beaucoup, man!
Moi sa m’affiche ça :
NullReferenceException
ThirdPersonController.UpdateSmoothedMovementDirection () (at Assets/Standard Assets/Character Controllers/Sources/Scripts/ThirdPersonController.js:129)
ThirdPersonController.Update () (at Assets/Standard Assets/Character Controllers/Sources/Scripts/ThirdPersonController.js:302)
Bonjour,
J’ai fais la copie du wowcamera.cs mais lors du drag and drop sur la main camera j’ai l’erreur suivante : « can’t add script behavior wowcamera. The script file name does not match the name of the class defined in the script ».
Mon commentaire doit paraitre bête pour les avertis mais si vous aviez la solution je vous remercie d’avance.
super! merci beaucoup 🙂
Salut salut! je dè-terre ce petit topic car ton script me plais et me sert .. cependant j’ai juste un soucis pour le moment. RMB ne me permet de pivoter qu’uniquement sur X alors que Y est lui débloquer pour LMB. comment m’y prendre PLZZ:)