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 firebird127 · Aug 18, 2018 at 07:55 AM · cameraprogramminglerpcamera rotateangles

Trouble coding a peeking system like in Rainbow Six Siege???

Hey guys im having trouble with the mathf.LerpAngles function in Unity. In my code i first ask for the input and establish the number of key presses it has been since the first "i" was equal to zero (even numbers mean that the player is peeking, while odd means the camera is at origin). For some reason my Camera will only rotate at the center. If a peeking button is pressed while not looking at the center, the camera snaps to it automatically. This is very incorrect. Also It doesnt seem to take into account the parameters i have set to see if i is even or odd. For now the positional statements are commented out because i am trying to fix lerping between angles first. Thank you all in advance! I appreciate any help or explanation i can get!

Here is the code:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class playerLean : MonoBehaviour {

 private int i = 0;
 private Vector3 currAngle;
 private Vector3 tarAngle;


 // Use this for initialization
 void Start () {
     Vector3 currAngle = transform.eulerAngles;
 }
 
 // Update is called once per frame
 void Update () {

     if (Input.GetButtonDown("Left Lean") && (i % 2) == 0)
     {
         tarAngle = new Vector3(currAngle.x, currAngle.y, 10f);

         currAngle = new Vector3
             (
             currAngle.x,
             currAngle.y,
             Mathf.LerpAngle(currAngle.z, tarAngle.z, 2 * Time.fixedDeltaTime)
             );

         transform.eulerAngles = currAngle;

 //        fpsCam.transform.position = new Vector3(Mathf.Lerp(fpsCam.transform.position.x, -0.39f, 0.5f * Time.fixedDeltaTime), 0f, 0f);

         i += 1;

     }

     if (Input.GetButtonDown("Right Lean") && (i % 2) == 0)
     {
         tarAngle = new Vector3(currAngle.x, currAngle.y, -10f);

         currAngle = new Vector3
             (
             currAngle.x,
             currAngle.y,
             Mathf.LerpAngle(currAngle.z, tarAngle.z, 2 * Time.fixedDeltaTime)
             );

         transform.eulerAngles = currAngle;

         //        fpsCam.transform.position = new Vector3(Mathf.Lerp(fpsCam.transform.position.x, -0.39f, 0.5f * Time.fixedDeltaTime), 0f, 0f);

         i += 1;
     }

     if ((Input.GetButtonDown("Left Lean") || Input.GetButtonDown("Right Lean")) && (i % 2) != 0)
     {
         tarAngle = new Vector3(currAngle.x, currAngle.y, 0f);

         currAngle = new Vector3
             (
             currAngle.x,
             currAngle.y,
             Mathf.LerpAngle(currAngle.z, tarAngle.z, 2 * Time.fixedDeltaTime)
             );

         transform.eulerAngles = currAngle;

         //        fpsCam.transform.position = new Vector3(Mathf.Lerp(fpsCam.transform.position.x, -0.39f, 0.5f * Time.fixedDeltaTime), 0f, 0f);

         i += 1;
     }
 }

}

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
0

Answer by Kitticid3 · Aug 18, 2018 at 03:57 PM

 using UnityEngine;
 
 //RED NOTE
 
 public class playerLean : MonoBehaviour {
     /////////////////////////////////VAR/////////////////////////////////
 
     // Use axis from Edit -> Project -> Input -> Axis ( create a new one and name "Lean" )
     /*You should use the following consturct:
     pivotY
     |
     +---+ pivotZ    - place script here
         |
         +--- pivotX ( camera, etc... )
 
         pivot X should have the local height elevated, for actual leaning ( local height - y )
     */
 
     //////////////////////////////// CFG
     public float    leanAngle;          //Target lean angle
     
     public float    leanDampingTo;     //Damp to peak angle
     public float    leanDampingBack;   //Damp to angle zero
 
     public float    leanError;         //Floating points are not quite as accurate as i would wish - set to 0.1  
 
     //////////////////////////////// MACHINE - DO NOT TOUCH =]]
     public float    leanDampingDelta;   //Lean damping currently in use
     public float    leanDelta;          //Lean distance remaining
 
     public bool     leanStatus;         //Still in lean?        // -1 for left, 1 for right, 0 for home
     public int      leanDirection;      //Lean direction
 
     /////////////////////////////////API/////////////////////////////////
     void Update() {
         if( (int)Input.GetAxis("Lean") != -leanDirection ) {
             leanDirection = -(int)Input.GetAxis("Lean");
             leanStatus = true;
             CheckDeltas();
         }
 
         if( leanStatus ) {
             ApplyLean();
             CheckLean();
         }
     }
 
     /////////////////////////////////FCT/////////////////////////////////
     void ApplyLean() {
         transform.Rotate( 0, 0, leanDelta * leanDampingDelta , Space.Self );
         leanDelta -= leanDelta * leanDampingDelta;
     }
 
     void CheckLean() {
         if ( Mod( leanAngle * leanDirection - AngleFilter(transform.localRotation.eulerAngles.z) ) < leanError  ) {
             Vector3 clamped = transform.localRotation.eulerAngles;
             clamped.z = leanAngle * leanDirection;
             transform.localRotation = Quaternion.Euler(clamped);
             leanStatus = false;
         }
     }
 
     void CheckDeltas() {
         Vector3 tmp = transform.localRotation.eulerAngles;
 
         leanDelta = leanAngle * leanDirection - AngleFilter(tmp.z);
 
         if (leanDirection == 0) {
             leanDampingDelta = leanDampingBack;
         } else {
             leanDampingDelta = leanDampingTo;
         }
     }
 
     float AngleFilter( float a ) {
         if ( a > 180 ) a -= 360;
         return a;
     }
 
     float Mod( float a ) {
         if (a < 0) return -a;
         return a;
     }
 
 }
Comment
Add comment · Show 2 · 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 Kitticid3 · Aug 18, 2018 at 04:01 PM 0
Share

@firebird127 - Ping!
It should work, for now. You can also use $$anonymous$$eycodes, if it better suits you.
As previously stated, it would be recommended you use a pivot system, to better shield rotations from each other.

avatar image firebird127 · Aug 18, 2018 at 04:29 PM 0
Share

@$$anonymous$$itticid3 Thank you so much for your reply! Will definitely try it out :)

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

155 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

Related Questions

Multiple Cars not working 1 Answer

3rd person camera doesn't rotate as intended 0 Answers

Lerping orthographic camera size jitters 1 Answer

Smooth camera shift, Lerp? SmoothShift? 2 Answers

Camera gets stuck when cursor is locked 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