- Home /
How can i set transform.position on y ? And why i'm getting error cannot convert double to float ?
var currentHeight = Terrain.activeTerrain.SampleHeight(transform.position);
transform.position += Mathf.Lerp(transform.position.y, currentHeight + 90.0f, 0.1);
How can i update the transofrm.position.y ? I can't make :
transform.position.y += Mathf.Lerp(transform.position.y, currentHeight + 90.0f, 0.1);
And why i'm getting error on the 0.1 cannot convert from double to float ? I tried to add f like 0.1f but it didn't solve it.
Answer by Bunny83 · May 22, 2017 at 05:51 PM
There are several errors in your code:
First
0.1
is a double constant. You have to use0.1f
Second your first code block tries to add a float to a Vector3. That doesn't work. You may want to convert the float into a Vector3
Next the way you use Lerp would return an absolute position value but you try to add that value to the current position which would result in a progressive addition if you execute that code several times. Also that piece of code would be frame rate dependent.
Finally your second code block doesn't work in C# because transform.position is a property of a value type (Vector3). So you can't directly change a component of the returned Vector3 value.
From the code it's hard to tell what that code should do. I guess that you want to move the object smoothly to a position 90 units above the terrain.
Generally there are two options:
Either work with a target position and use MoveTowards
var currentHeight = Terrain.activeTerrain.SampleHeight(transform.position);
var target = transform.position;
target.y = currentHeight + 90.0f;
transform.position = Vector3.MoveTowards(transform.position, target, Time.deltaTime * speed);
or if you prefer using Lerp (which will be framerate dependent) you can do:
var currentHeight = Terrain.activeTerrain.SampleHeight(transform.position);
transform.position += new Vector3(0f, currentHeight + 90.0f - transform.position.y, 0f) * 0.1f;
Answer by toddisarockstar · May 17, 2017 at 09:22 PM
the variable currenthight is pry a double since you did not decalir as a float. and in your lerp you dont need to add. try this:
var currentHeight:float = Terrain.activeTerrain.SampleHeight(transform.position);
transform.position.y = Mathf.Lerp(transform.position.y, currentHeight + 90.0, 0.1);
Your answer
Follow this Question
Related Questions
Why when creating new animator controller for the character the character is not walking right ? 0 Answers
How can i Instantiate on the terrain from left to right ? 0 Answers
Why the tile map scripts take almost all the cpu usage ? cpu usage is getting to 99% at times 1 Answer
How can i rotate object by pressing on key R and keep object facing to me my self ? 0 Answers
How can i spawn new gameobjects to be inside the terrain area ? 2 Answers