Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Character Rotates with camera

Discussion in 'Scripting' started by Teriki Tora, Dec 9, 2010.

  1. Teriki Tora

    Teriki Tora

    Joined:
    May 21, 2010
    Posts:
    132
    Ok, after deciding that my old control scheme wasn't working... I abandoned my game for a while, turned back to it and started fiddling around with it. What I decided to do was to make my control scheme similar to "Savage: Battle for Newerth"'s control scheme. I really didn't want to copy someone else's control scheme, but my old one wasn't working and was really messing with any progress with my shooting mechanism (which I can just use a look-at-camera method and have it fire in reverse... which actually fires forwards).

    Why I'm looking to replicate the Savage rotation script is because it keeps the camera behind the character at all times, and it allows me to control my character a lot easier than what I had (and add a free-look which allows me to rotate the camera without the character rotating by holding the CTRL button or something out of the way) Now, what the Savage camera does, is that the character rotates depending on the mouse's movement along the axises. So basically, if I move my mouse towards the right side of the screen, my character will rotate towards the right (and always stay facing away from the camera).

    Problem is, that even if I have the same value for the speeds, my rotation is slower than my camera rotation, and it would be very tedious to try and find the EXACT value to get the rotations to be the same, and it may not be the exact value. The Mouse-Look script may work for an FPS (where you don't see your pill of a character, like in the tutorial), but I need the entire character to rotate, not a weapon.

    Changing the FPSWalker script to react to mouse movement:
    Code (csharp):
    1. var speed = 6.0;
    2. var jumpSpeed = 8.0;
    3. var gravity = 20.0;
    4. var rotateSpeed = 250;
    5.  
    6. private var moveDirection = Vector3.zero;
    7. private var grounded : boolean = false;
    8.  
    9. function FixedUpdate() {
    10.     if (grounded) {
    11.         // We are grounded, so recalculate movedirection directly from axes
    12.         moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    13.         moveDirection = transform.TransformDirection(moveDirection);
    14.         moveDirection *= speed;
    15.        
    16.         if (Input.GetButton ("Jump")) {
    17.             moveDirection.y = jumpSpeed;
    18.         }
    19.     }
    20.  
    21.     // Apply gravity
    22.     moveDirection.y -= gravity * Time.deltaTime;
    23.    
    24.     // Move the controller
    25.     var controller : CharacterController = GetComponent(CharacterController);
    26.     var flags = controller.Move(moveDirection * Time.deltaTime);
    27.         transform.Rotate(0, Input.GetAxis("Mouse X") * rotateSpeed * Time.deltaTime, 0);
    28.     grounded = (flags  CollisionFlags.CollidedBelow) != 0;
    29. }
    30.  
    31. @script RequireComponent(CharacterController)
    I've tried a look-away script that I found, but what happens is that my character rotates in random directions o_O
     
  2. enragedmrt

    enragedmrt

    Joined:
    Oct 5, 2009
    Posts:
    95
    All you need to do is manually place the camera as a child of the GameObject with the FPSWalker attached, then set the camera to the proper distance and angle behind the player, then, as the Player G/O w/ the walker script rotates, the camera will rotate with it since it stays in the same local position within the Player G/O.
     
  3. Vicenti

    Vicenti

    Joined:
    Feb 10, 2010
    Posts:
    664
    If you're currently happy with your camera's movements and the player only rotates on the Y axis -

    Code (csharp):
    1. var look : Vector3 = cam.transform.forward;
    2. look.y = 0;
    3. transform.rotation = Quaternion.LookRotation ( look );
    You can add some checks to make that work for looking up and down.
     
  4. Teriki Tora

    Teriki Tora

    Joined:
    May 21, 2010
    Posts:
    132
    Camera's already a child of my player, but if you notice with the mouse-look script, it only rotates the camera. In the FPS tutorial, only the weapon rotates because the weapon is a child of the camera, the "pill" does not. I need my entire character to rotate, not just its invisible weapon (which will soon be a bubble blower that appears from hammerspace X3).

    That's what I was looking for, Thank you. I don't need up and down looking, as the head-bone is tricky to animate in Unity, and it's meant to be cartoony anyways (locking the up and down rotation of the camera within the head movement will work too). That'll certainly get it working, I'll try it this afternoon after my capstone (that is, if I don't decide to do it for my capstone X3)
     
  5. Teriki Tora

    Teriki Tora

    Joined:
    May 21, 2010
    Posts:
    132
    Say, I'm getting a problem with the line:

    Code (csharp):
    1. var look : Vector3 = cam.transform.forward;
    It might just be Unity 3 instead of 2, but it doesn't see 'cam' as an identifier. Is that a name for your camera in your scene or is it meant to be the name of the camera script.

    If it's the name of the camera script, it's telling me "An instance of type 'UnityEngine.Component' is required to access non static member 'transform'."

    And if I switch 'cam' to 'camera', it tells me "You are not allowed to call get_camera when declaring a variable.
    Move it to the line after without a variable declaration.
    Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function."

    It's the only spot that's causing a snag.

    EDIT: Ah, nevermind, I looked at my Bubbleblower script, I don't need to worry too much about it, as the camera-look thing works in that script, I can just take examples from there and re-structure it.
     
    Last edited: Dec 9, 2010
  6. Vicenti

    Vicenti

    Joined:
    Feb 10, 2010
    Posts:
    664
    cam is meant to be any Camera object -

    var cam : Camera;

    you'd then set that to whichever ccamera it is you're using to direct the player (very likely Camera.main)

    using cam instead of Camera.main is slightly more optimized, is why I wrote it like that. I wasn't even thinking; I should have included that in my post. >..<