logo

Perlin_Movement

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
//------------------------------------------------------------------------
//Perlin Noise Agents v1.0 - Perlin Noise Class
//--Class Controls the movements of the agents--Class lives in the SoloAgent Prefab which is used to instantiate--All behavioral changes must reference or update values from this class
//Mode Lab - Written by Luis Quinones
//------------------------------------------------------------------------
public class PerlinNoiseMovement : MonoBehaviour
{
    public float minSpeed = 0.1f;
    public float noiseScale = 75.0f;
    public float noiseStrength = 8.5f; 
    public bool proceed = true;
    private int offsetStep = 10000;
    public bool dimension;
    void Start(){

    }    
    void Update()
    {
        movement();
    }
    void movement()
    {
        if (proceed) {
            Transform currentPos = GetComponent<Transform>();
            if(dimension){ //2D NOISE
                float noiseVal = Mathf.PerlinNoise(currentPos.position.x / noiseScale, currentPos.position.z / noiseScale) * noiseStrength; //Get 2D Noise Value
                float angle = noiseVal;
                Vector3 newDirection = new Vector3(Mathf.Cos(angle), 0, Mathf.Sin(angle)); //Create Vector for new Direction
                Vector3 speed = new Vector3(Random.Range(-minSpeed, minSpeed), 0, Random.Range(-minSpeed, minSpeed)); //Vector for speed
                newDirection += speed;
                transform.Translate(newDirection); //Translate the position to the newPosition using the new vector

            }else if(dimension == false){ 

                float noiseVal = Mathf.PerlinNoise(currentPos.position.x / noiseScale, currentPos.position.z / noiseScale) * noiseStrength;
                float noiseValY = Mathf.PerlinNoise(currentPos.position.x / noiseScale + offsetStep, currentPos.position.y / noiseScale) * noiseStrength;
                
                float angle = noiseVal;
                float angleY = noiseValY;
                
                Vector3 newVec = new Vector3(0,0,0);
                
                newVec.x += Mathf.Cos (angleY) * Mathf.Cos (angle);
                newVec.y += Mathf.Cos (angleY) * Mathf.Sin (angle);
                newVec.z += Mathf.Sin (angleY);
                
                Vector3 newDirection = new Vector3(Mathf.Cos (angleY) * Mathf.Cos (angle), Mathf.Cos (angleY) * Mathf.Sin (angle), Mathf.Sin(angleY));
                Vector3 speed = new Vector3(Random.Range(-minSpeed, minSpeed), Random.Range(-minSpeed, minSpeed), Random.Range(-minSpeed, minSpeed));
                newDirection += speed;
            
                transform.Translate(newDirection);
            }
        }
    }    
}
Update
  • Share