Hello again
I have a new problem.
I want my “snail” to come up from the ground and then wander as usual. The code I have written now, is not working. The snail will only show up under the ground and then it stays there and wander under the ground.
What am I doing wrong?
Ok, so I have now changed the code and now the snail is comming up from ground but when it is nearly up, it shoots up like its been shoot out of a cannon. !?
using UnityEngine;
using System.Collections;
public class PlaceRandomSnails : MonoBehaviour {
[Header("Setup")]
[SerializeField]
[Range(1, 20)]
private int snailsInsideArea = 5;
[SerializeField]
//[Range(100,200)]
private float spawnRadius = 5.0f;
[SerializeField]
private Transform playerTransform;
[SerializeField]
[Range(1, 100)]
private int aChanceOfXToSpawn = 1;
[SerializeField]
private GameObject Snail;
private static int spawnedSnails = 0;
private float speed = 0.01f;
// Use this for initialization
void Start() {
}
// Update is called once per frame
void Update() {
if (spawnedSnails < snailsInsideArea) {
float posX, posY, posZ;
posX = Random.Range(
(playerTransform.position.x - spawnRadius),
(playerTransform.position.x + spawnRadius)
);
posZ = Random.Range(
(playerTransform.position.z - spawnRadius),
(playerTransform.position.z + spawnRadius)
);
posY = Terrain.activeTerrain.SampleHeight(new Vector3(posX, 0, posZ));
Vector3 position = new Vector3(
posX,
posY -= 1.5f,
posZ
);
GameObject go = (GameObject) Instantiate(Snail, position, Quaternion.Euler(new Vector3(-90, 0, 0)));
go.transform.GetComponent<Rigidbody>().useGravity = false;
spawnedSnails++;
}
}
}
And then in every Snail:
using UnityEngine;
using System.Collections;
public class Snail01Controller : MonoBehaviour {
[SerializeField]
[Range(0.01f, 1.0f)]
private float speedOfSnail = 0.01f;
private float moveDistance = 0f;
private float currentMoves = 0;
private float currentPosition;
private Vector3 currentDirection;
private float posY;
// Use this for initialization
void Start () {
moveDistance = Random.Range(100, 1000);
currentDirection = new Vector3( Random.Range(-1.0f, 1.0f) , 0.0f, Random.Range(-1.0f, 1.0f) );
posY = Terrain.activeTerrain.SampleHeight(new Vector3(this.transform.position.x, 0, this.transform.position.z));
}
// Update is called once per frame
void Update () {
if (this.transform.position.y >= posY) {
this.transform.GetComponent<Rigidbody>().useGravity = true;
Quaternion qTo = Quaternion.LookRotation(currentDirection);
transform.rotation = Quaternion.Slerp(transform.rotation, qTo, speedOfSnail * Time.deltaTime);
if (currentMoves < moveDistance) {
transform.Translate(Vector3.down * speedOfSnail * Time.deltaTime);
currentMoves++;
}
if (currentMoves >= moveDistance) {
moveDistance = Random.Range(100, 1000);
currentDirection = new Vector3(Random.Range(-1.0f, 1.0f), 0.0f, Random.Range(-1.0f, 1.0f));
currentMoves = 0;
}
} else {
// Om Snail inte är ovan mark
transform.Translate(Vector3.forward * speedOfSnail * Time.deltaTime);
}
}
}
I have figured it out!. I set collider to false as long as the collider is under the terrain. and when it gets ontop of terrain, it is set to true again. It works