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 JamesWTHarrop · Nov 14, 2018 at 11:07 PM · rotationplayercamera-look

Unable to turn character upside down due to problem with mouse look script

I'm making a 3D game in which the character is able to flip gravity and walk on the ceiling. I've done this before in 2D and wanted to try in 3D. The issue I'm having isn't the ability to flip gravity but that my character doesn't turn upside-down when gravity is flipped.

I followed a tutorial online to create 2 scripts for basic 3D character movement and mouse look. The issue I'm having stems from the last line in the mouse look script which sets the player's rotation based on the x component of the mouse around an upwards vector, as seen here:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CamMouseLook : MonoBehaviour
 {
     public Vector2 mouseLook;
     public Vector2 smoothV;
     public float sensitivity = 2.0f;
     public float smoothing = 2.0f;
 
     GameObject player;
 
     void Start ()
     {
         player = this.transform.parent.gameObject;
     }
     
     void Update ()
     {
         var md = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
 
         md = Vector2.Scale(md, new Vector2(sensitivity * smoothing, sensitivity * smoothing));
         smoothV.x = Mathf.Lerp(smoothV.x, md.x, 1f / smoothing);
         smoothV.y = Mathf.Lerp(smoothV.y, md.y, 1f / smoothing);
         mouseLook += smoothV;
         mouseLook.y = Mathf.Clamp(mouseLook.y, -90.0f, 90.0f);
 
         transform.localRotation = Quaternion.AngleAxis(-mouseLook.y, Vector3.right);
         player.transform.localRotation = Quaternion.AngleAxis(mouseLook.x, player.transform.up);
     }
 }

I know it's that last line that's causing the issue as commenting it out fixes everything but leaves my player unable to look/turn left and right. I've tried numerous ways to solve this issue to no avail. I'm not exceedingly experienced so there's a good chance that I'm missing something obvious and any help that gives me a push in the right direction is appreciated. If anyone wants to watch the tutorial in question to see if it is of any help, the link is here with the specific script being discussed in the last third of the video.

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 hectorux · Nov 15, 2018 at 01:14 AM

Maybe you could try player.transform.Rotate(new Vector3(0,mouseLook.x,0)); this wont make your rotation equal to a new Quaternion, instead, it will add a rotation in your local y axis

Comment
Add comment · Show 3 · 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 JamesWTHarrop · Nov 15, 2018 at 08:32 AM 0
Share

Hello, sorry for my delayed reply - It was late for me when you responded so I decided to look into it in the morning. I've tried using your code ins$$anonymous$$d and whilst it does work when both right way up and upside down, there seems to be an issue that the player will continuously rotate without stopping if the mouse is nudged ever so slightly left or right. I assume that this rotation is constantly being added to the player's current rotation which is causing the issue. If you have any other ideas, they would be greatly appreciated.

avatar image JamesWTHarrop JamesWTHarrop · Nov 15, 2018 at 09:06 AM 0
Share

Okay, so I tweaked your suggestion somewhat and came up with this: if (mouseLook.x != 0.0f) { player.transform.Rotate(new Vector3(0, mouseLook.x, 0)); mouseLook.x = 0.0f; } I used it to replace the last line of my code and it seems to work well so far. Thank you for the suggestion. :D

avatar image hectorux JamesWTHarrop · Nov 15, 2018 at 10:13 AM 0
Share

the problem with always rotating is because you used getAxisraw, this, have a difference with getaxis. The principal diference(if it isnt the only) is that you dont get a smooth resoult

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

141 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

Related Questions

Rotating objects to specific angle 3 Answers

Rotate Locally Player Towards Joystick/Axis Input 1 Answer

How to disable Mouselook with the new Unity 5? 1 Answer

Stopping object rotating at a certain rotation 1 Answer

How to have an object inherit rotation of player 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