Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by gabbobersi · Oct 14, 2020 at 03:36 PM · transform.positionrandom.rangetime.deltatime

2D - Move an object

I'm trying to implement a function which allow IA to move an object randomly (smoothly, NOT like from a point to point directly without seeing that the object is moving).

My question is if I'm coding movements right. This is just the movement parts which I'm having doubts about:

 //Moving towards right
 transform.position += transform.right * movementSpeed * Time.deltaTime;
 //Moving towards left
 transform.position -= transform.right * movementSpeed * Time.deltaTime;
 //Moving upwards
 transform.position += transform.up * movementSpeed * Time.deltaTime;
 //Moving downwards
 transform.position -= transform.up * movementSpeed * Time.deltaTime;

I'm asking this question, because seems that after few movements, the object is starting to make small movements in random direction (like if the transform.position is = 1).

If it can help, here the function complete:

 public class Movement_Worker : MonoBehaviour
 {
     public float movementSpeed;
     public int limiteStessaDirezione = 15;
     bool UP = false;
     bool DOWN = false;
     bool RIGHT = false;
     bool LEFT = false;
     bool FERMO = false;
 
     //sceltaMovimento indicates if we can choose a new direction or not  
     bool sceltaMovimento = true;
     //i_tipoMovimento is just a counter.
     int i_tipoMovimento = 0;
 
    
     void FixedUpdate()
     {
         sceltaMov();
     }
 
     private void sceltaMov()
     {
         Debug.Log("tipoMovimento = " + sceltaMovimento);
         int scelta = Random.Range(1, 6);
 
         if (i_tipoMovimento >= limiteStessaDirezione)
         {
             sceltaMovimento = true;
             i_tipoMovimento = 0;
         }
             
         if (sceltaMovimento == true)
         {
             switch (scelta)
             {
                 case 1:
                     //RIGHT
                     transform.position += transform.right * movementSpeed * Time.deltaTime;
                     RIGHT = true;
                     sceltaMovimento = false;
                     break;
                 case 2:
                     //LEFT
                     transform.position -= transform.right * movementSpeed * Time.deltaTime;
                     LEFT = true;
                     sceltaMovimento = false;
                     break;
                 case 3:
                     //UP
                     transform.position += transform.up * movementSpeed * Time.deltaTime;
                     UP = true;
                     sceltaMovimento = false;
                     break;
                 case 4:
                     //DOWN
                     transform.position -= transform.up * movementSpeed * Time.deltaTime;
                     DOWN = true;
                     sceltaMovimento = false;
                     break;
                 default:
                     //DONT MOVE
                     //transform.position = transform.position;
                     FERMO = true;
                     sceltaMovimento = false;
                     break;
             }         
         }
         //Repeat same movement X times
         while(sceltaMovimento == false)
         {
             if (FERMO == true)
             {
                 i_tipoMovimento += 1;
                 break;
             }
             else if (RIGHT == true)
             {
                 i_tipoMovimento += 1;
                 transform.position += transform.right * movementSpeed * Time.deltaTime;
                 break;
             }
             else if (LEFT == true)
             {
                 i_tipoMovimento += 1;
                 transform.position -= transform.right * movementSpeed * Time.deltaTime;
                 break;
             }
             else if (UP == true)
             {
                 i_tipoMovimento += 1;
                 transform.position += transform.up * movementSpeed * Time.deltaTime;
                 break;
             }
             else if (DOWN == true)
             {
                 i_tipoMovimento += 1;
                 transform.position -= transform.up * movementSpeed * Time.deltaTime;
                 break;
             }
         }
     }
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by VeryAnnoyingCat · Oct 14, 2020 at 05:41 PM

I think that you are trying to move a gameobject in a certain direction. This script may be useful:

 public float direction = 45; //in degrees (plus 270 degrees)
 public float speed = 2; //in Units per second
 
 private float directionInRadians;
 private Vector3 movementPerTimestepVector;
 void FixedUpdate()
 {
     directionInRadians = direction * Mathf.Deg2Rad;
     movementPerTimestepVector = new Vector3(Mathf.Cos(directionInRadians), Mathf.Sin(directionInRadians), 0) * speed * Time.deltaTime;
     transform.position += movementPerTimestepVector;
 }

It moves the gameobject in a certain direction at a fixed speed. You can set the direction to random.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gabbobersi · Oct 15, 2020 at 07:09 PM 0
Share

I tried (and re arrange) your script. If I randomize the direction (from 0 to 360), 2d object start moving like a "mosquito" (random every frame) and is not what I'm trying to achieve. I'd like to maintain a certain random direction for a bit (like in a more human way) and maybe to stop for a while (randomly).

The problem with my script above is that after few change in direction (for eg. object move a bit to the right, then a bit to the top ecc...), object start moving JUST one step in every random position every frame, like a "robot mosquito" (sorry for the example). How can I achieve the correct random movement?

avatar image gabbobersi gabbobersi · Oct 15, 2020 at 07:25 PM 0
Share

Ok using your script I finished with this script which is working as I expected:

 public float direction = 45; //in degrees (plus 270 degrees)
     public float speed = 8; //in Units per second
     public int directionCounter = 0;
     private float directionInRadians;
     private Vector3 movementPerTimestepVector;
     private bool youCan$$anonymous$$ove = true;
     void FixedUpdate()
     {
         if(directionCounter == 50)
         {
             //Change random direction
             direction = Random.Range(0, 360);
             directionCounter = 0;
             //Change the flow of function between moving or stop
             if (youCan$$anonymous$$ove == true)
                 youCan$$anonymous$$ove = false;
             else youCan$$anonymous$$ove = true;
         }
         if(youCan$$anonymous$$ove == true)
         {
             //$$anonymous$$oving in ONE random direction
             directionCounter++;
             directionInRadians = direction * $$anonymous$$athf.Deg2Rad;
             movementPerTimestepVector = new Vector3($$anonymous$$athf.Cos(directionInRadians), $$anonymous$$athf.Sin(directionInRadians), 0) * speed * Time.deltaTime;
             transform.position += movementPerTimestepVector;
         }
         else
         {
             //Dont move here
             directionCounter++;
 
         }
         
     }

Thank you very much for your help!

avatar image Ady_M gabbobersi · Oct 16, 2020 at 03:08 AM 0
Share

It's good that you solved your problem, but I wouldn't recommend that you use frames to time it, not even inside FixedUpdate. Use seconds.
Try this instead:

 public float speed = 8; // in Units per second

 public float directionAngle = 0; // in degrees (plus 270 degrees)
 public float directionDuration = 1; // Seconds to wait before changing direction
 private float directionTimeRemaining; // Seconds remaining

 void FixedUpdate()
 {
     directionTimeRemaining -= Time.deltaTime;
     if (directionTimeRemaining <= 0)
     {
         // Change random direction
         directionTimeRemaining = directionDuration;
         directionAngle = Random.Range(0, 360);
     }

     // $$anonymous$$ove
     transform.position += Quaternion.Euler(0, 0, -directionAngle) * Vector3.right * speed * Time.deltaTime;
 }
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

174 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Would i need Time.deltaTime if am just incrementing transform.position 1 Answer

How to fix box drop via code animation? 2 Answers

Transform player to RaycastHit.point depending on speed float 1 Answer

Generating an object in a random y position? c# 1 Answer

moving objects with transform position 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges