Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Head Camera Movement

Discussion in 'Scripting' started by urdnotedward, Dec 10, 2015.

  1. urdnotedward

    urdnotedward

    Joined:
    Dec 16, 2014
    Posts:
    2
    Hello all,

    I am working on a project where I need to simulate the head movement of a large robot while it lumbers along. Here is an example of the desired movement. I will probably add in a slight camera shake in the x axis during the slight pauses to get the effect of heavy foot stomps.

    I initially tried using pingpong for the movement but that fell flat pretty quickly. I don't want to use animations because they're not flexible enough for the project, so I have to do it in C#. Any help or suggestions are much appreciated.
     
  2. Strategos

    Strategos

    Joined:
    Aug 24, 2012
    Posts:
    255
    Use Sine instead of pingpong?
     
    urdnotedward likes this.
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    I think you can still use an animation curve without "using animations", just a way of storing and getting a position from a curve at time x... http://docs.unity3d.com/ScriptReference/AnimationCurve.html
    that would allow you to customise the movement.

    edit:

    Code (csharp):
    1.  
    2. public class TestScript : MonoBehaviour
    3. {
    4. // appears in the inspector and can be setup as you like
    5.     public AnimationCurve myCurve;
    6.  
    7.     void Update()
    8.     {
    9.         transform.position = new Vector3(transform.position.x, myCurve.Evaluate(Time.time % myCurve.length), transform.position.z);
    10.     }
    11.  
    12. }
    13.  
    etc.
     
    Last edited: Dec 11, 2015
    urdnotedward and Nigey like this.
  4. Nigey

    Nigey

    Joined:
    Sep 29, 2013
    Posts:
    1,129
    Another cool thing about the AnimationCurve is you can treat it like any other float. So you can multiply it dynamically to amplify it's effect, or increase animation speed faster running, ect :).
     
    urdnotedward and LeftyRighty like this.
  5. urdnotedward

    urdnotedward

    Joined:
    Dec 16, 2014
    Posts:
    2
    Sorry for the late response, I have been super busy. Thanks for the suggestions! I have started using the animation curve and so far it is doing what I want. I just need to tinker around with it some more to get it to where I want it.

    I thought about using mathf.sin but I'm not quite sure how it works either, but I am going to experiment with both. Ty again.