Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 soulblade · Feb 14, 2017 at 09:25 AM · c#camerarotationgameobjecttime.deltatime

help with rotation, mouse input and time.deltatime, a thirdperson script

hello unity forums, I haven't used unity 3d in a long while... and I thought I would pick it up again and do something interesting. I have gotten back into coding in C# but I am not getting the desired behavior from a script that I wrote. (sorry for the first paragraph of code non-formatted, it shows up wrong in the preview)

[code=CSharp]

using System; using UnityEngine;

public class playerlooky : MonoBehaviour { public float mouseSensitivity = 100.0f; public float clampAnglePos = 80.0f; public float clampAngleNeg = 50.0f; public bool mode = true; public bool Switch = false; public float MouseMagnitude = 2f; private Vector3 tmpMousePosition; private Vector2 MousePos2D; private Vector2 CurrentMousePos; public float timeleft = 0; public float CurrentMagnitude = 0;

 private float rotY = 0.0f; // rotation around the up/y axis
 private float rotX = 0.0f; // rotation around the right/x axis

 void Start()
 {
     Vector3 rot = transform.localRotation.eulerAngles;
     rotY = rot.y;
     rotX = rot.x;
     tmpMousePosition = Input.mousePosition;
     MousePos2D.x = tmpMousePosition.x;
     MousePos2D.y = tmpMousePosition.y;
 }

 void Update()
 {
     CurrentMousePos.x = Input.mousePosition.x;
     CurrentMousePos.y = Input.mousePosition.y;
     CurrentMagnitude = Mathf.Sqrt(Mathf.Pow(CurrentMousePos.x, 2) + Mathf.Pow(CurrentMousePos.y, 2));

     if (mode == true)
     {
         float mouseY = -Input.GetAxis("Mouse Y");
         float mouseX = Input.GetAxis("Mouse X");

         rotY += mouseY * mouseSensitivity * Time.deltaTime;
         rotX += mouseX * mouseSensitivity * Time.deltaTime;

         rotY = Mathf.Clamp(rotY, -clampAngleNeg, clampAnglePos);

         Quaternion localRotation = Quaternion.Euler(rotY, rotX, 0.0f);
         transform.rotation = localRotation;
     }

     if (CurrentMagnitude > MouseMagnitude)
     {
         timeleft = 0;
     }

     if (Switch == false)
     {
         timeleft += Time.deltaTime;
         mode = true;
     }

     if (transform.eulerAngles.x == 0 & transform.eulerAngles.y == 0 & Switch == true)
     {
         Switch = false;
         mode = true;
     }

     if (timeleft < 10)
     {
         Switch = true;
         mode = false;
         timeleft = 0;
     }

     if (Switch == false)
     {
         transform.rotation = Quaternion.Euler(Mathf.Lerp(transform.eulerAngles.x, 0, timeleft), Mathf.Lerp(transform.eulerAngles.y, 0, timeleft), 0);
         timeleft += 0.5f * Time.deltaTime;
     }
     
 }

}

[/code] what it is supposed to do is set up variables for keeping track of mouse position and attached object rotation then determine the amount the user has moved the mouse from the last position. the next "mode" block is for one of the modes it operates in, it has two states, one where the object is controlled by the user, the other is when the user hasn't moved the mouse enough by a set amount and rotates the object back to its default rotation relative to its parent. instead of giving the user control I have run into a recurring problem in that If I have more than 1 transform of any type be it quaternion, rotation or euler, none of the rotations will come into effect and leave the object stranded in its default rotation relative to the camera. I hope I have provided enough information and will check back when I can, thanks!

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

1 Reply

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

Answer by PixelSpartan · Feb 14, 2017 at 05:05 PM

First set Cursor.lockState to locked so cursor is always in middle of screen. Then use Input.GetAxis("Mouse X") and "Mouse Y" to get a delta of mouse movement, and then just do if Input.GetAxis("Mouse X") > 1 or other number to check if mouse moved more than that

Comment
Add comment · Show 4 · 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 soulblade · Feb 14, 2017 at 08:13 PM 0
Share

can you explain this in more detail? I already have some Input.GetAxis("$$anonymous$$ouse X") and '$$anonymous$$ouse y' and there is some code in there to handle telling if the mouse has moved or not. my problem is that my current code is not changing the objects angles, can you help with that?

avatar image PixelSpartan soulblade · Feb 14, 2017 at 09:12 PM 0
Share

On which axis do you want to rotate?

Ins$$anonymous$$d of doing quaternions to euler just do

transform.eulerAngles += new Vector3 (Input.GetAxis("$$anonymous$$ouse Y"), Input.GetAxis("$$anonymous$$ouse X", 0);

avatar image PixelSpartan PixelSpartan · Feb 14, 2017 at 09:12 PM 0
Share

And make sure to do

transform.eulerAngles = new Vector3(transform.eulerAngles.x,transform.eulerAngles.y,0);

after

Show more comments

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

300 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 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 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

Align rotation when using ScreenToWorldPoint 0 Answers

Flip over an object (smooth transition) 3 Answers

Camera rotation the same as player rotation 1 Answer

Rotating camera problem C# Android 2 Answers

Coding noob struggles to implement first-Person camera rotation. 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