Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Maniacbob · Jun 18, 2015 at 06:43 PM · camerarandomrangeperlin noise

Trying to even a randomized rotation

So the title is a little unspecific but what I'm trying to do is create a head bob for my character using the camera's rotation. I'm using a modified version of the Unity standard asset HeadBob.cs.

 using System;
 using UnityEngine;
 using UnityStandardAssets.Utility;
 
 using System.Collections;
 using System.Collections.Generic;
 
 [RequireComponent(typeof(CharacterMotor))]
 
 public class HeadBobber : MonoBehaviour 
 {
     public Camera Camera;
     public LerpControlledBob jumpAndLandingBob = new LerpControlledBob();
     private CharacterMotor motor;
 
     public float HorizontalBobRange = 0.33f;
     public float VerticalBobRange = 0.33f;
     public AnimationCurve Bobcurve = new AnimationCurve(new Keyframe(0f, 0f), new Keyframe(0.5f, 1f),
                                                         new Keyframe(1f, 0f), new Keyframe(1.5f, -1f),
                                                         new Keyframe(2f, 0f)); // sin curve for head bob
 
     public float StrideInterval = 5;
     [Range(0f, 1f)] public float RunningStrideLengthen;
     private bool m_PreviouslyGrounded;
     private Vector3 m_OriginalCameraPosition;
 
     // Use this for initialization
     void Start () 
     {
         motor = GetComponent<CharacterMotor>();
         m_OriginalCameraPosition = Camera.transform.localEulerAngles;
     }
     
     // Update is called once per frame
     void Update () 
     {
         Vector3 newCameraPosition;
         if(motor.movement.velocity.magnitude > 0 && motor.IsGrounded())
         {
             //float zPos = m_OriginalCameraPosition.z + (Bobcurve.Evaluate(Mathf.PerlinNoise(Time.time, 0))*VerticalBobRange) - VerticalBobRange/2;
             //float zPos = m_OriginalCameraPosition.z + (Bobcurve.Evaluate(Mathf.PerlinNoise(Time.time, 0)) *VerticalBobRange) - 2.325f;
             float zPos = Bobcurve.Evaluate(UnityEngine.Random.Range(-1f, 1f)) * VerticalBobRange;
 
             Camera.transform.localEulerAngles = new Vector3(Camera.transform.localEulerAngles.x, Camera.transform.localEulerAngles.y, zPos);
             newCameraPosition = Camera.transform.localEulerAngles;
         }
         else
         {
             newCameraPosition = Camera.transform.localEulerAngles;
         }
         Camera.transform.localEulerAngles = newCameraPosition;
     }
 }


This all works except that the value that Bobcurve.Evaluate() returns is only a positive float value and I need to correct it because 0 is straight up and I need the head to bob both right and left not just left. I've tried a few methods to correct it, subtracting by half the bobRange, and by half the Perlin Noise range (which should be 0.5), and by half the effective range of the bob (the bob's range does not equal the difference between the high and low, not sure why just what it seems to do). I can get one of two outcomes, I can either get an equal range on both sides of 0 (-1.6 - +1.6) but only 1 in 10 frames is on the negative side, or I get 50/50 positive and negative but the range is predominantly positive (-0.5 - +2.5). Also using the Random.Range makes the camera jitter back and forth.

Does anyone have any suggestions to combine the two outcomes? I would like to be able to use the Perlin Noise to generate a smooth bob that has an even distribution of outcomes in both maximums and odds.

Comment
Add comment · Show 1
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 AlwaysSunny · Jun 18, 2015 at 06:59 PM 0
Share

This seems like a strange way to approach the requirement. The terms in which you're speaking tell me your approach is not as intuitive as it might be.

Creating the bob behavior is a technical pursuit, while tailoring that behavior is an artistic one. In situations like this, it's wise to think like an artist; ensure that your solution to the task works in an intuitive way.

For mechanical things, this often involves helper empties; building a mechanical rig of objects, each of which can be manipulated easily in local space.

This approach has the added benefit of giving you much more control. For instance, your "head motion" rig could also have behaviors for looking at a specific object, shaking the player's head yes or no, shaking as if in an earthquake, etc. You can make the rig independent of the player's $$anonymous$$ouseLook behaviors by operating on the rig in the rig's local space, then or temporary and/or partial control of the $$anonymous$$ouseLook for certain behaviors.

Also, you're having to work with euler angles, and specific hardcoded values and ranges. This is usually also a good sign you could tailor your solution to be a little more intuitive. Leverage helper objects, vector math, and the vector and quaternion class methods.

WRT your original question, I don't grasp why you'd want to use a random range or perlin noise to achieve anything here. A traditional head-bob motion would not feature randomness manifested in the way you're trying to use it. Perhaps the extents or rates might be pre-randomized for each cycle, but during the cycle, I cannot imagine random values producing desirable results. To get any randomness here, using noise strikes me as major overkill. I'd try to dig in and help more, but I didn't really grasp what you're doing.

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

triggering random animations with gui 1 Answer

two Random.Range returning similar values 1 Answer

Making a camera list 1 Answer

C# Randomize Background.Color Issues 1 Answer

Missing a Method? Random.Range? 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