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 Bradwall · Nov 25, 2021 at 05:12 AM · rigidbodytransform.positioninvokerepeating

Slow down invoke repeating/rigidbody.transform.position?

So I've been working on this movement script for a really long time, remaking it in several different ways, and I have the following code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class moveCows : MonoBehaviour
 {
     public float speed;
     public Rigidbody rb;
 
     private int vert;
     private int horiz;
 
     public float delay;
 
     // Update is called once per frame
     void Start()
     {
         InvokeRepeating("PickRandom", 0.0f, delay);
     }
 
     void Update()
     {
         rb.MovePosition(transform.position + new Vector3(horiz, vert, 0.0f) * speed * Time.deltaTime);
     }
 
     public void PickRandom()
     {
         vert = Random.Range(-1, 1);
         horiz = Random.Range(-1, 1);
     }
 }


Basically what I'm trying to get it to do is pick random directions (vert and horiz) and then smoothly move the gameobject but not too fast. What I have right now, no matter what I do, makes the cows move insanely fast. Anybody know how to slow it down?

Comment
Add comment · Show 2
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 logicandchaos · Nov 25, 2021 at 10:38 AM 0
Share

have you tried decreasing the speed or increasing the delay?

avatar image Bradwall logicandchaos · Nov 25, 2021 at 06:48 PM 0
Share

logicandchaos, I have tried decreasing the speed, and while they do move a little slower, when I press play/run, they go shooting off to the sides until they hit a collider. Increasing the delay doesn't do anything because I'm not totally sure that the vert and horiz values are changing. Unfortunately I'm not sure how to fix either of those. Any suggestions would be welcome. Thanks.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by JethRow · Nov 25, 2021 at 07:38 PM

Hello, i think you should start by changing the code, try something like this

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class moveCows : MonoBehaviour
 {
     public float speed;
     public Rigidbody rb;
     
     private int vert;
     private int horiz;
 
     public float delay;
 
     // Update is called once per frame
     void Start()
     {
         InvokeRepeating("MoveCows", 0.0f, delay);
     }
 
     void MoveCows()
     {
         PickRandom();
         rb.velocity=(new Vector3(horiz, 0f, vert) * speed * Time.deltaTime);
         
     }
 
     public void PickRandom()
     {
         rb.velocity=0f;
         vert = Random.Range(5, -5);
         horiz = Random.Range(5, -5);
     }
 }

Also keep in mind when cow is choosing the Vector it will choose it in World, this is good if you want to make sure cows move in a certain radius, as for example Vector3(10,10,10) is one point in the world, so you can set that two components between (10,-10) and cow will always stay in that 20m point

Oh and yea, in order for this to work nice you should add physic material to the cow and set its friction to 0, for example in your assets press right click->create->Physic material and call it slippery, now click that material and go to inspector and set friction to 0 for both static and dynamic and just drag and drop it on your cow!

Alsooo keep in mind that i changed the "new Vector3" from(horiz,vert,0) to (horiz,0,vert) as it worked better for my test to help you with this :D

and yea, even though this will work, in future i would HIGHLY recommend you start using NavMeshAgents becausue of their SetDestination function, it runs perfect and you can adjust all of the values from the inspector, This is the workflow: You create cow, add NavMeshAgent to it, create a script on it and type in the using part up in the beggining "using UnityEngine.AI" that will allow you to use NavMesh functions, then u can set NavMesh for the cow for example "Public NavMeshAgent Cow" save it, go to inspector and drag in navmeshagent of a cow to Cow tab, then make a function that randomizes the point in the world as a "transform.position" and then in the Move() function u can just set Cow.SetDestination(transform.position) <--- this transform.position is the point on the world you have randomized,and finally you need to bake NavMesh(that will tell the cow what is walkable and what is not, and it is a MUST for all NavMeshAgents), for Navigation tab(where u bake navmesh) to open you need to go to first row of dropdowns,there you will see window,then go to AI, and hit navigation,and you will get Navigation window where u can bake ur map, it is That easy, trust me, and it works the best for all NPC's that you want to wander around ,move , chase you and even attack you, go on and check that out when u get some free time, Good luck!

Blockquote

Comment
Add comment · 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

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

183 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Game Object seems to move on it's own 0 Answers

Gravity is not working no matter what 0 Answers

Confusion in deciding the units for game objects 1 Answer

Getting error transform.position assign attempt for "Player" is not valid on rigidbody 2 Answers

my fps hand doesn't grab propperly 0 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