rotating an enemy gameobject

Im trying to find a way to rotate an enemy back and forth between 2 points and not rotate him all the way around. Ive tried using Quaternion.RotateTowards but i cant quite figure out how to get that to work. Any help would be appreciated.

bump

http://unity3d.com/support/documentation/ScriptReference/Transform.LookAt.html

You could use Transform.LookAt ();

and srry i said enemy in the original post but its actually a “friendly npc” in my game.

i thought about that except i dont want them to stare at one specific transform. I need them to look back and forth between 2 transforms. basically i need the npc to rotate back and forth. this npc has a raycast hes casting forward. and if his raycast ever crosses an gameobject tagged as enemy then he will shoot.

I did this for learning purpose. Maybe it can help you : http://forum.unity3d.com/threads/98337-AddForce-and-Time.deltaTime: http://forum.unity3d.com/threads/98337-AddForce-and-Time.deltaTime

i dont think that script will work because i think that script will make my npc rotate 360 degrees.

I’m still a Unity newbie too, so I don’t know if this is the easiest way, but it ought to work:

  1. Add a member variable in your NPC script to keep track of its “target” point — i.e., the next waypoint in its patrol. When it reaches point A, set this to point B, and vice versa.

  2. On each frame, make a new Quaternion and call SetLookRotation to make it represent the orientation of your NPC looking at the waypoint. Something like this (in C#):

Quaternion q = new Quaternion();
q.SetLookRotation(nextWaypoint - transform.Position);
  1. OK, now, if your NPC’s rotation isn’t already the same as q, then you want to rotate towards it, but at no more than a certain speed (i.e. amount per frame). Use Quaternion.RotateTowards for that, e.g.:
transform.rotation = Quaternion.RotateTowards(transform.rotation, q, 90*Time.deltaTime);

This code is untested, but should at least get you pointed in the right direction!

Cheers,

  • Joe

awesome i will try that and get back to you.

Airmand

I just did this and it works fine, a bit more organic if you want…

using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour {

    bool lookingLeft;

	void Start () {

        lookingLeft = true;
	}

	void Update () {

        if (transform.rotation.eulerAngles.z <5)
        {
            lookingLeft = true;
        }
        else if (transform.rotation.eulerAngles.z > 240)
        {
            lookingLeft = !lookingLeft;
        }

        if (lookingLeft)
        {
            transform.Rotate(0, 0, 1);
        }
        else
        {
            transform.Rotate(0, 0, -1);
        }
        
	}
}

Joe - i couldnt quite figure out how to get yours to work, but i also have very litttle experience with C#.

chubbbspet : thank you for this it works great i just need to fix the axis he rotates on now.

Sorry, I forgot to ask about your platform. You can obviously expand on this a lot and even make it semi - AI, you know. E.g if you make the left and right rotation values random - basically just incorporate some random values (or the rotation speed) to make it a bit more unpredictable, i guess.

ah for this level its top down. you as the player have two friendly who sit beside you rotating. If your friendlys raycast hit an enemy they will fire a shot. im just trying to make them rotate in the correct direction right now. Having trouble figuring out the way the angles read. like if its torward a world point or what.

rotation.eulerAngles is the actual rotation that you would see in the Unity editor. What I suggest is to rotate your object in the editor and note down the values, that is how I did mine above. Let us know if you need more help.

ah ok good to know. Thank you alot for the help. I did finally get it how i need it though and now it works perfectly. Only question was if theres a way to slow down the speed that he rotates.

transform.Rotate(0, 0, 1);

change the above line of code by chubbspet to transform.Rotate(0, 0, 1*Time.deltaTime);

if u think its too slow change tht 1 to some higher value but dont forget to add Time.deltaTime… if u dont have it, the rotation will vary from one system to other…

ah thats what i figure it was i guess i should have just tried that. thanks though.

Yup, Flamy is right, (0,0,1) is not the right way, since that will react differently according to the computer’s processing speed. Best is to replace the “1” with a public variable like rotSpeed, then you can fine tune it in the editor while your game is running and then like suggested time that variable with Time.deltaTime