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 conguerror · May 29, 2020 at 11:59 AM · cameracamera rotatecamera-lookcamera movement

Camera/can't turn on X

Camera is rotating on Y but not on X;

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraHandler : MonoBehaviour
 {
     public Transform camTrans;
     public Transform pivot;
     public Transform character;
     public Transform mTransform;
 
     public CharacterStatus characterStatus;
     public CameraConfig cameraConfig;
     public bool leftPivot;
     public float delta;
 
     public float mouseX;
     public float mouseY;
     public float smoothX;
     public float smoothY;
     public float smoothXVelocity;
     public float smoothYVelocity;
     public float lookAngle;
     public float tiltAngle;
 
     private void FixedUpdate()
     {
         FixedTick();
     }
     void FixedTick()
     {
         delta = Time.deltaTime;
         HandlePosition();
         HandleRotation();
         Vector3 targetPosition = Vector3.Lerp(mTransform.position, character.position, 1);
         mTransform.position = targetPosition;
     }
     void HandlePosition()
     {
         float targetX = cameraConfig.normalX;
         float targetY = cameraConfig.normalY;
         float targetZ = cameraConfig.normalZ;
 
         if (characterStatus.isAiming)
         {
             targetX = cameraConfig.aimX;
             targetZ = cameraConfig.aimZ;
         }
         if (leftPivot)
         {
             targetX = -targetX;
         }
         Vector3 newPivotPosition = pivot.localPosition;
         newPivotPosition.x = targetX;
         newPivotPosition.y = targetY;
         Vector3 newCameraPosition = camTrans.localPosition;
         newCameraPosition.z = targetZ;
         float t = delta * cameraConfig.pivotSpeed;
         pivot.localPosition = Vector3.Lerp(pivot.localPosition, newPivotPosition,t);
         camTrans.localPosition = Vector3.Lerp(camTrans.localPosition, newCameraPosition, t);
     }
     void HandleRotation()
     {
         mouseX = Input.GetAxis("Mouse X");
         mouseY = Input.GetAxis("Mouse Y");
         if(cameraConfig.turnSmooth > 0)
         {
             smoothX = Mathf.SmoothDamp(smoothX,mouseX,ref smoothXVelocity,cameraConfig.turnSmooth);
             smoothY = Mathf.SmoothDamp(smoothY, mouseY, ref smoothYVelocity, cameraConfig.turnSmooth);
         }
         else
         {
             smoothX = mouseX;
             smoothY = mouseY;
         }
         lookAngle += smoothX * cameraConfig.Y_Rotation_Speed;
         Quaternion targetRot = Quaternion.Euler(0, lookAngle, 0);
         tiltAngle -= smoothY * cameraConfig.Y_Rotation_Speed;
         tiltAngle = Mathf.Clamp(tiltAngle, cameraConfig.minAngle, cameraConfig.maxAngle);
         pivot.localRotation = Quaternion.Euler(tiltAngle,0,0); 
     }
 }
 Character status and Camera config are simple scriptable objects with no functions; 
Comment
Add comment · Show 2
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 SteenPetersen · May 29, 2020 at 12:10 PM 1
Share

Please be respectful of people's time if they wish to help you. Simply posting your code and saying it doesn't work is not exactly making it easy for someone to help you. Try to show us what you have tried before. Boil it down to a very simple reproducible aspect of the code, which will normally help you fix the problem yourself, and then ask for help. I cannot simply copy paste this code into my own project to help you as it is reliant on all sorts of different things, meaning it would take me a while to figure this out. Keep that in $$anonymous$$d when posting.

refer to these posts:
How do I post a good question - Unity

How do I post a good question - Stackoverflow

avatar image conguerror · May 29, 2020 at 12:13 PM 0
Share

@SteenPetersen Okay, won't happen again. I think that problem lies in HandleRotation() but I really can't see where is the problem, so I don't know what to try either

1 Reply

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

Answer by conguerror · May 30, 2020 at 08:40 AM

Solved it:

  lookAngle += smoothX * cameraConfig.Y_Rotation_Speed;
         Quaternion targetRot = Quaternion.Euler(0, lookAngle, 0);
         mTransform.rotation = targetRot;
         tiltAngle -= smoothY * cameraConfig.Y_Rotation_Speed;
         tiltAngle = Mathf.Clamp(tiltAngle, cameraConfig.minAngle, cameraConfig.maxAngle);
         pivot.localRotation = Quaternion.Euler(tiltAngle,0,0); 
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 conguerror · May 30, 2020 at 08:41 AM 0
Share

and ins$$anonymous$$d of FixedUpdate, I wrote Update

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

196 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

Related Questions

Automatic Camera Rotation in the 3D Game Kit 0 Answers

My camera is not linking with my movement 0 Answers

Camera gets stuck when cursor is locked 0 Answers

Fixed-Position Camera that follows player,Fixed Position Camera that follows target. 2 Answers

Camera movement and gun follow in unity 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