- Home /
Question by
omnidream · Jul 24, 2020 at 02:55 PM ·
scripting problemaimoving
How to make Ais move randomly and smooth
Hi! I have a few small boats controlled by this script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AiControlls : MonoBehaviour
{
private Rigidbody rb;
public GameObject FrontRadar;
public GameObject LeftRadar;
public GameObject RightRadar;
public float rotationSpeed = 0.75f;
public float speed = 0.75f;
void Start()
{
rb = GetComponent<Rigidbody> ();
}
void FixedUpdate()
{
int rnd = Random.Range(0, 10);
if(rnd == 0)
TurnLeft();
else if (rnd == 1)
TurnRight();
else
MoveForward();
}
int ARandomInt()
{
int rnd = Random.Range(0, 10);
return rnd;
}
void TurnLeft()
{
while(KeepDirection())
{
transform.Rotate(0, -rotationSpeed, 0, Space.Self);
}
}
void TurnRight()
{
while(KeepDirection())
{
transform.Rotate(0, rotationSpeed, 0, Space.Self);
}
}
void MoveForward()
{
while(KeepDirection())
{
Vector3 movement = new Vector3 (-1f, 0.0f, 0.0f);
rb.AddRelativeForce((movement * (speed*2000)));
}
}
bool KeepDirection()
{
bool keepTurning = true;
while(Random.Range(0, 4) == 0)
{
keepTurning = false;
}
return keepTurning;
}
}
It works as far as I get them to move around but their movement are VERY jerky. It´s like they can´t decide where to go. What I am asking for is really two things: 1. Can I use this script with improvements? 2. If question 2 != true, how should I do this?
Many thanks forehand!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613003950im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
OnTriggerEnter called only one time?!! 5 Answers
More Efficient way? 1 Answer
Wandering AI "ignores" speed 2 Answers
I need help for an animation to be played when my funciton is called. 0 Answers