Search Unity

Camera rotation script on Z-axis

Discussion in 'Scripting' started by Predster, Feb 13, 2008.

  1. Predster

    Predster

    Joined:
    Sep 11, 2007
    Posts:
    145
    Hello,

    I have an FPS type project and I would like to create a script that allows the user to rotate the camera on the Z-axis (basically--they can spin around), using the x and z keyboard keys as negative and positive inputs, but I can't get it to work. :(

    First, I have no idea how to set up the inputs! You can only choose from X and Y in the axis box. Neither of these work for me.

    Also, I modified the Mouselook code to try and rotate in the Z direction, maybe the problem is here:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [AddComponentMenu("Camera-Control/Zrot1")]
    5. public class Zrot1 : MonoBehaviour {
    6.  
    7.     public float sensitivityZ = 15F;
    8.     public float minimumZ = -360F;
    9.     public float maximumZ = 360F;
    10.  
    11.     float rotationZ = 0F;
    12.    
    13.     Quaternion originalRotation;
    14.  
    15.     void Update ()
    16.     {
    17.    
    18.             rotationZ += Input.GetAxis("Mouse Z") * sensitivityZ;
    19.             rotationZ = ClampAngle (rotationZ, minimumZ, maximumZ);
    20.  
    21.             Quaternion zQuaternion = Quaternion.AxisAngle (Vector3.right, Mathf.Deg2Rad * rotationZ);
    22.             transform.localRotation = originalRotation * zQuaternion;
    23.             }
    24.    
    25.     void Start ()
    26.     {
    27.         // Make the rigid body not change rotation
    28.         if (rigidbody)
    29.             rigidbody.freezeRotation = true;
    30.         originalRotation = transform.localRotation;
    31.     }
    32.    
    33.     public static float ClampAngle (float angle, float min, float max)
    34.     {
    35.         if (angle < -360F)
    36.             angle += 360F;
    37.         if (angle > 360F)
    38.             angle -= 360F;
    39.         return Mathf.Clamp (angle, min, max);
    40.     }
    41. }
    42.  
    Thanks for the help! Is there a better way to do this?
     
  2. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    try the camera orbit script (located at camera control => comera orbit (it comes with unity when you buy it)) or PM me and i can send you a complete script with key control (the same as i am using in my games)
     
  3. Predster

    Predster

    Joined:
    Sep 11, 2007
    Posts:
    145
    Thanks, but it doesn't seem to work. I'd actually like the camera to rotate around--this script appears to make objects rotate around the camera, but I have no idea how to apply it to the camera to make that rotate, I'll message you.
     
  4. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Well take this script and apply it to your camera, then you set the object you want it to rotate around in the target variable, and then you set the "xbutton" and the "ybutton" in the input settings (set them to be the arrow keys), and then you are done.

    Oh and disable any other camera script in the object and the camera.
     

    Attached Files:

  5. Predster

    Predster

    Joined:
    Sep 11, 2007
    Posts:
    145
    Thank you for the script, but I actually don't want the camera to rotate around an object--I'd like it to rotate around the Z-axis. In an FPS, basically allowing the character to spin around to an upside down position. Is it possible to do this with this script?
     
  6. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    hm i don't know... try to set distance to 0... i think i have done that with my game as a text once...

    Hope it works.
     
  7. tim

    tim

    Joined:
    Feb 3, 2007
    Posts:
    113
    Code (csharp):
    1. Quaternion zQuaternion = Quaternion.AngleAxis (rotationZ, Vector3.forward);
    2. transform.localRotation = originalRotation * zQuaternion;
    Changing Vector3.right to Vector3.forward will give you a "barrel roll"-type rotation, if that's what you're looking for.
     
  8. Kenneth

    Kenneth

    Joined:
    Mar 3, 2008
    Posts:
    144
    This is a nice script. However when I implement it and follow the directions given it's still the camera that's moving around the object (a prefab in this instance). Is it possible to change it so it's the prefab that rotates around the prefabs own axis (not around the camera as it is now)?
     
  9. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
  10. Kenneth

    Kenneth

    Joined:
    Mar 3, 2008
    Posts:
    144
    Well, I don't have a player. I only have a fixed camera. And I'd like to have the camera or the object to rotate around both the x- and y-axis.
    I tried adding the script to the camera but it doesn't seem to work...?
     
  11. Kenneth

    Kenneth

    Joined:
    Mar 3, 2008
    Posts:
    144
    Ahh, I fixed it now. Apparantly it can't bee fixed on the 1st level of the prefab. I have to attach the script to the 2nd level of the prefab in the Project-view (not sure that the "level-terminology" is the correct way of refering).
     
  12. Kenneth

    Kenneth

    Joined:
    Mar 3, 2008
    Posts:
    144
    The only thing I'd like to improve is the thing about when the object is turned around it's turned around a spot right next to it. I'd like to turn the object around a point in the center of the object. That way the object turns around it's own axis and not around an axis right next to it.
     
  13. Kenneth

    Kenneth

    Joined:
    Mar 3, 2008
    Posts:
    144
    ..and by the way the problem only exists on the up/down-axis. Then it turns around the lowest point in it's figure. When the object is turned around sideways it's turning aound it's own axis.
     
  14. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    set the position of the gameobject you have attached the script to to zero.
     
  15. Kenneth

    Kenneth

    Joined:
    Mar 3, 2008
    Posts:
    144
    It looks like if I change any of the values of the position in the object then the object only appears different places on the screen. It didn't solve the problem.

    Is it by the way possible to implemet a function of zoom relative to the mousewheel?
     
  16. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    are you sure you have the camera attached to the same gameobject as the script?
     
  17. Kenneth

    Kenneth

    Joined:
    Mar 3, 2008
    Posts:
    144
    I fixed the zoomproblem but I still have the problem about the object moves around one if its corners instead of its center.
     
  18. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    can you set up an web player so i can see the problem?
     
  19. Kenneth

    Kenneth

    Joined:
    Mar 3, 2008
    Posts:
    144
    I'm behind a firewall so it's a no go, sorry.
    But the problem is actually very simple. Imagine a box that has to rotate around the center of it. Instead my version rotates around one of the corners of the box. In this way the box is kind of "floating" around that corner.
     
  20. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    try changing the position of the cube so the center of the cube is in the same position as the parents position
     
  21. Kenneth

    Kenneth

    Joined:
    Mar 3, 2008
    Posts:
    144
    The parents postion? Is that the main view or...? I haven't made the box to a child manually.
     
  22. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    hmm. if you select the box in the scene view is the xyz arrows in the middle of the cube?
     
  23. Kenneth

    Kenneth

    Joined:
    Mar 3, 2008
    Posts:
    144
    Well, the prefab is constructed during runtime, but if I add the prefab to the view just to try it then the arrows are preatty much off one of the corners. If I select click the prefab the arrows appear in the center of it. I noticed a change in the hierarchy-windows while doing this. When I add the prefab the "new prefab" is hightlighted. When I click the prefab afterwards the subtitle of the "new prefab" which is named "default" is highlighted. Maybe this has something to say about the problem?
     
  24. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    mm is your box made in another 3d program or is it just made in unity?
     
  25. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    oh and if you try this: start the game and select the gameObject the script is attached to,(and be sure that the handle position buttons is set to pivot (they are at the top of the screen)) i'm sure that the arrows will be placed at the point the camera is spinning around.
     
  26. Kenneth

    Kenneth

    Joined:
    Mar 3, 2008
    Posts:
    144
    The box is made in Maya.

    As there are not other objects in the hierarchy-windows then the camera (the objects are prefabs that are added during runtime) is the only thing I can choose. I tried setting the cameras handleposition to "pivot" but nothing changes during runtime.
     
  27. Kenneth

    Kenneth

    Joined:
    Mar 3, 2008
    Posts:
    144
    I managed to solve the problem now. Apparently the positionsettings in the "main prefab" is coordinates in to with "the world" while the "defualt prefab" contains the settings of the prefab in relashionship to the prefab itself.
     
  28. Kenneth

    Kenneth

    Joined:
    Mar 3, 2008
    Posts:
    144
    I managed to solve the problem now. Apparently the positionsettings in the "main prefab" is coordinates in to with "the world" while the "defualt prefab" contains the settings of the prefab in relashionship to the prefab itself.
     
  29. half_voxel

    half_voxel

    Joined:
    Oct 20, 2007
    Posts:
    978
    Oh good you solved the problem, so you can set the position settings per prefab, I did not know that. :D