Search Unity

Animation problem with movement camera

Discussion in 'Animation' started by Snykeur, Aug 19, 2015.

  1. Snykeur

    Snykeur

    Joined:
    Aug 19, 2015
    Posts:
    9
    Hello,
    So I created some animations for a character who has a camera. But I made the animations with the camera. Now I can't move the camera vertically. How can i resolve that problem please ?
    Thank you in advance !
     
  2. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    You'll have to explain more.

    What do you mean "Has a camera" Holding a camera model? Has a camera parented to him? Camera has keyframes?
     
  3. Snykeur

    Snykeur

    Joined:
    Aug 19, 2015
    Posts:
    9
    No it's the character parented to the camera.
     
  4. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Ok so is your camera controlled by a script? Something thats explicitly telling it to be at a certain height?

    I am thinking by your first post, that it isn't and that your camera has some keyframes on it, that are forcing it to move back to where you don't want it.. You want to probably remove then if so, on the camera object, or at least the y translation ones. Is it a first person setup, or a third person where you can see the back of your characters head?
     
  5. Snykeur

    Snykeur

    Joined:
    Aug 19, 2015
    Posts:
    9
    The script is applying the character but it controls the camera and the character's movement. And by the way, it's a first person setup.
     
  6. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Anything in your script forcing your cameras y position?
     
  7. Snykeur

    Snykeur

    Joined:
    Aug 19, 2015
    Posts:
    9
    Yes i think it's that :
    Vector3 desiredMove = cam.transform.forward*input.y + cam.transform.right*input.x;
     
  8. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    You really need to post your whole script. Its a lot of guesswork for everyone else when we don't know what the rest of your script is doing.

    EG whats "input" and how are you calculating it? Post your script and we can provide better feedback.
    - but it does look like the issue you are having stems from here.
     
  9. Snykeur

    Snykeur

    Joined:
    Aug 19, 2015
    Posts:
    9
    It's a script from a prefab in the "standard assets" so i don't really know how it works... Anyway there's the script.
     

    Attached Files:

  10. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Oh right. Yea the Unity guys love writing stuff that is not that easy to follow if you're new or an artist.

    Its an ugly place to start, for progress's sake I'd try and get rid of all the camera code in this, make sure you cameras parented to your character object, and put a mouselook script on your camera for a start. The default samples are written by highend coders and its probably fair to say most of us never really get whats happening in detail with these examples. The authors don't seem to understand this disconnect with their users, so yea, keep trying and don't try to figure it all out anytime soon.

    You could try beating it into submission with your keyboard or strangling it with your mouse cord;
    Maybe:
    Code (csharp):
    1.  
    2. desiredMove.y = desiredMove.y*movementSettings.CurrentTargetSpeed;
    3.  
    Could be assaulted into something like

    Code (csharp):
    1.  
    2. desiredMove.y = desiredMove.y*movementSettings.CurrentTargetSpeed+(yourOffset);
    3. eg
    4. desiredMove.y = desiredMove.y*movementSettings.CurrentTargetSpeed+(1.5f);
    5.  
    EDIT: I just added and "f"to the 1.5^

    Don't know, guessing now. Its late and even if it was early I wouldn't attempt deciphering this personally.
    Try that else time for someone else to step in sorry.
     
    Last edited: Aug 20, 2015
  11. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    I don't know if this is any help- its the original RigidBodyFPS script in Javascript from 2006. Still a little complex but still easier to follow. It comes from a setup as I described where the camera is simply parented to the player capsule with a mouselook on it.

    I apologise if its so old it just doesn't work. It might be useful to you though.

    Code (csharp):
    1.  
    2. var speed = 10.0;
    3. var gravity = 10.0;
    4. var maxVelocityChange = 10.0;
    5. var canJump = true;
    6. var jumpHeight = 2.0;
    7. private var grounded = false;
    8.  
    9. @script RequireComponent(Rigidbody, CapsuleCollider)
    10.  
    11. function Awake ()
    12. {
    13.    rigidbody.freezeRotation = true;
    14.    rigidbody.useGravity = false;
    15. }
    16.  
    17. function FixedUpdate ()
    18. {
    19.    if (grounded)
    20.    {
    21.      // Calculate how fast we should be moving
    22.      var targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    23.      targetVelocity = transform.TransformDirection(targetVelocity);
    24.      targetVelocity *= speed;
    25.      
    26.      // Apply a force that attempts to reach our target velocity
    27.      var velocity = rigidbody.velocity;
    28.      var velocityChange = (targetVelocity - velocity);
    29.      velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
    30.      velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
    31.      velocityChange.y = 0;
    32.      rigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
    33.    
    34.      // Jump
    35.      if (canJump && Input.GetButton("Jump"))
    36.      {
    37.        rigidbody.velocity = Vector3(velocity.x, CalculateJumpVerticalSpeed(), velocity.z);
    38.      }
    39.    }
    40.    
    41.    // We apply gravity manually for more tuning control
    42.    rigidbody.AddForce(Vector3 (0, -gravity * rigidbody.mass, 0));
    43.    
    44.    grounded = false;
    45. }
    46.  
    47. function OnCollisionStay ()
    48. {
    49.    grounded = true;  
    50. }
    51.  
    52. function CalculateJumpVerticalSpeed ()
    53. {
    54.    // From the jump height and gravity we deduce the upwards speed
    55.    // for the character to reach at the apex.
    56.    return Mathf.Sqrt(2 * jumpHeight * gravity);
    57. }
     
  12. Snykeur

    Snykeur

    Joined:
    Aug 19, 2015
    Posts:
    9
    Ok thanks for helping and i've another question : is it better to use animator or animation in my case to resolve the problem of the camera not moving ? Because if i turn off the animator attach to the character, the MouseLook works
     
  13. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    Are you using an animator to animate hands on front of your camera?
    If so you should have that as a child of your main object
    The hierarcy is usually like
    x-Root (Capsule Collider with rigidbody fps script - usually with mouselook that enables left to right rotation)
    xx-(Camera usually with mouselook that enables up and down rotation)
    xx-(Animated Hands as child of Capsule Collider-Which is where your animation/Animator should be located)

    - My point being that the Camera Mouselook and Animator/Animation are usually down two forks of the hieracy, and you shouldn't have any interference.
    If you don'r have animated hands or a body viewd in first person, you shouldn't need either animator or animation, because you are driving your movement via script. Its two layers really, and this tends to apply to NPCs and Player characters:

    3dmovement in the worlds space: Controlled by script.
    player/Npcs animations (walk jump etc): parented to scripted controller, triggered by script (or mecanim state machine), but keyframed artwork made in a modelling or animation app.


    So if you do have animated keyframed artwork- its animator should be in the hireacy where its not effecting your mouselooks and camera and if you don't have keyframed animated artwork I cant see why you'd need a animator or animation component and you can try removing it.
     
    theANMATOR2b likes this.