- Home /
The question is answered, right answer was accepted
transform in worldspace
Hi guys, i need some help in what i guess is actually a quite simple task, but I couldn't figure it out for two hours.
I have a script attached to my enemy, that makes him wobble in the air:
transform.position += amplitude*(Mathf.Sin(2*Mathf.PI*frequency*Time.time) - Mathf.Sin(2*Mathf.PI*frequency*(Time.time - Time.deltaTime)))*transform.up;
The problem: since my enemy is angled by about 30° on the x axis, he wobbles deeper with each cycle, causing him to go through the ground.
I suppose i have to generate the direction in relation to world space, rather than its local space. But what do i use instead of transform.up, to get world space coordinates?
thanks in advance
Dr. H
Answer by robertbu · Aug 03, 2014 at 02:13 PM
'transform.up' is the world direction the top of your object. If you want the world up, then you can use Vector3.up. Unless there is other code impacting this object's rotation elsewhere, I cannot see the use of transform.up as the reason for the larger cycles. In looking at your code, I would suspect that variations in 'deltaTime' would make more of an impact. I only see this one line out of context, but I'd do an absolute movement based on a start position rather than rely on relative movement if I could. Example.
#pragma strict
var amplitude = 1.0;
var frequency = 2.0;
private var start : Vector3;
function Start () {
start = transform.position;
}
function Update () {
transform.position = start + amplitude*(Mathf.Sin(2.0 * Mathf.PI*frequency * Time.time)) * transform.up;
}
This way no errors of any sort will creep in over time.
first off, thanks for the reply.
Your code did the intended job.
Unfortunately, i do have other scripts, affecting the enemies movement. These are in conflict with the total movement.
However they work on the x and z axis, so I now only apply the total y value.
It's not exactly beautiful (neither code nor game-esthetics wise) but unless someone has an easy way to fix this elegantly, it's a workaround i can live with - for now, anyways.
cheers
Follow this Question
Related Questions
transform.position is giving me local space 1 Answer
How to move two same Objects at the same place in different scenes? 1 Answer
Convert terrain map coordinates (GetDetailLayer) into world position 0 Answers
Programatically calculate child offset in world space 1 Answer
Is there an end to Unity world space? 3 Answers