Question by
DroidifyDevs · Apr 17, 2016 at 03:32 PM ·
c#scripting problemcartransform.lookatbots
How to limit rotation speed when using Transform.LookAt?
Hi!
So I'm making bot cars for my game where they drive towards the player and chase him. Here's the script:
using UnityEngine;
using System.Collections;
public class BOTAI : MonoBehaviour {
private Rigidbody rb;
public int AddForceAmount;
public int bothealth;
public Transform PlayerToLookAt1;
public Transform PlayerToLookAt;
// Use this for initialization
void Start ()
{
AddForceAmount = 2000;
PlayerPrefs.SetInt("AddForceAmount", AddForceAmount);
rb = GetComponent<Rigidbody>();
PlayerToLookAt = GameObject.FindWithTag("Player").transform;
PlayerPrefs.SetInt("CarID", 2);
}
// Update is called once per frame
void Update ()
{
if (PlayerPrefs.GetInt ("BotLife") == 1)
{
AddForceAmount = PlayerPrefs.GetInt ("AddForceAmount");
rb.AddForce (transform.forward * AddForceAmount);
transform.LookAt(PlayerToLookAt);
transform.LookAt(new Vector3(PlayerToLookAt.transform.position.x, transform.position.y, PlayerToLookAt.transform.position.z));
rb.AddForce (transform.forward * AddForceAmount);
//Debug.Log("BOT looking at CarID3");
}
}
}
Problem is, if the player gets rammed and ends up on top of one of the bots, the bots can spin instantly, looking very bad. Or if the player drives past a bot, the bot will instantly turn around. This looks very bad and cheap in the project. How can I limit the amount of degrees the bot can turn per second?
Or if there is a better way let me know, I'm open to all ideas.
Thanks :)
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612075023im_/https://answers.unity.com/themes/thub/images/avi.jpg)