• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Nov 20, 2016 at 09:24 PM by Midane53 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Midane53 · Nov 13, 2016 at 10:17 PM · c#rotationmovementgameobjecttransform

GameObject not looking at me..

Hi ! I got a problem. Surfed the web a lot to find a solution, But I don't know, i tried so much possibilities and variants, and it still doesn't work :( ! I'm trying to make a gameObject ( a creature ) rotating to look at me and then moving towards me. Here's my code, I really don't understand why the creature doesn't turn to me. Sorry for bag english grammar, I'm french c':

 using UnityEngine;
 using System.Collections;
 
 public class IA : MonoBehaviour {
 
     //Déclaration des variables
     bool joueurVu = false;
     public GameObject joueur;
     float mouvementSpeed = 2.5f;
 
     // Use this for initialization
     void Start () {
         joueur = GameObject.Find ("FPSController");
 
     }
     
     // Update is called once per frame
     void Update () {
         if (getDistance () < 10) {
             joueurVu = true;
             print ("Player seen!");
         }
 
         if (joueurVu) {
             transform.position = Vector3.MoveTowards (transform.position, joueur.transform.position, Time.deltaTime * mouvementSpeed);
             transform.LookAt (joueur.transform.position);
         }
     
     }
 
     //Autres méthodes
     float getDistance(){
         return Vector3.Distance (joueur.transform.position, transform.position);
     }
 
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

Answer by ElijahShadbolt · Nov 15, 2016 at 07:05 AM

That is a mystery. It works fine for me.

Maybe you put the script on the wrong object? It should be on the highest parent in the hierarchy of the creature.

I'm not sure about animations, but could they possibly be set to world space, to not update with rotation?

Also, on the 'if' statement on line 19, you might want to add an 'else' statement to set joueurVu to false.

 if (getDistance () < 10) {
     joueurVu = true;
     // ...
 } else {
     joueurVu = false;
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Midane53 · Nov 16, 2016 at 11:40 AM 0
Share

I will check that ! Jup adding an "else" sounds good ahah :P

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

My Prefab goes crazy and it moves faster when i click in another thing of the unity editor or the scene even if that didn't suppose to happen 0 Answers

Auto level an object 0 Answers

i'm using a code to make my cube walk with character controller but when i play the cube spins. 0 Answers

Unity 2D - Rotating the controls themselves 0 Answers

LookAt() rotating the wrong way 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges