Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Random movement of a sprite in 2D

    • 0
    • 9
    • 0
    • 0
    • 4
    • 0
    • 0
    • 0
    • 6.11k
    Comment on it

    When we develop a game sometimes we need gameobject move randomly without any user input. In this script a sprite randomly move in 2D and also rotate in moving direction.

    using UnityEngine;
    using System.Collections;
    
    public class move : MonoBehaviour 
    {
        public float moveSpeed ;
        public Vector3 dir;
        public float turnSpeed;
        float targetAngle;
        Vector3 currentPos;
        bool play=true;
        Vector3 direction;
        void Start()
        {
            dir = Vector3.up;
            InvokeRepeating ("Start1", 0f, 5f);
        }
        void Start1(){
            play = true;
            direction = new Vector3(Random.Range(-3.0f,3.0f),Random.Range(-4.0f,4.0f),0); //random position in x and y
        }
    
        void Update()
        {
             currentPos = transform.position;//current position of gameObject
            if(play)
            { //calculating direction
            dir = direction - currentPos;  
        
            dir.z = 0;
            dir.Normalize ();
                play = false;
            }    
    
            Vector3 target = dir * moveSpeed + currentPos;  //calculating target position
            transform.position = Vector3.Lerp (currentPos, target, Time.deltaTime);//movement from current position to target position
             targetAngle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 90; //angle of rotation of gameobject
            transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.Euler (0, 0, targetAngle), turnSpeed * Time.deltaTime); //rotation from current direction to target direction
        }
        void OnCollisionEnter2D()
        {
            
                CancelInvoke ();//stop call to start1 method
                direction = new Vector3 (Random.Range (-3.0f, 3.0f), Random.Range (-4.0f, 4.0f), 0); //again provide random position in x and y
                play = true;
        
        }
        void OnCollisionExit2D()
        {
            InvokeRepeating ("Start1", 2f, 5f);
        }
    }

     

 4 Comment(s)

  • This is my solution for circle random movement:
     
    using System.Collections;
    using UnityEngine;
    public class RandomMove : MonoBehaviour
    {
     public float moveSpeed;
        Vector3 target;
        public float SpeedOfChangingTarget = 5;
        private float camHeight;
        private float camWidth;
        private float circleRadious = 2;
        SpriteRenderer spriteRenderer;
        void Start()
        {
            Camera cam = Camera.main;
            camHeight = 2f * cam.orthographicSize;
            camWidth = camHeight * cam.aspect;
            
            spriteRenderer = GetComponent<SpriteRenderer>();
            
            InvokeRepeating("GenerateNewTarget", 0f, SpeedOfChangingTarget);
        }
    
        void Update()
        {
            circleRadious = spriteRenderer.bounds.size.x / 2;
            Vector3 currentPos = transform.position;
            if (currentPos == target)
            {
                GenerateNewTarget();
            }
            transform.position = Vector3.MoveTowards(currentPos, target, Time.deltaTime*moveSpeed);//movement from current position to target position
       }
        void OnCollisionStay2D()
        {
            GenerateNewTarget();
        }
        void GenerateNewTarget()
        {
            target = new Vector3(Random.Range(-(camWidth/2-circleRadious), camWidth/2-circleRadious), Random.Range(-(camHeight/2-circleRadious), camHeight/2-circleRadious), 0); //again provide random position in x and y
    
        }
    }
    
Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: