- Home /
How to check the rotation
i gor a script for my animals so they can walk around the world but the problem is my rigidbody. i need it for my hp script so the animals can be killed but if u throw something at them, they fall down. i wanted to make a script wich checks if the rotation on the z is under or above 0, if the number is above or under 0 it should change the number to 0 back so the animal is not able to fall down but i dont know how to do it, can somebody help me?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Animal : MonoBehaviour { public float movementspeed = 3f; public float rotspeed = 100f;
public Rigidbody rb;
private bool iswandering = false;
private bool isrotleft = false;
private bool isrotright = false;
private bool iswalking = false;
private void Update()
{
if (iswandering == false)
{
rb.isKinematic = false;
rb.detectCollisions = true;
StartCoroutine(Wander());
}
if (isrotright == true)
{
rb.isKinematic = true;
rb.detectCollisions = false;
transform.Rotate(transform.up * Time.deltaTime * rotspeed);
}
if (isrotleft == true)
{
rb.isKinematic = true;
rb.detectCollisions = false;
transform.Rotate(transform.up * Time.deltaTime * -rotspeed);
}
if (iswalking == true)
{
transform.position += transform.forward * movementspeed * Time.deltaTime;
}
}
IEnumerator Wander()
{
int rottime = Random.Range(1, 3);
int rotwait = Random.Range(1, 4);
int rotatelorR = Random.Range(0, 3);
int walkwait = Random.Range(1, 4);
int walktime = Random.Range(1, 5);
iswandering = true;
yield return new WaitForSeconds(walkwait);
iswalking = true;
yield return new WaitForSeconds(walktime);
iswalking = false;
yield return new WaitForSeconds(rotwait);
if (rotatelorR == 1)
{
isrotright = true;
yield return new WaitForSeconds(rottime);
isrotright = false;
}
if (rotatelorR == 2)
{
isrotleft = true;
yield return new WaitForSeconds(rottime);
isrotleft = false;
}
iswandering = false;
}
}
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Rotating A Camera Back and Forth 1 Answer
Rotation Numbers Are Off? 0 Answers
Realtime cubemap that respects rotation? 0 Answers