- Home /
Rotating a GameObject 180 to face another Gameobject on single axis
Hi people, im making a top down game where u can feed fish, the fish get hungry after a certain amount of time and then i can click somewhere to spawn some food and the fish goes and get it, the problem is when i spawn it behind him it swims backwards to the food, so I'm trying to rotate the fish only when its not facing the food. Here's the code im using now: using UnityEngine; using System.Collections;
public class VisBeweging : MonoBehaviour
{
public float speed;
public GameObject muntje;
public GameObject muntje2;
public GameObject voedsel;
public bool honger;
public bool dood;
public Vector3 richting = Vector3.down;
public Texture whiteTexture;
public Vector3 visPos;
void Start ()
{
Invoke ("HeeftHonger", Random.Range (10,20)); //start heefthonger tussen 15 en 30 seconden
Invoke ("Grow", 30); //start grow na 30 seconden
Invoke ("Turn", Random.Range (15, 30)); //start turn tussen 15 en 30 seconden
transform.Rotate(0, 0, 90); //draai de prefab 90 graden eenmalig
speed = Random.Range (2f, 5f); //snelheid is random waarde tussen 2 en 5
}
void FixedUpdate ()
{
visPos = transform.position; //vispostie is plek waar die nu is
if(dood == false && honger == true && VoedselSpawn.voedselCount > 0) //als hij leeft en honger heeft en er voedsel aanwezig is
{
transform.position = Vector3.MoveTowards(visPos, VoedselScript.foodPos, 10 * Time.deltaTime); //beweeg naar het voedsel
}
if(dood == false)
{
transform.Translate (richting * speed * Time.deltaTime); //Beweging van links naar rechts als hij leeft
}
if(dood == true && transform.position.z < 19) //als hij dood is en in het veld
{
richting = Vector3.forward; //beweeg naar boven
transform.Translate (richting * speed * Time.deltaTime);
gameObject.renderer.material.mainTexture = whiteTexture; //laad witte texture
gameObject.renderer.material.color = new Color(1, 1, 1, 1); //kleur word wit
Invoke ("Destroy", 5);
if (transform.position.z > 19) //als hij bovenaan komt
{
speed = 0;
gameObject.renderer.material.color = new Color(1, 1, 1, 1); //kleur is boven nog steeds wit
}
}
}
public void OnTriggerEnter (Collider other)
{
if(other.tag == "Grenslijn") //als je de grenslijn raakt
{
transform.RotateAround (transform.position, transform.forward, 180f); //draai om
}
if(honger == true && other.tag == "Voedsel") //als je honger hebt en je raakt voedsel aan
{
honger = false; //geen honger meer
gameObject.renderer.material.color = new Color(1, 1, 1, 1); //kleur word oranje
Invoke ("HeeftHonger", Random.Range (15,30)); // en krijgt tussen 15 en 30 sec weer honger
CancelInvoke ("Doodanimatie"); //hoeft niet meer dood te gaan
Invoke ("Changetag",0.01f); //verander tag met 0.1 sec vertraging
}
}
void Changetag()
{
gameObject.tag = "Visje";
}
void Doodanimatie ()
{
dood = true;
CancelInvoke ("Spawnmuntje");
CancelInvoke ("HeeftHonger");
CancelInvoke ("Grow"); //hij is dood dus cancel alle timers
CancelInvoke ("Turn");
CancelInvoke ("Spawnmuntje2");
}
void Destroy()
{
Destroy (gameObject);
}
void HeeftHonger ()
{
gameObject.renderer.material.color = new Color(0, 1, 0); // is hij groen
honger = true; //heeft honger
Invoke ("Doodanimatie", 30); //start doodanimatie
gameObject.tag = "Zieke Vis"; //is ziek
}
void Spawnmuntje ()
{
Vector3 muntSpawnpos = transform.position; //spawnpositie muntje is positie van vis
muntSpawnpos.z -= 2f; //iets eronder
muntSpawnpos.y = Random.Range(8,10); //random hoogte
Instantiate (muntje, muntSpawnpos, transform.rotation); //spawn het muntje
Invoke ("Spawnmuntje", Random.Range (6,10)); //na 6 - 10 sec weer
}
void Grow ()
{
transform.localScale = new Vector3(1.5f, 1.5f, 1.5f); //word 1.5x zo groot
Invoke ("Spawnmuntje", Random.Range (6,10)); //start spawnen van grijze muntje
Invoke ("Grow2", 60); //groei na 60 seconden weer
}
void Grow2 ()
{
transform.localScale = new Vector3 (2, 2, 2); //word 2x zo groot
CancelInvoke("Spawnmuntje"); //cancel spawn grijze muntje
Invoke ("Spawnmuntje2", Random.Range (6,10)); //spawn gouden muntje
}
void Spawnmuntje2 ()
{
Vector3 muntSpawnpos = transform.position; //spawnpositie muntje is positie van vis
muntSpawnpos.z -= 10f; //iets eronder
muntSpawnpos.y = Random.Range (8, 10); //random hoogte
Instantiate (muntje2, transform.position, transform.rotation); //spawn het gouden muntje
Invoke ("Spawnmuntje2", Random.Range (6,10)); //na 6 - 10 sec weer
}
void Turn ()
{
transform.RotateAround (transform.position, transform.forward, 180f); //draai om
Invoke ("Turn", Random.Range (15, 30)); //draai weer om tussen 15 en 30 seconden
}
}
I think you want to look into AI behaviours, moving towards a point: http://gamedevelopment.tutsplus.com/tutorials/understanding-steering-behaviors-flee-and-arrival--gamedev-1303
Your answer
Follow this Question
Related Questions
Rotate to Center of Screen 2 Answers
LookAt() and RotateAround() 1 Answer
Instantiated Object - Look At. 1 Answer
Simple rotations question 2 Answers
360° Roation in one min 1 Answer