• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by fernandovt · Oct 28, 2013 at 01:57 AM · cameraobjectstickmaincamera

Object doesn't stick on Camera right Corner?? (Basic Question)

Hello:), So i move an object close to the player (like a flashlight) And then i drag the object to the maincamera hoping that this object moves along with 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?

1) An image showing the problem:

alt text

2)Main Camera Settings:

alt text

3) My MouseLook Script:

 using UnityEngine;
 using System.Collections;
 
 /// MouseLook rotates the transform based on the mouse delta.
 /// Minimum and Maximum values can be used to constrain the possible rotation
 
 /// To make an FPS style character:
 /// - Create a capsule.
 /// - Add the MouseLook script to the capsule.
 ///   -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
 /// - Add FPSInputController script to the capsule
 ///   -> A CharacterMotor and a CharacterController component will be automatically added.
 
 /// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
 /// - Add a MouseLook script to the camera.
 ///   -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
 [AddComponentMenu("Camera-Control/Mouse Look")]
 public class MouseLook : MonoBehaviour {
 
     public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
     public RotationAxes axes = RotationAxes.MouseXAndY;
     public float sensitivityX = 15F;
     public float sensitivityY = 15F;
 
     public float minimumX = -360F;
     public float maximumX = 360F;
 
     public float minimumY = -60F;
     public float maximumY = 60F;
 
     float rotationY = 0F;
 
     void Update ()
     {
         if (axes == RotationAxes.MouseXAndY)
         {
             float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
             
             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
             
             transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
         }
         else if (axes == RotationAxes.MouseX)
         {
             transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
         }
         else
         {
             rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
             rotationY = Mathf.Clamp (rotationY, minimumY, maximumY);
             
             transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
         }
     }
     
     void Start ()
     {
         // Make the rigid body not change rotation
         if (rigidbody)
             rigidbody.freezeRotation = true;
     }
 }


lololo.jpg (518.1 kB)
lilili.png (32.2 kB)
Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tomer-Barkan · Oct 28, 2013 at 05:46 AM 0
Share

Please share the hierarchy between the camera and the object you want to show, and the inspector settings for the object as well.

avatar image fernandovt · Oct 30, 2013 at 12:02 AM 0
Share

1) Hierarchy between $$anonymous$$ainCamera And Object(FlashLight):

alt text

2) Object inspector Settings:

alt text

lelele.png (20.9 kB)
lululu.jpg (32.1 kB)
avatar image fernandovt · Oct 30, 2013 at 03:50 PM 0
Share

I just can't figure out whats wrong with my scene..., Any object that i drag into the $$anonymous$$ainCamera doesn't move right when looking up/down:(, I have done everything in order to solve this but i can't find the answer. I downloaded a scene from someone else and when i drag objects to his player, they follow the camera, but in my scene, they don't. I paste both of his $$anonymous$$ouseLook Scripts in my player, nothing changes, i tried disabling all of the scripts in my scene, except the $$anonymous$$ouseLook scripts...Nothing happens. I even create a new First Person Controller, to try if it will behave normally, it had the same problem. I need to fix this but i can't find the problem, what could be wrong??, my scene settings???, I'm frustated, i can't solve this Please Help!!

avatar image Tomer-Barkan · Oct 30, 2013 at 03:56 PM 0
Share

Share the hierarchy again, expanded this time please. Specifically the transform information.

avatar image fernandovt · Oct 30, 2013 at 04:04 PM 0
Share

Ok here:

alt text

123.jpg (92.6 kB)
avatar image Tomer-Barkan · Oct 30, 2013 at 04:19 PM 0
Share

flashlight too please

avatar image fernandovt · Oct 30, 2013 at 04:22 PM 0
Share

alt text

flashlight image.png (43.6 kB)
avatar image fernandovt · Oct 30, 2013 at 04:24 PM 0
Share

But if it helps, i have experimented this camera/object behaviour with every object i have tried, it's not the flashlight, It happens with a cube too for example...

avatar image Tomer-Barkan · Oct 30, 2013 at 06:41 PM 0
Share

I tested your exact script (copy-paste) with a cube and it works perfectly.... Are you saying that if you have just a camera and a cube it still happens? Without all the flashlight objects whatsoever?

BTW, your camera appears to be a prefab. You do have just the one camera in the scene, right?

Try creating a new scene. Add a cube as child to the main camera. Add the script to the main camera. You'll see it works fine. Then replace the cube with your flashlight, but no scripts. Just the mesh. See if it still works. If it does, start adding scripts slowly, until you see where it breaks.

1 Reply

· Add your reply
  • Sort: 
avatar image
-1

Answer by Matthew012 · Oct 30, 2013 at 06:43 PM

Alright, this is what I do. I just apply the MouseLook script onto the Flashlight and it works for me (I've done this with others things when i had this issue)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get the constant Y rotation of the camera 2 Answers

Screen go darker when it look at an Object? 1 Answer

Typecasting Camera to Object for ObjectField 3 Answers

Attaching an RTS scrolling script to a camera (Noob Question) 3 Answers

Shaking camera 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges