0
\$\begingroup\$

I intend to make exactly like this. I highly suggest you watch just 5 seconds of the video because it already explains what I intend to do. I've been trying cosine and sine on vector3s but I can't seem to make it work since I usually get these results:

enter image description here

This however is my intended result:

enter image description here

This is my current code:

public class CameraWave : MonoBehaviour
{
    [SerializeField] float speed = 1f;
    // how deep and how wide the sine waves should be
    [SerializeField] float depthXY = 2f;
    // radius of the circle
    [SerializeField] float radiusZ = 10f;
    // makes the waves on the y axis and translates it 
    [SerializeField] Transform targetLookAt;
    // parent of target look at so we can move it in the z direction
    [SerializeField] Transform lookAtContainer;
    // set angle to middle
    float angle = 0.35f;
    Vector3 cameraOffset;

    void Start() { cameraOffset = targetLookAt.position; }

    void Update()
    {
        // adjust angle
        angle += Time.deltaTime * speed;
        angle %= 360;

        // make cos-sine wave
        float x = Mathf.Cos(angle) * depthXY;
        float y = Mathf.Sin(angle) * depthXY;
        // set to look at
        Vector3 position = new Vector3(x,y);
        targetLookAt.localPosition = position;

        // update container to make it go in circles
        lookAtContainer.position = MakeCircleZ(angle) + cameraOffset;
    }

    Vector3 MakeCircleZ(float angle){
        Vector3 v = Vector3.zero;
        v.x = (radiusZ * Mathf.Cos(angle));
        v.z = (radiusZ * Mathf.Sin(angle));
        return v;
    }
}

My hierarchy setup:

enter image description here

any help is appreciated

\$\endgroup\$
3
  • 1
    \$\begingroup\$ It looks like you problem is that you are not moving enough in the Y-axis. This is probably because you use the same angle value for every axis. I would try changing "float y = Mathf.Sin(angle) * depthXY;" to "float y = Mathf.Sin(angle*cameraBobCount) * depthXY;". That should make your cameras Y position change more quickly than the x and z positions. You might also have to do the same to your x position in the Update function but I'm not really sure why you need this anyway. \$\endgroup\$ Jan 25, 2021 at 4:19
  • \$\begingroup\$ I wouldn't program anything for this and use the animation system instead. It's not just for characters. \$\endgroup\$
    – Philipp
    Jan 25, 2021 at 9:50
  • \$\begingroup\$ @Philipp can you provide an example where an animation system can utilize this? Just so we're all in the same page \$\endgroup\$ Jan 27, 2021 at 14:55

1 Answer 1

0
\$\begingroup\$

Managed to fix it. The camera offset was creating problems with the math; hence, my camera is in an offset position. I modified the code so that it does what I want.

public class CameraWave : MonoBehaviour
{
    [SerializeField] float speed = .1f;
    // how deep and how wide the sine waves should be
    [SerializeField] float depthXY = 2f;
    // radius of the circle
    [SerializeField] float radiusZ = 10f;
    
    [SerializeField] float cameraBob = 1;
    // makes the waves on the y axis and translates it 
    [SerializeField] Transform targetLookAt;
    // parent of target look at so we can move it in the z direction
    [SerializeField] Transform lookAtContainer;
    // set angle to middle
    float angle = 0.35f;

    Vector3 cameraOffset;
    void Start() {
        cameraOffset.y = targetLookAt.position.y;
    }
    void Update()
    {
        // adjust angle
        angle += Time.deltaTime * speed;
        angle %= 360;

        // make cos-sine wave
        float x = Mathf.Cos(angle) * depthXY;
        float y = Mathf.Sin(angle* cameraBob) * depthXY;
        // set to look at
        Vector3 position = new Vector3(x,y);
        targetLookAt.localPosition = position;

        // update container to make it go in circles
        lookAtContainer.position = MakeCircleZ(angle) + cameraOffset;
    }

    Vector3 MakeCircleZ(float angle){
        Vector3 v = Vector3.zero;
        v.x = (radiusZ * Mathf.Cos(angle));
        v.z = (radiusZ * Mathf.Sin(angle));
        return v;
    }
}
```
\$\endgroup\$
1
  • \$\begingroup\$ will make this the answer in two days \$\endgroup\$ Jan 25, 2021 at 6:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .