Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

LookAt only on Z-axis

Discussion in 'Scripting' started by Anon1234, Dec 1, 2014.

  1. Anon1234

    Anon1234

    Joined:
    Feb 17, 2014
    Posts:
    78
    Yea yea, this has been probably the most spammed topic ever, BUT, I have good reason to make another one, I actually went for like 4/5 links on Unity and they didn't provide solution.

    My game is very basic, there's zombie model over there, there's player controller first-person, so I added script to zombie, which was

    Code (CSharp):
    1. public Transform Target;
    2. transform.LookAt(Target);
    It's no doubt it's correctly placed, it works. But when I add ridigbody and freeze it on certain direction, it doesn't work. There was moderator saying, that if you put LookAt in code, the "restriction" in rigidbody has nothing to say, but I don't know how else may I make model look at player.
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Code (csharp):
    1. Vector3 position = Target.position;
    2. position.z = transform.position.z;
    3. transform.LookAt (position);
     
    ArmagdoGaming and Anon1234 like this.
  3. Anon1234

    Anon1234

    Joined:
    Feb 17, 2014
    Posts:
    78
    You just copied this script and changed names. It was from thread that was about fishing rods. This script doesn't work, if it did, I wouldn't be posting this topic.
     
  4. Nubz

    Nubz

    Joined:
    Sep 22, 2012
    Posts:
    553
  5. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    I didn't copy that script (nor know what fishing rod thread you're referencing), there just isn't much you can change with such a simple script. Have you described your problem correctly? The code posted (and that you've seen before) will rotate the GameObject to face another by rotating only on the (world) z-axis.

    I'll take a guess and say you actually want to rotate around the y-axis, which would make the zombie turn left/right towards the player. If so, replace the .z with .y
     
    Anon1234 likes this.
  6. Anon1234

    Anon1234

    Joined:
    Feb 17, 2014
    Posts:
    78
    @Nubz Instead I get array of blue/purple links.
    @DanielQuick Oh, well, I'm an idiot. Sorry for inconvenience.
     
  7. Anon1234

    Anon1234

    Joined:
    Feb 17, 2014
    Posts:
    78
    Yea, well, it works nicely, but Rotation in Transform stays the same, nice that model is looking at me, but I want him actually to turn to me, not just look.
     
  8. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    In that case you'll want to do something similar to this:
    Code (csharp):
    1. public Transform Target;
    2. public float speed;
    3.  
    4. void Update () {
    5.     Vector3 targetPosition = Target.position;
    6.     targetPosition.y = transform.position.y;
    7.     Quaternion targetRotation = Quaternion.LookRotation (targetPosition-transform.position);
    8.     transform.rotation = Quaternion.RotateTowards (transform.rotation, targetRotation, speed*Time.deltaTime);
    9. }
    (Not tested)
     
    Anon1234 likes this.
  9. Anon1234

    Anon1234

    Joined:
    Feb 17, 2014
    Posts:
    78
    Is there something you don't know about Unity3D and C#, yet?
     
  10. A.Killingbeck

    A.Killingbeck

    Joined:
    Feb 21, 2014
    Posts:
    483
    That's actually maths, not specific to C# or Unity
     
  11. mathias234

    mathias234

    Joined:
    Sep 9, 2012
    Posts:
    239
    And it's not hard at all