Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by ashjack · Oct 29, 2013 at 11:35 AM · c#follownpcflying

NPC flying towards me!!!

I am following a tutorial by BergZergArcade [][1]

and I have definitely copied out the script correctly, but instead of using a cube, I am using an NPC model. The NPC turns towards me, but if it gets too close, it rotates so that is is leaning backwards, and also, it lifts off the ground too! I have made it a rigidbody with no luck, because it still flies and rotates awkwardly. Here is the code BergZergArcade used:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyAi : MonoBehaviour {
     
     public Transform target;
     public int moveSpeed;
     public int rotationSpeed;
     
     private Transform myTransform;
     
     void Awake()
     {
         myTransform = transform;
     }
 
     // Use this for initialization
     void Start () {
         GameObject go = GameObject.FindGameObjectWithTag("Player");
         
         target = go.transform;
     
     }
     
     // Update is called once per frame
     void Update () 
     {
         Debug.DrawLine(target.position, myTransform.position, Color.clear);
         
         //Look At Target
         myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
         
         //Move Towards Target
         myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
     }
 }
 

I considered posting on the video, but the last comment was posted a month ago, and that's too long for me too wait. [1]: http://www.youtube.com/watch?v=pqDtPKLgbBE

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

· Add your reply
  • Sort: 
avatar image
0

Answer by fafase · Oct 29, 2013 at 11:39 AM

Don't worry this is a classic error.

You are using Transform to move the object. As a result, when it gets too close, your character may be bigger so the Slerp makes it look up to you, then the forward is now goign upwards and your guy is lifting up.

First, you should use a Character Controller. This way your guy will not go flying. Second you should only rotate the y axis, x and z remain the same.

You could check this one : http://unitygems.com/basic-ai-character/

Comment
Add comment · Show 6 · 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 ashjack · Oct 29, 2013 at 11:47 AM 0
Share

How do I lock the axis, because the freeze position has no effect?

avatar image ashjack · Oct 29, 2013 at 12:02 PM 0
Share

Sorry to keep posting, but I Put a character controller on the NPC, and he stays on the ground now, but he still rotates backwards.

avatar image fafase · Oct 29, 2013 at 12:04 PM 0
Share

Did you go and read the link ?

avatar image ashjack · Oct 29, 2013 at 12:13 PM 0
Share

Yes but I didn't find it. I'll have another look.

avatar image ashjack · Oct 29, 2013 at 12:47 PM 0
Share

I have looked again, but I still can't find it. What is it I need to do?

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

16 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting gameObjects to lookAt using runtime csharp script 1 Answer

Adding a box collider to an object in csharp script 3 Answers

Selecting transform in FBX hierarchy csharp script 1 Answer

How to destroy object forever? when i go to another scene and back to first, i want to that object stay destroyed! , Please !!!!  1 Answer

How do I get a script to make a character or object follow your player? 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