Search Unity

  1. Unity 2020.1 has been released.
    Dismiss Notice
  2. We are looking for feedback on the experimental Unity Safe Mode which is aiming to help you resolve compilation errors faster during project startup.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Camera/Character Movement (MMORPG/WoW Like)

Discussion in 'Scripting' started by Menog, Oct 9, 2012.

  1. Menog

    Menog

    Joined:
    Oct 2, 2012
    Posts:
    23
    Hello,

    this is extracted from here -> http://forum.unity3d.com/threads/16949-WOW-Camera-Movement
    a great thx to all other ppl in that thread who are working on that


    i will keep this piece of code up-to-date
    every time i change the code here, i will add the changes to the list


    here is what i get so far.
    - the camera is moving back behind the player after moving around when the player was moving
    - selfattaching the player gameobject to the camera target
    - slower back and sidewalk
    - strafing (need to setup Input as "Strafing")
    - run just on hold key (need to setup Input as "Run")
    - public var "swimming" to reduce movementspeed by 70% when triggert
    - left right mousebutton moving, also sidebutton movetoggel (like wow) (setup Input as "Toggle Move" ("mouse 3" for my mx518 )
    - playerlayer avoid
    - camera softreturn while moving with mousesidebutton // v1.7 - 09.Oct.2012
    - added full movement list for animation usage to CharacterController // v1.9 - 09.Oct.2012
    - codecleanup // v2.0 - 09.Oct.2012
    - stop movement on autorun(sidebutton toggle) when jump or move back/forward // v2.1 - 09.Oct.2012

    - stop automovement on L+R Mousebuttons (sidebutton toggle) // v2.2 - 09.Oct.2012
    - fixxed the bug that doubels the movementspeed *// v2.3 - 19.Sep.2013

    what u need to setup to get this running.
    0. the Player/whatever u want to move needs a CharacterController (Unity->Component->Physics)
    1. the Main Camera needs to be tagged as MainCamera
    2. the Player/whatever u want to move needs to be tagged as Player
    3. the player needs to be added to a seperate layer
    4. on the attached CameraController script u need to deselect the Layer from 3. at "collisionLayers"
    5. the CharacterController script needs to be attached at the Player/whatever u want to move
    6. the CameraController script needs to be attached at the Main Camera
    7. "fix" for the clipping problem with the MainCamera. :
    - go to MainCamera -> Camera -> Clipping Planes -> Near and set it to 0.01

    - go to MainCamera -> Camera -> Field of View and set a value around 70 (it looks good)- 12.Oct.2012
    8. Example Project - 16.Oct 2012

    !! have fun with it and thx to all the other ppl who are working on this scripts !!
    and here is the code :)

    I had some free time and wrote a small website for it :) there u will find Version 2.3 of the character controller,
    screenshots how to use the scripts, a playable webbrowser demo and some more details :)

    http://ruhrnuklear.de/fcc/
     
    Last edited: Sep 24, 2013
  2. BlackMantis

    BlackMantis

    Joined:
    Feb 7, 2010
    Posts:
    1,475
    Nice Job. The cd works like a charm, and so does the lerping back to pos.

    I'm sure many will beg for a js version. (like me lol)

    Your code-fu is strong grasshopper.
     
  3. Menog

    Menog

    Joined:
    Oct 2, 2012
    Posts:
    23
    thx

    "Your code-fu is strong grasshopper. "
    hehe i know.. i will add a js and a cs version in the next days, clean the code and do some better comment (as far as my english allows that ^^)
     
  4. Menog

    Menog

    Joined:
    Oct 2, 2012
    Posts:
    23
    I added the scripts in JS and CS with code and for download also i made a codecleanup and corrected some "misstakes" and added some new futures (movements for animation)
     
  5. hcwork

    hcwork

    Joined:
    Sep 11, 2012
    Posts:
    72
    Hey, this script is nice. I wanted to show you a video of my results though. I love the acute control of the camera, very nice. But when I run up or down terrain, the camera snaps in and out. Check this video

    http://youtu.be/W7lvFWHxAes

    So, to be clear. My player is tagged as Player and also on another layer of Player. The camera is attached to my MainCamera. But the target is actually a childed game object called CameraTarget. Its really right behind the player. I got the same results when making the target the Player as well. Thanks
     
  6. Menog

    Menog

    Joined:
    Oct 2, 2012
    Posts:
    23
    I had that problem too, i solved it by adding the layer.

    thats a problem that there is still a gameobject between u and the camera that is not in the layer player layer, maybe try to place the camera and the player in the hirachy root without beeing in other gameobjects

    "My player is tagged as Player and also on another layer of Player. The camera is attached to my MainCamera. But the target is actually a childed game object called CameraTarget. Its really right behind the player. I got the same results when making the target the Player as well. Thanks "

    i didn't understand that fully...
    My player is tagged as Player and also on another layer of Player. So u mean the Player is on the Layer Player ?
    The camera is attached to my MainCamera Did u mean the camera script or that the camera is part of a gameobject that is tagged as maincamera?

    i have a gameobject and in it the player mesh with all the animation stuff and subobjects
    my camera is tagged as MainCamera and in the Hierarchy root without anything else

    they must be a gameobject between the gameobject tagged "Player" and the MainCamera tagged "MainCamera" that is not added to the Layer "Player"
    that is all what i can tell u so far
     
    Last edited: Oct 9, 2012
  7. hcwork

    hcwork

    Joined:
    Sep 11, 2012
    Posts:
    72
    moving the camera into the root hierarchy seemed to do the trick. I had it as a child of the player and it was assuming the Player layer as well. THanks for the tips. And great work, This is a nice, easy to use AND understand camera. Love it
     
  8. Menog

    Menog

    Joined:
    Oct 2, 2012
    Posts:
    23
    Oh dear.. it could be so easy XD

    what i didn't get running is that the camera didn't move under the terrain :/... maybe someone can help on this

    solved...
    go to the maincamera, under camera set -> Clipping Planes -> Near to "0.01"

    With this my "underworld" is perfect hidden ;)

    *first post updated*
     
    Last edited: Oct 12, 2012
  9. John-Higuera

    John-Higuera

    Joined:
    Oct 14, 2012
    Posts:
    20
    We have this messages,

    UnityException: Input Axis Toggle Move is not setup.
    To change the input settings use: Edit -> Project Settings -> Input
    CameraControllerjs.LateUpdate () (at Assets/WowoThree/CameraControllerjs.js:66)

    UnityException: Input Axis Run is not setup.
    To change the input settings use: Edit -> Project Settings -> Input
    CharacterControllerjs.Update () (at Assets/WowoThree/CharacterControllerjs.js:32)

    What controls shall we set on the project?

    Thanks
     
  10. Menog

    Menog

    Joined:
    Oct 2, 2012
    Posts:
    23
    The Message says all what u need ;)

    Got to -> Edit -> Project Settings -> Input

    Under Axes u see the Line "Size" increase the value by 2
    now on the bottom of the list u see the last Action 3 times, open the last one and name it "Toggle Move" and set the "Positive Button" to any key u want for it ("mouse 3" for me- sidebutton)

    do the same with the other duplicated entry and name it "Axis Run" and set the "Positive Button" to any key u want for it ("left shift" for me)

    I added a example Project
     
    Last edited: Oct 16, 2012
  11. Fijit

    Fijit

    Joined:
    Sep 1, 2011
    Posts:
    7
    Had a quick look at the example project and it looks great so far but I had to make one small change to the strafing code..

    on line 67 of the V2.2 Character Controller the strafing negative key is "q" (in the example project) but the code was also subtracting creating a double negative and basically making the controller go right instead of left.

    was this:
    Code (csharp):
    1. moveDirection.x -= Input.GetAxis("Strafing");
    changed to this:
    Code (csharp):
    1. moveDirection.x += Input.GetAxis("Strafing");
    Please correct me if I'm wrong as I've only been awake for 10 minutes :)

    Thanks for sharing this work
     
  12. Menog

    Menog

    Joined:
    Oct 2, 2012
    Posts:
    23
    uups.. u are right.. i made this project very fast at work and didn't test it

    i uploaded a new example project were i switched the keys, don't want to change the scripts

    thx for the hint
     
  13. madmike6537

    madmike6537

    Joined:
    Aug 20, 2012
    Posts:
    49
    Hello - just wanted to pop in and say I am using the latest version of your java scripts and they are working great. Thank you very much for the hard work on this.
     
  14. Menog

    Menog

    Joined:
    Oct 2, 2012
    Posts:
    23
    thx a lot but its not only my work ;)
     
  15. chrisx84

    chrisx84

    Joined:
    Nov 9, 2011
    Posts:
    84
    All this is not working for me. i been adding inputs for the past hour with no help and still does not work for me.
     
  16. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    Thank you to all those who worked on this
    I haven't looked at the post in forever and happened to see it in the forums today

    Very nice job

    ""Only problem I am having is that when I jump while moving the camera zooms all the way into the player and then jumps back out really fast
    Not sure if that explains it well or not but it seems the camera shouldn't zoom in out out at all while jumping""

    Did I set something up wrong or what haha


    EDIT: Jumping problem due to tagging issue with my stand in character
    He is made in Maya out of a couple cubes and spheres and all of them weren't tagged as Player


    Just would like to say thank you again for sharing this publicly



    EDIT:
    Still wondering wth "4. on the attached CameraController script u need to deselect the Layer from 3. at "collisionLayers" is supposed to mean

    Most of these instructions aren't very clear
     
    Last edited: Nov 16, 2012
  17. Menog

    Menog

    Joined:
    Oct 2, 2012
    Posts:
    23
    chrisx84, could u give me more infos whats not working ;)


    Nubz,
    3. the player needs to be added to a seperate layer
    4. on the attached CameraController script u need to deselect the Layer from 3. at "collisionLayers"

    that mean u have to add the player to a seperate layer and then deselect this layer in the CameraController script under the option "collisionLayers"
     
  18. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    I got it straight in my head long enough to get it working after I looked over the example again right after posting that
    Thanks for the reply
     
    Last edited: Nov 26, 2012
  19. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
    I had another thing in mind to ask you

    This doesn't handle animations at all correct?

    Meaning I would have to write a script for it?
     
  20. Venged

    Venged

    Joined:
    Oct 24, 2010
    Posts:
    500
    Thanks alot the controller is so smooth and works as described.

    Robert
     
  21. Menog

    Menog

    Joined:
    Oct 2, 2012
    Posts:
    23
    what animation do u mean ? my animations work like charm.. movement and swimming
     
  22. Deadwall

    Deadwall

    Joined:
    Feb 13, 2012
    Posts:
    5
    I might have found a bug,
    When I hold down "w" for forward movement (vertical) and also Left mouse and Right mouse, the 2 speeds combine and it moves at twice the intended rate.
    Is there a way to only take the L + R movement or only the w movement?
     
    Last edited: Feb 14, 2013
  23. yvanbroeck

    yvanbroeck

    Joined:
    Mar 20, 2013
    Posts:
    28
    Oh man, you saved me a lot of time!

    in order to learn how to code i suppose i should've tried this myself but to be honest when I see the complexity of the code I realise that this would've been too much for me.

    Thanks a ton!
     
  24. getyour411

    getyour411

    Joined:
    Oct 14, 2012
    Posts:
    6
    Wonderful thread, very useful.

    I do have a question - - the parameter "moveStatus" seems to be how you are passing the animation states - to what, and how, is the character controller script passing that string value? I assume the values for moveStatus you have (walking, running, swimming, backwalking_l, etc) match your animations but I don't see within the context of the CCscript how those are used to drive animations.
     
  25. getyour411

    getyour411

    Joined:
    Oct 14, 2012
    Posts:
    6
    I added this to the very end of the CController script to match moveStatus to my avatar's animation clips. I'm not certain about CrossFade or the timing, just found a reference to this method in the forums and it seems to work ok.

    Code (csharp):
    1.     switch (moveStatus) {      
    2.         case "idle" : animation.CrossFade("idle",.2f); break;
    3.         case "walk" : animation.CrossFade("walk",.2f); break;
    4.         case "run" : animation.CrossFade("run",.2f); break;
    5.         case "sidewalking_r" : animation.CrossFade("strafeRight",.2f); break;
    6.         case "sidewalking_l" : animation.CrossFade("strafeLeft",.2f); break;
    7.         case "jump" : animation.CrossFade("jump",.2f); break;
    8.         case "backwalking" : animation.CrossFade("walk",0.2f); break;
    9.         case "backrunning" : animation.CrossFade("run",.02f); break;
    10.         case "default" : animation.CrossFade("walk",.2f); break;
    11.         }
     
    Last edited: May 1, 2013
  26. LinerFlame

    LinerFlame

    Joined:
    Jul 24, 2013
    Posts:
    1
    Thank you so much Menog :) Really a good job, everything is just perfect :D Congratulations, really nice camera system :)

    Liner Flame Team
     
  27. Menog

    Menog

    Joined:
    Oct 2, 2012
    Posts:
    23
    Oh Man xD i forgot this topic...
    @Deadwall i never checked that :) i will fix that in the next days (i'm under linux only atm, need a WinVM again...)

    @getyour411, i do not really understand what u mean. the parameter "moveStatus" is for a walking or swimming character, i never use vehicels or something like that :) and yes, the animations are build to match my own animations but its good that u found a solution for your problem
     
  28. nelodev

    nelodev

    Joined:
    Jan 22, 2013
    Posts:
    14
    The animations does't work for me. What I have to do for play animations? I can't understand how moveStatus works.

    Thanks for the work.
     
  29. Menog

    Menog

    Joined:
    Oct 2, 2012
    Posts:
    23
    What u need to play animations... you need animations ;)
    the string just gives you a status what animation should be played, but you still need a animation for every movement, then you can assign the status with your animations
     
  30. nelodev

    nelodev

    Joined:
    Jan 22, 2013
    Posts:
    14
    Yes, I have animations. But the only animation that playing is idle.

    How I should play the animation? Short piece of code maybe?

    Thank you.
     
  31. nelodev

    nelodev

    Joined:
    Jan 22, 2013
    Posts:
    14
    Yeah!

    I found a videotutorial (at last) and I have animations!

    :-D
     
  32. Menog

    Menog

    Joined:
    Oct 2, 2012
    Posts:
    23
    Great you found a solution for your Problem :)

    I fixxed the bug that doubles the movementspeed when u hold L+R mousebuttons and press the forrwardkey

    also i wrote a small website for it where u will find the scripts and a playable demo and some more infos about it all :)
    http://ruhrnuklear.de/fcc/
     
  33. tkoptur1

    tkoptur1

    Joined:
    Sep 27, 2013
    Posts:
    2
    Hello, I am having problem with moving the object around. the camera works well but I am getting this error;

    "UnityException: Input Axis Run is not setup.
    To change the input settings use: Edit -> Project Settings -> Input
    CC.Update () (at Assets/Objects/Scripts/CC.cs:40)"

    I have added this Axis Run to the Input just like you have mentioned on this forum before but it is still giving me the same error over and over again. Can you help me?
     
  34. tkoptur1

    tkoptur1

    Joined:
    Sep 27, 2013
    Posts:
    2
    Nvm, i have just fixed it. I have downloaded your example unity folder and found what I was missing:). Your are awesome, thank you so much!!
     
  35. Menog

    Menog

    Joined:
    Oct 2, 2012
    Posts:
    23
    Did i explain it wrong? so i can correct it, or did you made a little mistake ? :)
     
  36. Calum-McManus

    Calum-McManus

    Joined:
    Sep 23, 2013
    Posts:
    5
    I've been trying to read through this, but I didn't seem to find a working answer, was there one there?
     
  37. Vis

    Vis

    Joined:
    Nov 16, 2013
    Posts:
    1
    So, I love this script. Awesome job. I am having difficulty with the animation. I can't get my animations to switch when a different move status is detected. I have it set to grab the walk animation and it has it walk with all forms. It walks with idle, walk, run, jump, all of them. So I set in my idle animation and now it only has idle, except for when I jump. When I jump it has the walk animation. Here, let me add my code really quick:

    Code (csharp):
    1.             //movestatus normal movement (for animations)          
    2.             if((moveDirection.x == 0 )  (moveDirection.z == 0))
    3.                 moveStatus = "idle";
    4.             if(moveDirection.z > 0)
    5.                 moveStatus = isWalking ? "walk" : "run";
    6.                 animation.Play("walk");
    7.             if(moveDirection.z < 0)
    8.                 moveStatus = isWalking ? "walk" : "run";
    9.                 animation.Play("walk");
    10.             if(moveDirection.x > 0)
    11.                 moveStatus = isWalking ? "walk" : "run";
    12.                 animation.Play("walk");
    13.             if(moveDirection.x < 0)
    14.                 moveStatus = isWalking ? "walk" : "run";
    15.                 animation.Play("walk");
    I don't have an animation for running so that's fine. If I add my idle animation under the moveStatus idle, it stays idle the whole time. Have I been building and looking at this screen too long and just doing a stupid mistake (probably) but I can't find it right now.

    Thanks :D
     
  38. Nightasy

    Nightasy

    Joined:
    Dec 1, 2013
    Posts:
    1
    What this really needs is an example or an explanation showing how to use the moveStatus string to actually apply an animation. I'm kind of newish to Unity 3d and my knowledge of animations has been working fine using Mechanim. This script does not make use of Mechanim as shown in the Unity tutorials, so how does one even apply or make use of the animations with this script?

    Just saying "you add animations" doesn't really cover it. The script doesn't offer a place to add animations, it just provides a string. Wonderful, now what do I do with that string? I love the camera movement and I definitely am sure I prefer this physics system versus the Rigidbody system as that can lack a responsive feeling. Any actual complete example showing what one would do with the string to apply an idle animation or even use a walk animation when pressing forward with this script would go light years into helping newer users such as myself and would be much appreciated.

    I can't make heads nor tails out of what I am supposed to do with the moveStatus string.
     
  39. Menog

    Menog

    Joined:
    Oct 2, 2012
    Posts:
    23
    I understand your problem, remember that this script ist just "my script as i use it"...
    i use another script for the animations because i want to cut or stop animations (when i'm falling i don't want to move forward for ex.) or when i swim and get hit or jump and start hitting, thats all handled in a new script. i use the string just for "basic" movementinfos.
    when someone real need "animations" then they need to change the script, because i don't need, i won't change the script ;)

    maybe i will attach a "animationscriptcontroller" in the future, but atm real life is taking all my time ;)
     
  40. GrimR

    GrimR

    Joined:
    Feb 2, 2014
    Posts:
    1
    Hello, I am a beginner with Unity.

    I tried applying this to Unities Third Person Controller (which already has a character with animations etc) and following the steps on the website does zilch (not a thing, no effect at all). Does anyone know why?
     
  41. Rev6604

    Rev6604

    Joined:
    Feb 15, 2014
    Posts:
    1
    I set mine up exactly as the instructions said too. It doesnt work. Im just looking out the camera at my player doing nothing.
    I know it all still works because I downloaded his example, made sure it looked exactly the same and mine didnt work but the example did.
     
  42. Menog

    Menog

    Joined:
    Oct 2, 2012
    Posts:
    23
    "It's not working for me" is very bad to debug :D

    maybe u both could upload your projects somewhere ?
     
  43. Rbriddick

    Rbriddick

    Joined:
    Nov 24, 2013
    Posts:
    3
    I have this same issue, but for me it is not helped. I have the Main Camera in root hierarchy, just as the Player GameObject. GameObject is tagged with "player" and also on the "player" layer.
    Main camera tagged with "MainCamera" and is on the "Default" layer. Camera Near Clipping set to 0.01 too.
    What am i missing?
     
    Last edited: Apr 23, 2014
  44. Menog

    Menog

    Joined:
    Oct 2, 2012
    Posts:
    23
    Could you upload your project ?
     
  45. Rbriddick

    Rbriddick

    Joined:
    Nov 24, 2013
    Posts:
    3
    It is strange, as i upraded Unity to 4.3.2 the whole problem seems to be solved. But all my Animator settings are resetted or something. It cannot remember my loop periods. Anyways, thank you for the support and this great code!
     
  46. Kalleal

    Kalleal

    Joined:
    May 3, 2014
    Posts:
    1
    I have a Jumping problem when i jump in place its fine but running an jumping will zoom like another sprint button !! Help please ....

    Script is amazing !!
     
  47. Musty

    Musty

    Joined:
    Jul 1, 2013
    Posts:
    31
    Hey,
    I was wondering if anyone successfully added animations to this, specially running diagonally?
     
  48. Mode_Six

    Mode_Six

    Joined:
    Feb 16, 2014
    Posts:
    4
    I was wondering if there is a way to disable Mouse 0 camera control. I'm trying to use this camera controller with a PS Vita and when I touch the screen it jumps the camera position in accordance with the touch.

    I've tried making some modifications to the camera controller script and it doesn't seem to work.
     
  49. Demonith88

    Demonith88

    Joined:
    Jun 30, 2014
    Posts:
    215
    Can some one update this i cant get it work
     
  50. paulus51

    paulus51

    Joined:
    Dec 5, 2012
    Posts:
    33
    well bump for this one to , i am wondering if there is a real like camera and controller out there like in wow ( world of warcraft) ?

    is it so hard to write this scripts ? i am not a expert in c sharp i have tested over 25 scripts but none is working like in wow

    ( MMO) RPG Camera & Controller versioon 1.1 is not oke bevouse the animations provided are just plane animation for the demo cube , you cant use them, wy not make a scipt where you put in the animations you want in like the start of the thirdperson controller from unity it self ?
     
    Last edited: Jan 30, 2015
unityunity