cwisbg

LeanDirection

Jan 3rd, 2019
446
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5.  
  6. public class MoverNoise : MonoBehaviour {
  7.  
  8. public float NoiseAmntZ = 1;
  9. public float NoiseSpeedZ = 1;
  10. public float NoiseRandZ = 1;
  11. public float RotMag = 10;
  12. public float LeanAmnt = 45f;
  13. private Vector3 LastPosSave;
  14.  
  15. void Update()
  16. {
  17. float RandomZ = (Mathf.Sin(Time.time * NoiseSpeedZ) + Random.Range(-NoiseRandZ, NoiseRandZ)) * NoiseAmntZ;
  18. transform.position = new Vector3(0f, 0f, RandomZ);
  19. Vector3 VelocityCalcAmnt = transform.position - LastPosSave;
  20. float VelocityAmnt = (VelocityCalcAmnt.magnitude);
  21.  
  22. Debug.Log(VelocityAmnt);
  23.  
  24. //transform.Rotate((transform.right * (VelocityAmnt.magnitude*RotMag)) * Time.deltaTime, Space.World);
  25. Vector3 euler = transform.localEulerAngles;
  26.  
  27. euler.z = Mathf.Lerp(euler.z, transform.rotation.z, 2.0f * Time.deltaTime);
  28. transform.localEulerAngles = euler;
  29. LastPosSave = transform.position;
  30.  
  31. }
  32. }
  33.  
  34. ///////////////////////////////////////////////////////////
  35.  
  36. /// <summary>
  37. /// Tilts a rotation towards a velocity relative to referenceUp
  38. /// Example:
  39. /// myTransform.rotation = TiltRotationTowardsVelocity( myCleanRotation.rotation, Vector3.up, velocity, 20F );
  40. /// </summary>
  41. /// <param name="cleanRotation" >Target rotation of the transform, maybe your transform is already looking at something, you don't want to loose this alignment</param>
  42. /// <param name="referenceUp" >The up Vector, mostly, this will be Vector3.up, if your gravity is pointing down</param>
  43. /// <param name="vel" >The velocity vector that is meant to cause the tilt</param>
  44. /// <param name="velMagFor45Degree" >A velocity with a magnitude of velMagFor45Degree will yield a 45degree tilt</param>
  45. /// <returns>returns currentRotation modified by a tilt</returns>
  46. public static Quaternion TiltRotationTowardsVelocity( Quaternion cleanRotation, Vector3 referenceUp, Vector3 vel, float velMagFor45Degree )
  47. {
  48. Vector3 rotAxis = Vector3.Cross( referenceUp, vel );
  49. float tiltAngle = Mathf.Atan( vel.magnitude /velMagFor45Degree) *Mathf.Rad2Deg;
  50.  
  51. // Helping tp visualize, https://docs.unity3d.com/ScriptReference/Vector3.Cross.html
  52. // Debug.DrawRay( myTransform.position, referenceUp, Color.yellow ); //leftHand thumb
  53. // Debug.DrawRay( myTransform.position, vel, Color.green ); //leftHand index
  54. // Debug.DrawRay( myTransform.position, rotAxis, Color.cyan ); //leftHand middle
  55.  
  56. return Quaternion.AngleAxis( tiltAngle, rotAxis ) *cleanRotation; //order matters
  57. }
Advertisement
Add Comment
Please, Sign In to add comment