I’m new to coding, I’ve only been self-researching for about six months now. I wrote a very simple script to make the terrain move while keeping the player at the origin, but I think I need a buffer or something to make it work, as the terrain stutters. Anyone able to help me figure this out? Here’s what I’ve got:
An empty GameObject with the below script attached.
A FP Controller, attached to GameObject
A Terrain, attached to GameObject
using UnityEngine;
using System.Collections;
public class terrain : MonoBehaviour {
public Terrain TRef;
public GameObject Player;
private Vector3 PlayerPos;
private Vector3 TPos;
private Terrain TActive;
private int PlayerPosCase;
// Use this for initialization
void Start () {
TActive = TRef;
TPos = TActive.transform.position;
}
// Update is called once per frame
void Update () {
for (PlayerPos = Player.transform.position; PlayerPos.x != 0 PlayerPos.z != 0;) {
TPos.x = TPos.x - PlayerPos.x;
TPos.z = TPos.z - PlayerPos.z;
PlayerPos.z = 0;
PlayerPos.x = 0;
TActive.transform.position = TPos;
TRef = TActive;
Player.transform.position = PlayerPos;
}
}
}
When you just change the position of the terrain, you’re just transporting it from one position to another. There’s no transition between the old position and the new position.
It’s like it’s teleporting in very small increments rather than moving.
Whenever you want something to move in a smooth manner, I believe you want to use Translate.
You’ve said you found another way of doing what you want, but I will still offer up an idea. Perhaps it will be useful to you.
Don’t move the player’s position at all. Instead, when you detect the player presses a movement key/button, move the terrain in the opposite direction of how you’d move the player. So when they press the foward key, move the terrain backwards, etc.
Why move the terrain? Isn’t easier to move the FP controller? I’ve tried once to paint something on the terrain at runtime and the frame rate dropped to a stand still picture.
@Gilley, thank you, that’s actually very helpful. I had not seen Translate yet.
@Scarpelius, I’m working on a couple prototypes to accompany a procedural terrain engine I’m having much more trouble working on. The idea was to keep the player centralized and the terrain moving while caching out of range terrain blocks to avoid ridiculous floating points resulting in minecraft style Far Lands.