- Home /
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?
have you tried decreasing the speed or increasing the delay?
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.
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