Search Unity

[c#] Make enemy always look at you while rotating. Code overlaping?

Discussion in 'Scripting' started by Deleted User, Jul 29, 2014.

  1. Deleted User

    Deleted User

    Guest

    Hello:

    I have a quad/plane that always is staring at the player, with this code (in the Update):

    Code (CSharp):
    1. transform.rotation = Camera.main.transform.rotation;//Always look at the player
    It works. Now, I want that that quad/plane rotates, using in the Update:

    Code (CSharp):
    1. this.transform.Rotate(Vector3.forward * Time.deltaTime * 100);
    There is an overlapping in the code. I can't make rotate the object and, at the same time, make it look at the player (FP view). Is one or another and I need both.

    I need that the object (a quad) looks at me constantly because if not, if I walk pass to it, it won't be rendered (only one normal, is 2D)

    Could you help me to solve this?



    UGLY SOLUTION:
    I created an empty object, and putted the quad as son. The quad has the code for the rotation, while the father (the empty one) has the code for the look at the player. Is a dirty solution. Any more elegant one?
     
    Last edited by a moderator: Jul 29, 2014
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Your "ugly solution" is perfectly fine, honestly. The other option would be to store the "offset" of the value. In other words, instead of directly rotating the object when you want it rotated, add some value to myRotation.
    Code (csharp):
    1.  
    2. void Update() {
    3. transform.rotation = Camera.main.transform.rotation;
    4. transform.Rotate(Vector3.forward * myRotation);
    5. }
    6.