JWdell
March 2, 2017, 5:04pm
1
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.
JWdell
March 11, 2017, 1:11am
4
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.
JWdell:
Yes, I’ve pretty much tried every variation possible.
Start / Update
y,x,z = default - bobbing up and down as intended exactly where the object is in the level
x,y,z = bobs up / down at an angle and moves gameobject to a new spot than where it is originally in the level
x,x,z = bobs up and down and moves gameobject to a new spot than where it is originally in the level
y,y,z = bobs up and down at angle and moves gameobject to a new spot than where it is originally in the level
No matter what I change the x,y,z coordinates to it just never goes left and right. Only up / down or up / down at an angle.
I edit this in Start
transform.position.y;
and this in the Update
transform.position.x
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
JWdell
March 11, 2017, 1:20am
6
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.
JWdell
March 11, 2017, 3:23am
7
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