Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

WOW Camera Movement

Discussion in 'Scripting' started by matrix211v1, Jan 20, 2009.

  1. Tilluss

    Tilluss

    Joined:
    Mar 5, 2010
    Posts:
    34
    In what way are they not correct? Are you sure your just not selecting the right options to set it up to your needs?
     
  2. nevaran

    nevaran

    Joined:
    May 21, 2010
    Posts:
    247
    In what way are they not correct? Are you sure your just not selecting the right options to set it up to your needs?[/quote]

    theres a bit diffrence from the wow camera and the scripts they are, so i dont think its so "same" :wink:
     
  3. Ensane

    Ensane

    Joined:
    Jul 13, 2010
    Posts:
    221
    hmm Im trying to convert the movement script over to C# and when it gets to

    Code (csharp):
    1.  
    2. if(Input.GetMouseButton(1)  Input.GetAxis("Horizontal")  Input.GetAxis("Vertical")) {
    3. moveDirection *= .7;
    4. }
    5.  
    and i get this error

    What do i need to do to correct it?
     
  4. zergmouse

    zergmouse

    Joined:
    Jun 30, 2010
    Posts:
    216
    you may need to explicitly cast each one? I could be wrong, I am only a novice at C#

    http://jsnelders.wordpress.com/2009...-applied-to-operands-of-type-object-and-bool/
     
  5. nevaran

    nevaran

    Joined:
    May 21, 2010
    Posts:
    247
    if u make it like that ofcourse it wont work, it cant make on move mouse and press mouse key(dont bother asking why) try doing in on js --- C# have some limited coding for unity, like spawning object but no effects or light and stuff like that, js may slow ur pc but it wont do such things :)
     
  6. jokeman248

    jokeman248

    Joined:
    Jul 31, 2010
    Posts:
    1
    all of the scripts have been really helpfull because im really new to unity (like i just got like a week ago) except none of them had one major thing wow has in its charecter control. in wow, if a mountain is too steep, its you cant get up it. with all of the carecter controls you gave the charecter is slowed down when it gets steep, but it can still climb up a pretty much streaight up mountain. I was thinking about just putting an invisible barrier to block it from going up the mountain, but is there a better way? and if not, how do you make a barier invisible?

    PLEASE REPLY!!! :eek: :eek: :eek:
     
  7. dart

    dart

    Joined:
    Jan 9, 2010
    Posts:
    211
    Try this:
    Code (csharp):
    1.  
    2. if (Input.GetMouseButton(1)  Input.GetAxis("Horizontal") != 0  Input.GetAxis("Vertical") != 0)
    3. {
    4.    moveDirection *= .7F;
    5. }
    6.  
    Also, insert a F after .7 otherwise it implies you´re passing a double to moveDirection, which, as a Vector3 object, only accpets floats.

    jokeman, to make an invisible barrier, go to menu GameObject, create an Empty GameObject, select this new empty GO created, go to Components menu, Physics, Box Collider (or the collider you want to insert). Then set the size and position of the collider the way you want.
     
  8. Ensane

    Ensane

    Joined:
    Jul 13, 2010
    Posts:
    221
    Thanks for the help that worked...

    Altho my next issue is im trying to get these scripts to work with badumna network suite. I have a AnimationHandler.cs that handles sending the animations from my character to the server...

    But im having an issue with the javascript not seeing the c#... i can get the c# to see the javascript by moving the script out of the folder but not the other way...

    Code (csharp):
    1. function Start()
    2. {
    3.        var animHandler:AnimationHandler = GetComponent(AnimationHandler);
    4. }
    and i get this error
    ?

    then if i switch it too

    Code (csharp):
    1. var animHandler = GetComponent(AnimationHandler);
    i get
    Code (csharp):
    1. Assets/Plugins/Controller/PlayerController.js(28,39): BCE0005: Unknown identifier: 'AnimationHandler'.
    I have tried moving the scripts out of the same directory... Im sure im not the only one who has wanted to use custom movement scripts with Badumna.
     
  9. Hogfather

    Hogfather

    Joined:
    Aug 17, 2010
    Posts:
    44
    What you're asking is totaly independed from the camera which is the topic of this discussion, so it's only natural that it doesn't work as expected.

    I'm really new at this too, but if i understood correctly what you want, you need to go to your character controler, and change the slope limit.
     

    Attached Files:

  10. Wowshop15

    Wowshop15

    Joined:
    Aug 27, 2010
    Posts:
    28
    Thank-you for this script it is VERY helpful.However I cant seem to adjust the speed that the character turns.If anybody had a similar problem and fixed it any help would be appreciated.
     
  11. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    I'm not sure, but did you try adjusting RotateSpeed at the top of the script?
     
  12. Wowshop15

    Wowshop15

    Joined:
    Aug 27, 2010
    Posts:
    28
    Yeah I did and it has no effect.I am using the 1st script posted here on my character.(10 pages back lol)
     
  13. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    I posted the one on page seven and limit my liability to that script (just kidding! i assume no liability for anything, including my physical actions, past present or future)...


    anyway... give it a shot.
     
  14. Wowshop15

    Wowshop15

    Joined:
    Aug 27, 2010
    Posts:
    28
    Ok That does work better it lets me control the rotate speed now but it says that there is no Axis Mouse X setup.Im not certain on how to fix this but Im sure its somewhere in the Edit>Project Settings>Input menu somewhere if maybe you could point me in the right direction.As you can probably tell I'm a new Unity and scripting user(I'm only 13 so what do your expect?lol).
     
  15. Wowshop15

    Wowshop15

    Joined:
    Aug 27, 2010
    Posts:
    28
    Better Yet I got it working.Thanks for posting the script very nicly put together.
     
  16. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    Keep at it, kid, you got spirit.

    and thanks for the compliment!
     
  17. Koropokuruh

    Koropokuruh

    Joined:
    Mar 23, 2010
    Posts:
    18
    hey there.

    I've been working on my own 3rd person camera script (more like a gta camera style than wow).
    I'm using linecast just like you guys to make the camera collide and not go through objects, but instead it just hits terrain colliders.

    anyone having the same problem and knows how to fix it?

    Code (csharp):
    1. var hit : RaycastHit;
    2.       if (Physics.Linecast (transform.position, target.position, hit)){
    3.          distance -= hit.distance + 0.25; //camera comes in front of the object
    4.          ponto = hit.point; //the collision point
    5.          
    6.       //true if the distance from the camera and the hitpoint is greater than 0.25
    7.       }else if(Vector3.Distance(transform.position, ponto)  > 0.25){
    8.          //smooth movement back to 5m
    9.          distance += 1.5 * Time.deltaTime;
    10.          //keeps the distance at a max of 5m.
    11.          distance = Mathf.Clamp(distance, 0.0, 5.0);
    12.       }
     
  18. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    You need to have mesh colliders as components of your object in order for that to work. If there's no collider, the Raycast won't pay attention to it.
     
  19. Koropokuruh

    Koropokuruh

    Joined:
    Mar 23, 2010
    Posts:
    18
    but there's a house with mesh collider and trees withs capsule colliders.

    can't figure out what is happening.
     
  20. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    You mean it only works when it hits the terrain and not other object?

    You are remembering to move the camera again after adjusting the distance, yes?
     
  21. Koropokuruh

    Koropokuruh

    Joined:
    Mar 23, 2010
    Posts:
    18
    yeah, it just hits the terrain.
    I'll show my script here.

    I've translated the comments to english

    Code (csharp):
    1. //orbital camera script
    2. #pragma strict
    3.  
    4. var sensibilidade = 0.02; //mouse sensitivity
    5. var alvo : Transform; //camera target
    6.  
    7. var distancia : float = 5.0; //distancia da camera
    8.  
    9. private var x : float = 0.0;
    10. private var y : float = 0.0;
    11. private var minimoX : float = -75; //vertical limit x-axis
    12. private var maximoX : float = 75; //vertical limit x-axis
    13. private var esse : Transform;
    14. private var ponto : Vector3;
    15.  
    16. @script AddComponentMenu("Camera Config/Camera Orbital")
    17.  
    18. function Start(){
    19.     var anguloX = transform.eulerAngles;
    20.     x = anguloX.y;
    21.     y = anguloX.x;
    22.     esse = Camera.main.transform;
    23. }
    24.  
    25. function LateUpdate () {
    26.     if(alvo){
    27.         y += Input.GetAxis("Mouse X") * sensibilidade * 250;
    28.         x -= Input.GetAxis("Mouse Y") * sensibilidade * 120;
    29.         x = LimiteVertical(x, minimoX, maximoX);
    30.         y = LimiteVertical(y, -360, 360);
    31.        
    32.         var rotacao = Quaternion.Euler(x,y,0); //camera rotation based on mouse movement
    33.         //updates camera position to orbit the target
    34.         var posicao = rotacao * Vector3(0.0, 0.0, - distancia) + alvo.position;
    35.        
    36.         //applies the rotation and position values
    37.         esse.rotation = rotacao;
    38.         esse.position = posicao;
    39.  
    40.         var hit : RaycastHit; // raycasthit
    41.         //raycast between the camera and the target
    42.         if (Physics.Linecast (esse.position, alvo.position, hit)){
    43.             distancia -= hit.distance + 0.25; //makes the camera come forth
    44.             ponto = hit.point; //last position of the raycast hit.
    45.             Debug.Log(hit.transform.name);
    46.         //makes the camera move back when the distance from the last hit and camera are greater than 0.25
    47.         }else if(Vector3.Distance(esse.position, ponto)  > 0.25){
    48.             //smooth movement back to 5m
    49.             distancia += 1.5 * Time.deltaTime;
    50.             //keeps the distance at a max of 5m
    51.             distancia = Mathf.Clamp(distancia, 0.0, 5.0);
    52.         }
    53.        
    54.     }  
    55.         var direction = transform.TransformDirection (Vector3.forward) * 5;
    56.         Debug.DrawRay (transform.position, direction, Color.red);
    57. }
    58.  
    59.  
    60. //vertical angle limitation function
    61. function LimiteVertical (angulo : float, minimo : float, maximo : float){
    62.     if (angulo < -360) {
    63.         angulo += 360;
    64.     }
    65.     if (angulo > 360){
    66.         angulo -= 360;
    67.     }
    68.     return Mathf.Clamp(angulo, minimo, maximo);
    69. }
     
  22. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    I really don't know.

    If it is working on the terrain correctly and the line

    Code (csharp):
    1.  
    2.          Debug.Log(hit.transform.name);
    3.  
    prints terrain, then I don't know why it wouldn't work on other objects, as long as there are colliders attached to the object. No collider, no hit. I would double check that you have colliders on the other objects. That's all I can think of.

    Maybe someone else will see what I don't.
     
  23. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    One other thing might be this: After checking for the raycast.hit and adjusting the distance, you should re-position the camera in the right place by putting in the line:

    Code (csharp):
    1.       //calculate new position:
    2.       var posicao = rotacao * Vector3(0.0, 0.0, - distancia) + alvo.position;
    3.      
    4.       //Put camera in front of object:
    5.       esse.position = posicao;
    after your raycast check, right at the end of LateUpdate.
     
  24. Koropokuruh

    Koropokuruh

    Joined:
    Mar 23, 2010
    Posts:
    18
    Code (csharp):
    1. var rotacao = Quaternion.Euler(x,y,0); //camera rotation based on mouse movement
    2.       //updates camera position to orbit the target
    3.       var posicao = rotacao * Vector3(0.0, 0.0, - distancia) + alvo.position;
    4.        
    5.       //applies the rotation and position values
    6.       esse.rotation = rotacao;
    7.       esse.position = posicao;
    8.  
    moved this part of the code to the end, and no difference.
    what I found out with the debug is that sometimes it hits the colliders sometimes it doesn't, but it always hits the terrain collider.
    it's like it hits only the corners of the colliders.

    i've created a webplayer and uploaded to a webhost.
    here's the link, it's easier to show than to explain what is happening.

    http://rasguti.sites.uol.com.br/Odnum.html
     
  25. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    You don't want to MOVE the code, you want to COPY the position part so it:

    1) positions the camera
    2) checks the Raycast
    3) re-positions the camera if hit.

    I thought of one more thing and this might be the answer! The Raycast needs to come FROM the target TO the camera! Remember, mesh normals only face OUT, so if you go from camera to the object, you may be looking through walls.

    I might be wrong, but try changing the line:
    Code (csharp):
    1.  
    2. if (Physics.Linecast (esse.position, alvo.position, hit))
    to this:

    Code (csharp):
    1. if (Physics.Linecast (alvo.position, esse.position, hit))
     
  26. Koropokuruh

    Koropokuruh

    Joined:
    Mar 23, 2010
    Posts:
    18
    it's now working!
    thanks a lot!

    Code (csharp):
    1.  
    2. if (Physics.Linecast (alvo.position, esse.position, hit))
    this is the answer, just need now to tweak a little cause the camera got crazy in collisions, but at least it collides. XD

    thanks a lot.

    ------edit-------------------

    here is the camera script, if anyone wants.
    to make it like wow, just put an if to make the LMB the trigger to rotate camera.
    Code (csharp):
    1. //orbital camera script
    2. #pragma strict
    3.  
    4. var sens = 0.02; //mouse sensitivity
    5. var target : Transform; //camera target
    6.  
    7. var distance : float = 5.0; //distance of the camera
    8.  
    9. private var x : float = 0.0;
    10. private var y : float = 0.0;
    11. private var minX : float = -75; //vertical limit x-axis
    12. private var maxX : float = 75; //vertical limit x-axis
    13. private var camera : Transform;
    14. private var point : Vector3;
    15.  
    16. @script AddComponentMenu("Camera Config/Orbital Camera")
    17.  
    18. function Start(){
    19.    var angleX = transform.eulerAngles;
    20.    x = angleX.y;
    21.     y = angleX.x;
    22.    camera = Camera.main.transform;
    23. }
    24.  
    25. function LateUpdate () {
    26.    if(target){
    27.       y += Input.GetAxis("Mouse X") * sens * 250;
    28.       x -= Input.GetAxis("Mouse Y") * sens* 120;
    29.       x = VerticalLimit(x, minX, maxX);
    30.       y = VerticalLimit(y, -360, 360);
    31.        
    32.       var rotation = Quaternion.Euler(x,y,0); //camera rotation based on mouse movement
    33.       //updates camera position to orbit the target
    34.       var position = rotation * Vector3(0.0, 0.0, - distance) + target.position;
    35.        
    36.       //applies the rotation and position values
    37.       camera.rotation = rotation;
    38.       camera.position = position;
    39.  
    40.       var hit : RaycastHit; // raycasthit
    41.       //raycast between the camera and the target
    42.       if (Physics.Linecast (target.position, camera.position, hit)){
    43.          distance = hit.distance - 0.3; //makes the camera come forth
    44.          point = hit.point; //last position of the raycast hit.
    45.          Debug.Log(hit.transform.name);
    46.       //makes the camera move back when the distance from the last hit and camera are greater than 0.3
    47.       }else if(Vector3.Distance(camera.position, point)  > 0.3){
    48.          //smooth movement back to 5m
    49.          distance += 1.5 * Time.deltaTime;
    50.          //keeps the distance at a max of 5m
    51.          distance = Mathf.Clamp(distance, 0.0, 5.0);
    52.       }
    53.        
    54.    }    
    55.      
    56. }
    57.  
    58.  
    59. //vertical angle limitation function
    60. function VerticalLimit (angle : float, minimum : float, maximum : float){
    61.    if (angle < -360) {
    62.       angle += 360;
    63.    }
    64.    if (angle > 360){
    65.       angle -= 360;
    66.    }
    67.    return Mathf.Clamp(angle, minimum, maximum);
    68. }
     
  27. JJ123

    JJ123

    Joined:
    Aug 10, 2010
    Posts:
    7
    So with this camera setup how would I make it so when I perform click and drag actions over my GUI objects it doesn't move the camera around?

    For example...I have a pop up menu in my wow-type game and one of the pop up menus has a scroll list of controls that you have to click and drag in order to see other options in the scroll menu. But right now when I'm scrolling through the menu it also moves the camera around. I need it so the camera stays still while im navagating my menus on the GUI layer.

    Any help is greatly appreciated. Thanks!
     
  28. Vimalakirti

    Vimalakirti

    Joined:
    Oct 12, 2009
    Posts:
    755
    It is a complete pain in the buttocks to deal with this. You need something like this code snippet (untested). This would go in your camera movement script, not the GUI script.

    Code (csharp):
    1.  
    2. // Check to see if LMB is clicked to Select
    3.                 if (Input.GetMouseButton(0))
    4. {          
    5.     // First, see if we clicked inside the GUI Box:
    6.     var GUILeft = (10);
    7.     var GUITop = (10);
    8.     var GUIRight = (90);
    9.     var GUIBottom = (120);
    10.     var mouseX = Input.mousePosition.x;
    11.     var mouseY = Input.mousePosition.y;
    12.     if( mouseX > GUILeft  mouseX < GUIRight  mouseY > GUIBot  mouseY < GUITop)
    13.         {
    14.             // Don't move camera, you're inside the GUI Box
    15.         } else
    16.         {
    17.             // Now go ahead and move the camera as usual
    18.             // cause you're not in the GUI box
    19.             //   ... code here ...
    20.         }
    21. }
    22.  
    Obviously put the right numbers in the GUILeft, right, etc variables. They need to be the same as your GUI Box.
     
  29. wafflehead66

    wafflehead66

    Joined:
    Sep 22, 2010
    Posts:
    21
    Thanks Guys
     
  30. digoxy

    digoxy

    Joined:
    Dec 3, 2010
    Posts:
    6
    Hello Gang...

    @Vimalakirti, you had asked for the C# version of this script, I have yet to see the completed version. You seem to point back to your original java version right? I would reallyl like to get my hands on the C# if at all possible.

    @ALL,

    I had seen mentioned a few pages back in this thread a question about an error that is recieved on recieving a null ref on:

    Code (csharp):
    1.  
    2. transform.rotation = Quaternion.Euler(0,Camera.main.transform.eulerAngles.y,0)
    3.  
    I have tried several variations of this but cannot rid this error, anyting the right mouse button is pressed, it spits out the error. Could this be the version of unity perhaps? I have tried naming the cam, "Camera", "Main", "Main Camera", "MainCamera" without any progress. I have followed the instructions on this thread many times, re-implemented, deleted all the scripts added them back etc.

    The cam works great, LMB looks around while W is pressed, I would like to steer with the right though as It think this is how it was meant to work. Also, seems that pressing both mouse buttons together should move forward. And also, how about strafe?

    If anyone has this put together, or, can shed some light on getting this up and running fully with Unity3. please let me know. I would gladly post the scripts all together as I have them working except these few things.
     
  31. zweihander111

    zweihander111

    Joined:
    Nov 7, 2010
    Posts:
    20
    Thank you very much for the information, It really helps, non coder people!
     
  32. minevr

    minevr

    Joined:
    Mar 4, 2008
    Posts:
    1,018
    Thanks for share...good..
     
  33. tbx

    tbx

    Joined:
    Dec 8, 2010
    Posts:
    5
    I have error :( it says

    Code (csharp):
    1. Assets/Standard Assets/Character Controllers/Sources/Scripts/ThirdPersonCamera.js(37,7): BCE0004: Ambiguous reference 'camera': ThirdPersonCamera.camera, UnityEngine.Component.camera.
    2.  
     
  34. rhianu

    rhianu

    Joined:
    Feb 25, 2010
    Posts:
    93
    Hi guys, I'm having a little trouble with my own code, as well. I want to achieve something similar to WoW, but unlike what most people in this thread seem to be doing I don't want to do exactly the same thing that WoW does. That is, I want a camera that moves like WoW's camera, but I want character movement more like what you see in the FINAL FANTASY games (i.e. character always walks forward in the direction you press, rather than strafing, walking backwards, or turning in place).

    I've got the camera script working perfectly thanks to the help from this thread, and I previously had character movement script working perfectly as well. But the jump function kinda got screwed up when I upgraded to Unity 3.1 (I had been using 2.6 until just the other day). So now whenever the player jumps forward, rather than traveling in a smooth arc, the player character goes straight up and then straight forward, forming a 90-degree angle. I had it working fine back in unity 2.6, and I'm not sure why it doesn't work in 3.1.

    Here's my code:

    Player Character Movement:
    Code (csharp):
    1. var speed =7.0;
    2. var jumpHeight = 10.0;
    3. var gravity = 20.0;
    4. var cam : Transform;
    5. var sprint = 2;
    6. var playerMesh : Renderer;
    7. static var death : boolean;
    8.  
    9. private var moveDirection = Vector3.zero;
    10. private var grounded : boolean = false;
    11.  
    12. function Awake(){
    13.     cam = Camera.main.transform;
    14. }
    15.  
    16. function FixedUpdate() {
    17.    
    18.     var cameraDistance = cam.GetComponent(MouseOrbit).distance;
    19.     var yMinLimit = cam.GetComponent(MouseOrbit).yMinLimit;
    20.    
    21.     if (cameraDistance <= 0.1)
    22.         {
    23.             playerMesh.enabled = false;
    24.             yMinLimit = -90;
    25.         }
    26.        
    27.         else
    28.         {
    29.             playerMesh.enabled = true;
    30.             yMinLimit = -9;
    31.         }
    32.  
    33.     if (grounded) {
    34.         // We are grounded, so recalculate movedirection directly from axes
    35.         moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    36.        
    37.         //The following line makes it so that movement direction cannot be greater than 1 (so that diagonal movement is not faster than normal)
    38.         moveDirection.Normalize();
    39.        
    40.        
    41.         animation.CrossFade("idle");
    42.            
    43.     //Justin's script (kinda):
    44.         //If the player presses the sprint button, movement speed is faster than normal (movement direction variable * multiplied speed variable * multiplied by sprint variable)
    45.         if (Input.GetButton("Sprint"))
    46.             {
    47.             moveDirection = moveDirection * speed * sprint;
    48.             }
    49.        
    50.         //If the player does not press the sprint button, movement speed is normal (movement direction variable * multiplied by speed variable)
    51.         else
    52.             {
    53.             moveDirection *= speed;    
    54.             }
    55.        
    56.         //If the movement direction variable contains any values (which it only will if you've pressed something), then do the following:
    57.         if (!Mathf.Approximately(moveDirection.sqrMagnitude, 0) )
    58.             {
    59.             transform.rotation = Quaternion.identity;
    60.             transform.eulerAngles.y = cam.eulerAngles.y;
    61.             var pos = moveDirection;
    62.             moveDirection = transform.TransformDirection(moveDirection);
    63.             animation["run forward"].speed = 1;
    64.             animation.CrossFade("run forward");
    65.            
    66.             //get direction to point character
    67.             pos = transform.TransformPoint(pos);
    68.             var relativePos = pos - transform.position;
    69.             var rotation = Quaternion.LookRotation(relativePos);
    70.             transform.rotation = rotation;
    71.             }
    72.        
    73.         if (Input.GetButton ("Jump")) {
    74.             animation["run forward"].speed = 0.1;
    75.             animation.CrossFade("run forward");
    76.             moveDirection.y = jumpHeight;
    77.         }
    78.        
    79.         if (Input.GetButton ("Dance")) {
    80.             animation.CrossFade("dance");
    81.         }
    82.     }
    83.  
    84.     // Apply gravity
    85.     moveDirection.y -= gravity * Time.deltaTime;
    86.    
    87.     if (death)
    88.     {
    89.         moveDirection = Vector3.zero;
    90.         animation.CrossFade("idle", .01, PlayMode.StopAll);
    91.     }
    92.    
    93.     // Move the controller
    94.     var controller : CharacterController = GetComponent(CharacterController);
    95.     var flags = controller.Move(moveDirection * Time.deltaTime);
    96.     grounded = (flags  CollisionFlags.CollidedBelow) != 0;
    97.  
    98. }
    99.  
    100.  
    101.  
    102.  
    103. @script RequireComponent(CharacterController)
    Camera Movement:
    Code (csharp):
    1. var target : Transform;
    2.  
    3. var targetHeight = 2.0;
    4. var distance = 10.0;
    5.  
    6. var xSpeed = 250.0;
    7. var ySpeed = 120.0;
    8.  
    9. var yMinLimit = -9;
    10. var yMaxLimit = 80;
    11.  
    12. var rotationDampening = 3.0;
    13.  
    14. private var x = 0.0;
    15. private var y = 0.0;
    16.  
    17. function Start ()
    18. {
    19.     var angles = transform.eulerAngles;
    20.     x = angles.y;
    21.     y = angles.x;
    22.  
    23.     // Make the rigid body not change rotation
    24.     if (rigidbody)
    25.         rigidbody.freezeRotation = true;
    26. }
    27.  
    28.  
    29. function LateUpdate ()
    30. {
    31.  
    32.     Screen.lockCursor = false;
    33.  
    34.     if (pause.gamePaused == false)
    35.     {
    36.         if (target)
    37.         {
    38.             x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
    39.             y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
    40.  
    41.             //Enables zooming in and out with the mouse scroll wheel (with scrolling speed multiplied by 5):
    42.             distance -= Input.GetAxis("Mouse ScrollWheel") * 5;
    43.  
    44.             //Clamp the minimum and maximum distances that the camera can be zoomed in or out (distance variable, minimum 0, maximum 20):
    45.             distance = Mathf.Clamp(distance, 0, 20);
    46.  
    47.             //Sets a variable limitation on camera rotation along the Y axis
    48.             y = ClampAngle(y, yMinLimit, yMaxLimit);
    49.  
    50.             //When the camera is zoomed all the way in (first-person perspective) adjust the limitation on camera rotation along the Y axis
    51.             if (distance <= 0.1)
    52.             {
    53.                 yMinLimit = -90;
    54.             }
    55.  
    56.             //Reset the limitation on camera rotation along the Y axis when in third-person perspective
    57.             else
    58.             {
    59.                 yMinLimit = -9;
    60.             }
    61.  
    62.             //Create variables which calculate how the camera should rotate and how to follow a pre-designated target
    63.             var rotation = Quaternion.Euler(y, x, 0);          
    64.             var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
    65.  
    66.             //Alternate method of calculation
    67.             //var rotation:Quaternion = Quaternion.Euler(y, x, 0);
    68.             //var position = target.position - (rotation * Vector3.forward * distance + Vector3(0,-targetHeight,0));
    69.  
    70.             // If right mouse button is held down, let the mouse govern camera rotation and position (and also hide the cursor)
    71.             if (Input.GetMouseButton(1))
    72.             {
    73.                 Screen.lockCursor = true;
    74.                 transform.position = position;
    75.                 transform.rotation = rotation;
    76.             }
    77.  
    78.             //Otherwise, ease behind the target if any of the directional keys are pressed (doesn't seem to work)
    79.             else if(Input.GetAxis("Vertical") || Input.GetAxis("Horizontal"))
    80.             {
    81.                 var targetRotationAngle = target.eulerAngles.y;
    82.                 var currentRotationAngle = transform.eulerAngles.y;
    83.                 x = Mathf.LerpAngle(currentRotationAngle, targetRotationAngle, rotationDampening * Time.deltaTime);
    84.             }
    85.         }
    86.     }
    87. }
    88.  
    89. static function ClampAngle (angle : float, min : float, max : float)
    90.     {
    91.         if (angle < -360)
    92.             angle += 360;
    93.         if (angle > 360)
    94.             angle -= 360;
    95.         return Mathf.Clamp (angle, min, max);
    96.     }
    97.  
    98.  
    99. @script AddComponentMenu("Camera-Control/Mouse Orbit")
    Any help would be appreciated, thanks! :)
     
    Last edited: Jan 11, 2011
  35. deadManN

    deadManN

    Joined:
    May 12, 2010
    Posts:
    154
    for Issue number 2... wow do some thing ... when camera is near character, from a specify distance to the exact middle of character, character start to transparent and camera go to the head of character, so you need to define a head position in ur character, and camera look from middle in range to head in little distance....
     
  36. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Isn't that kind of easily fixed using a simple math function...

    Code (csharp):
    1.  
    2. var lowerVisLimit=2.0;//fades completely if closer than this number
    3. var upperVisLimit=4.0;//starts fading at this distance
    4.  
    5. target.renderer.material.color.a = Mathf.Clamp01(((Camera.position-target.transform.position).magnitude - lowerVisLimit) / (upperVisLimit - lowerVisLimit)));
    6.  
    or something like that?
     
  37. stjepan

    stjepan

    Joined:
    Feb 8, 2011
    Posts:
    6
    Great thread ppl. I was interested more in phisics of camera (how to make it not go trough terrain and objects etc. and this hepled ALOT.
    Just wanted to let you know...
     
  38. MaliceA4Thought

    MaliceA4Thought

    Joined:
    Feb 25, 2011
    Posts:
    406
    Greetings all :)

    My first post here I am afraid and dragging this one up as an old thread, but I too am having this same problem.

    I have also tried renaming the Camera a number of ways but no luck.

    What's probably more annoying is that I have had this installed without this problem, but had to change the character I was using and when I re-installed this, I got the exception and cannot clear it.

    Any ideas welcomed.


    Regards

    Graham
     
  39. craigerz

    craigerz

    Joined:
    Mar 30, 2009
    Posts:
    19

    Yeh this was resolved somewhere back a few pages, you dont have your Camera tagged:

    1. Select your main Camera (or the camera holding the WOW cam script)
    2. Click the TAG dropdown menu located near the very top of the Inspector
    3. Select the main Camera as the tag.

    This will get rid of your error.

    Craig.
     
  40. MaliceA4Thought

    MaliceA4Thought

    Joined:
    Feb 25, 2011
    Posts:
    406
    Thanks Craig... figures it was something like that.

    Regards

    Graham
     
  41. cha9chi

    cha9chi

    Joined:
    Mar 24, 2011
    Posts:
    4
    Hello :D
    i want to convert this code from mouse input and manipulation to iphone touch manipulation... Can i find specific method for this??
     
  42. Ilikescifi

    Ilikescifi

    Joined:
    Dec 15, 2010
    Posts:
    17
    Great scripts. Also the ones before. Very usefull and (esp. for me important very simple to understand for the not so experienced programmer. Thank you everyone.
     
  43. xxxDjdogxxx

    xxxDjdogxxx

    Joined:
    Mar 28, 2011
    Posts:
    751
    Last edited: May 23, 2011
  44. jessee03

    jessee03

    Joined:
    Apr 27, 2011
    Posts:
    729
    Does anyone know how to make it so when you click the left or right mouse button that it doesn't reset to the center of the screen and stays in place ? Also when you hold down both mouse buttons you move forward / can move mouse left and right to change directions ?
     
  45. xxxDjdogxxx

    xxxDjdogxxx

    Joined:
    Mar 28, 2011
    Posts:
    751
  46. Malidian

    Malidian

    Joined:
    Mar 15, 2010
    Posts:
    19
    I saw some discussion about these way back in the discussion...maybe around a year ago or so...Go back and read from the beginning of the post all the way through and you should be able to do what you are asking. Its in there somewhere.
     
  47. kingdutka

    kingdutka

    Joined:
    Feb 1, 2011
    Posts:
    129
    Absolutely wonderful, thanks! The only thing that's missing is "move player/rotate camera if BOTH mouse clicks are held", but I can live with it as is. Thanks again!!
     
  48. Flapman

    Flapman

    Joined:
    Aug 15, 2010
    Posts:
    84
    Question about the Swimming Code. I created a file for this and get this warning:

    Thoughts? Sorry, I am "ignoramus" with coding.

    A
     
  49. Flapman

    Flapman

    Joined:
    Aug 15, 2010
    Posts:
    84
    About the Camera Script, I cannot seem to make it work. I have attached it to my camera. No mouse controls seem to do anything (eg. Zoom, etc).

    I may be missing a step.

    A
     
  50. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    How far do you think you're going to get? Let me tell you, not very far. I'm not trying to be nasty, but its true that non coder people are not going to get far with unity. But there is good news. Javascript isn't an impossible thing to learn, and Boo is even easier. You can do it if you try...