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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Object doesn't move along camera?? (Basic Question)

Discussion in 'Scripting' started by fernandovt, Oct 27, 2013.

  1. fernandovt

    fernandovt

    Joined:
    Oct 1, 2012
    Posts:
    67
    Hello:), So i move an object close to the player (like a flashlight) And the i drag the object to the maincamera hoping that this object moves along the camera. But when i do this, the object has a weird behaviour. When I move my view playing the game in the "y" axis rotation(looking to the left/right) the object looks fine, but when i look up, the object goes away, and when i look down the same...
    I just whant that the object stick to the camera and goes where i look like in a normal first person game./like the movement of the gun in a shooter, or a torch/flashlight in horror games, etc) Any ideas?
     
    Last edited: Oct 27, 2013
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You are probably using the first person controller. The rotate is on the controller, but the up and down are from the camera. If you parent it to the camera, it should stay in view.
     
  3. fernandovt

    fernandovt

    Joined:
    Oct 1, 2012
    Posts:
    67
    The flashlight object is parented to the camera, thats why i don't understand why it doesn't follow it normally. the up and down moves the object away from the camera. This is how i have the flashlight object attached to the camera:

    $lalalala.jpg
     
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    All I can think is it's something to do with the mouselook script. Try changing the rotation of the camera in the editor and see if the object stays in view. Remember a down rotation may put it under the ground. It shouldn't go out of view if the camera is rotated.
     
    Last edited: Oct 27, 2013
  5. fernandovt

    fernandovt

    Joined:
    Oct 1, 2012
    Posts:
    67
    Maybe an image helps you to figure out the problem:)

    $LOLOLO.jpg

    MouseLook Script:

    Code (csharp):
    1.  
    2. using System.Collections;
    3.  
    4. /// MouseLook rotates the transform based on the mouse delta.
    5. /// Minimum and Maximum values can be used to constrain the possible rotation
    6.  
    7. /// To make an FPS style character:
    8. /// - Create a capsule.
    9. /// - Add the MouseLook script to the capsule.
    10. ///   -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
    11. /// - Add FPSInputController script to the capsule
    12. ///   -> A CharacterMotor and a CharacterController component will be automatically added.
    13.  
    14. /// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
    15. /// - Add a MouseLook script to the camera.
    16. ///   -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
    17. [AddComponentMenu("Camera-Control/Mouse Look")]
    18. public class MouseLook : MonoBehaviour {
    19.  
    20.     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
    21.     public RotationAxes axes = RotationAxes.MouseXAndY;
    22.     public float sensitivityX = 15F;
    23.     public float sensitivityY = 15F;
    24.  
    25.     public float minimumX = -360F;
    26.     public float maximumX = 360F;
    27.  
    28.     public float minimumY = -60F;
    29.     public float maximumY = 60F;
    30.  
    31.     float rotationY = 0F;
    32.  
    33.     void Update ()
    34.     {
    35.         if (axes == RotationAxes.MouseXAndY)
    36.         {
    37.             float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
    38.            
    39.             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    40.             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    41.            
    42.             transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
    43.         }
    44.         else if (axes == RotationAxes.MouseX)
    45.         {
    46.             transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
    47.         }
    48.         else
    49.         {
    50.             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
    51.             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
    52.            
    53.             transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
    54.         }
    55.     }
    56.    
    57.     void Start ()
    58.     {
    59.         // Make the rigid body not change rotation
    60.         if (rigidbody)
    61.             rigidbody.freezeRotation = true;
    62.     }
    63. }
    64.  

    Main Camera Settings:

    $LILILI.png
     
    Last edited: Oct 27, 2013
  6. laurelhach

    laurelhach

    Joined:
    Dec 1, 2013
    Posts:
    229
    I know this is an old thread, but I was wondering if someone found a way to fix this particular issue.
    I have a similar problem.

    Thanks.