Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 bscohen06 · Mar 31, 2020 at 08:58 PM · rotationinputcamera movementtransformation

I was trying to have the camera track the mouse and this happened what is wrong???

Vid is bad quality but you can notice when I move the mouse the screen shakes I want the camera to rotate with the mouse. Vid: https://youtu.be/KNPOERVCYrU

code: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class turn : MonoBehaviour { // Start is called before the first frame update public float sens = 1.0f;

 void Update()
 {
     float mouseX = Input.GetAxis("Mouse X") * sens;
     float mouseY = Input.GetAxis("Mouse Y") * sens;
     transform.rotation = Quaternion.Euler(mouseX, mouseY, 0);
     mouseX *= Time.deltaTime;
     mouseY *= Time.deltaTime;
 }}
Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by The_Three_Vs · Mar 31, 2020 at 10:54 PM

As it currently is, your code sets the camera's rotation to be the value of the input axes themselves, so the camera's rotation will always be only minscule rotations in any direction. In order to get the camera to pan with the mouse, you have to make the rotation additive, for example with transform.eulerAngles += new Vector3(-mouseY, mouseX, 0);(The rearranged mouse values make the camera rotate correctly, as well).

Comment
Add comment · Show 5 · 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
avatar image bscohen06 · Mar 31, 2020 at 10:58 PM 0
Share

this worked well thx so much just wondering I never figured out what vector3 means?

avatar image The_Three_Vs · Mar 31, 2020 at 10:58 PM 0
Share

Furthermore, the mouse input axes only register values when you are actively moving the mouse, not when the mouse is still but off-center. If you want the camera to rotate when the camera is off-center, you could use something like:

 Vector2 mousePosition = Input.mousePosition;
 float mouseX = $$anonymous$$athf.Sign(mousePosition.x) * sens;
 float mouseY = $$anonymous$$athf.Sign(mousePosition.y) * sens;

$$anonymous$$athf.Sign returns -1 if the value is negative, 0 if the value is 0, and +1 if the value is positive.

avatar image The_Three_Vs · Mar 31, 2020 at 11:04 PM 0
Share

Vector3 is an object consisting of an x number, a y number, and a z number, all of which convey a distance from a certain origin. For example, a gameObject in Unity with a position of Vector3(5, -3, -8) would be located 5 units to the right, 3 units down, and 8 units behind the world origin. Vector3s are typically used for location, like in the example, but they can be used in any situation where you want a tight set of three float values. With eulerAngles, the provided Vector3 affects what the transform rotation values in the Unity Editor are.

avatar image bscohen06 The_Three_Vs · Mar 31, 2020 at 11:24 PM 0
Share

uhh I cant camera to stop when is stop moving mouse.

avatar image The_Three_Vs bscohen06 · Apr 01, 2020 at 12:07 AM 0
Share

Whoops. Forgot that the origin of Input.mousePosition is at the bottom left corner of the screen, not the center. Change them to $$anonymous$$athf.Sign(mousePosition.x - (Screen.width / 2)) and $$anonymous$$athf.Sign(mousePosition.y - (Screen.height / 2)). Alternatively / Additionally, if you want to have a "dead zone" in the middle of the screen, establish a size and then check two "rectangles" on the screen to see if the camera can rotate:

     public float deadZoneScreenProportion = 0.1f;
     void Update()
     {
         //Recenters mouse position to be relative to the center of the screen
         Vector2 mousePosition = Input.mousePosition;
         float xCenter = Screen.width / 2;
         float yCenter = Screen.height / 2;
         mousePosition -= new Vector2(xCenter, yCenter);
 
         Vector3 cameraRotation = Vector3.zero;
 
         //Dynamically adjusts the deadZoneSize to be relative to the screen so     that window resizing won't affect anything
         float deadZoneXSize = Screen.width * deadZoneScreenProportion;
         float deadZoneYSize = Screen.height * deadZoneScreenProportion;
 
         //If mousePosition.x is outside the vertical dead zone, add rotation
         if ($$anonymous$$athf.Abs(mousePosition.x) > deadZoneXSize)
         {
             cameraRotation.y = $$anonymous$$athf.Sign(mousePosition.x);
         }
         //If mousePosition.y is outside the horizontal dead zone, add rotation
         if ($$anonymous$$athf.Abs(mousePosition.y) > deadZoneYSize)
         {
             cameraRotation.x = -$$anonymous$$athf.Sign(mousePosition.y);
         }
 
         transform.eulerAngles += cameraRotation;
     }

avatar image
1

Answer by Codester95 · Mar 31, 2020 at 09:10 PM

Take a look at the FreeLookCamera from Unity's standard assets package. You need to have a pivot point for your camera to rotate around. For example you can target the player and rotate in reference to the players transform. If you are still having troubles. Message me and I can help further :)

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
avatar image
0

Answer by bscohen06 · Mar 31, 2020 at 09:25 PM

@codester95 how could I target a player?

Comment
Add comment · Show 1 · 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
avatar image bscohen06 · Mar 31, 2020 at 10:02 PM 0
Share

I actaully meant the caera rotating not on a axis but just rotating (im tsting for custom player controller)

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.

Follow this Question

Answers Answers and Comments

127 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 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 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 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 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 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 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 avatar image

Related Questions

lerp to 0, problem with horizontal value 1 Answer

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

Maya rotation transformation to Unity 0 Answers

How to rotate input based velocity vector based on relative angle of colliding wall so that player moves smoothly? 1 Answer

transform.rotation increment slows to a stop 1 Answer


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