Bobbing Platform?

Hello everyone, I’ve been using this script for a bobbing platform which works fine going up and down. My problem is and I’ve tried manipulating this script over and over but I can never get the platform to bob left and right. It either is always just up and down or up and down at an angle. Never just left and right. Any help what is wrong? This seems to be the only script that isn’t creating garbage for me.

using UnityEngine;
using System;
using System.Collections;

public class PlatformBob : MonoBehaviour
{
    float originalY;
   
    public float floatStrength = 1;
   
    void Start()
    {
        this.originalY = this.transform.position.y;
    }

    void Update()
    {
        transform.position = new Vector3(transform.position.x,originalY + ((float)Math.Sin(Time.time) * floatStrength),transform.position.z);
    }
}

Have any of those modifications involved altering the X component of the transform and not altering the Y position of it? Right now you’re modifying the Y and not the X.

Y is up-down, X is left-right, Z is forward-backward. Replace all the Y with X and you’ll get it to move left-right.

OK I was able to solve it moving left and right only by changing:

changing the Start function line from y to x and in the Update function from transform.position.x,originalY to just using originalY.

Will rework the script to use bools to choose X or Y axis.

I’m afraid you don’t quite understand your code. You’re changing the Y.

You need to change the X:

using UnityEngine;
using System;
using System.Collections;
public class PlatformBob : MonoBehaviour
{
    float originalX;
  
    public float floatStrength = 1;
  
    void Start()
    {
        this.originalX = this.transform.position.x;
    }
    void Update()
    {
        transform.position = new Vector3(originalX + ((float)Math.Sin(Time.time) * floatStrength),transform.position.y,transform.position.z);
    }
}

I’ve pretty much spoonfed you the answer here, so before you frustrate yourself too much by not understanding how this works, I highly recommend reading this:
Understanding Transforms: https://docs.unity3d.com/Manual/Transforms.html
Scripting tutorials: https://unity3d.com/learn/tutorials/topics/scripting
And whole-project tutorials (Roll-a-ball is my favorite): https://unity3d.com/learn/tutorials/

1 Like

Yeah I just did that right before you responded. Going to use bools to choose X or Y axis so I can use the same script.

Here’s the final version working 100%.

using UnityEngine;
using System;
using System.Collections;

public class PlatformBob : MonoBehaviour
{
    float originalX;
    float originalY;

    public bool X;
    public bool Y;
   
    public float floatStrength = 1;
   
    void Start()
    {
        this.originalX = this.transform.position.x;
        this.originalY = this.transform.position.y;
    }

    void Update()
    {
        if(X)
        {
            transform.position = new Vector3(originalX + ((float)Math.Sin(Time.time) * floatStrength),transform.position.y,transform.position.z);
        }
        if(Y)
        {
            transform.position = new Vector3(transform.position.x,originalY + ((float)Math.Sin(Time.time) * floatStrength),transform.position.z);
        }
    }
}
1 Like