Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class MoverNoise : MonoBehaviour {
- public float NoiseAmntZ = 1;
- public float NoiseSpeedZ = 1;
- public float NoiseRandZ = 1;
- public float RotMag = 10;
- public float LeanAmnt = 45f;
- private Vector3 LastPosSave;
- void Update()
- {
- float RandomZ = (Mathf.Sin(Time.time * NoiseSpeedZ) + Random.Range(-NoiseRandZ, NoiseRandZ)) * NoiseAmntZ;
- transform.position = new Vector3(0f, 0f, RandomZ);
- Vector3 VelocityCalcAmnt = transform.position - LastPosSave;
- float VelocityAmnt = (VelocityCalcAmnt.magnitude);
- Debug.Log(VelocityAmnt);
- //transform.Rotate((transform.right * (VelocityAmnt.magnitude*RotMag)) * Time.deltaTime, Space.World);
- Vector3 euler = transform.localEulerAngles;
- euler.z = Mathf.Lerp(euler.z, transform.rotation.z, 2.0f * Time.deltaTime);
- transform.localEulerAngles = euler;
- LastPosSave = transform.position;
- }
- }
- ///////////////////////////////////////////////////////////
- /// <summary>
- /// Tilts a rotation towards a velocity relative to referenceUp
- /// Example:
- /// myTransform.rotation = TiltRotationTowardsVelocity( myCleanRotation.rotation, Vector3.up, velocity, 20F );
- /// </summary>
- /// <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>
- /// <param name="referenceUp" >The up Vector, mostly, this will be Vector3.up, if your gravity is pointing down</param>
- /// <param name="vel" >The velocity vector that is meant to cause the tilt</param>
- /// <param name="velMagFor45Degree" >A velocity with a magnitude of velMagFor45Degree will yield a 45degree tilt</param>
- /// <returns>returns currentRotation modified by a tilt</returns>
- public static Quaternion TiltRotationTowardsVelocity( Quaternion cleanRotation, Vector3 referenceUp, Vector3 vel, float velMagFor45Degree )
- {
- Vector3 rotAxis = Vector3.Cross( referenceUp, vel );
- float tiltAngle = Mathf.Atan( vel.magnitude /velMagFor45Degree) *Mathf.Rad2Deg;
- // Helping tp visualize, https://docs.unity3d.com/ScriptReference/Vector3.Cross.html
- // Debug.DrawRay( myTransform.position, referenceUp, Color.yellow ); //leftHand thumb
- // Debug.DrawRay( myTransform.position, vel, Color.green ); //leftHand index
- // Debug.DrawRay( myTransform.position, rotAxis, Color.cyan ); //leftHand middle
- return Quaternion.AngleAxis( tiltAngle, rotAxis ) *cleanRotation; //order matters
- }
Advertisement
Add Comment
Please, Sign In to add comment