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 gdennis · Dec 29, 2014 at 12:46 PM · transformpositionlerpyieldslerp

Turning character slowly with Slerp

I make my character walk using a navmesh and when he arrived I would like him to turn towards the character he is in front of. I use the following function but I keep getting errors and I can't get it to work.

 public static IEnumerator TurnToFaceSmoothly (Transform targetThatWalks, Transform targetToLookAt) {
         while (Vector3.Dot (targetThatWalks.forward, (targetToLookAt.position - targetThatWalks.position).normalized) < 0.95f) {
             Vector3 dir = targetToLookAt.position - targetThatWalks.position;
             targetThatWalks.rotation = Quaternion.Slerp(targetThatWalks.rotation, Quaternion.LookRotation(dir), 5f * Time.smoothDeltaTime);
             targetThatWalks.eulerAngles = new Vector3(0f, targetThatWalks.eulerAngles.y, 0f);
             yield return null;
         }



I call this function as following in my "static IEnumerator doWalkingAction()" method:

 yield return StartCoroutine (TurnToFaceSmoothly (playerObj.transform, npcObj.transform));


Which gives me the following error: [QUOTE]error CS0120: An object reference is required to access non-static member `UnityEngine.MonoBehaviour.StartCoroutine(System.Collections.IEnumerator)'[/QUOTE]

I tried removing the static and calling the function just with:

 TurnToFaceSmoothly (playerObj.transform, npcObj.transform);

However when I did this I noticed that the function never ever gets called, I put some debug.log's in that turntofacesmoothly function and they never showed up (and obviously the turning did not happen as well)

What am i doing wrong? I cannot seem to figure it out, i've been stuck here for hours and hours.

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 SkaredCreations · Dec 29, 2014 at 12:53 PM

You're using coroutines statically but in your doWalkingAction you're using playerObj which I think is (reading the exception message) a local member of the script. As the message states: you cannot use the class members directly in the static functions because they belongs to an instanced object. Your second try (calling directly the function without StartCoroutine) is wrong because if you call that way it's not creating a "coroutine loop" (which is part of StartCoroutine) and instead it's called just once.

Comment
Add comment · Show 2 · 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 gdennis · Dec 29, 2014 at 01:03 PM 0
Share

@SkaredCreations Thank you for your answer.

I found a script StaticCoroutine (static coroutine). This is I guess what I need, but how do I use this? I noticed when I tried implementing it that it calls the function non-stop ins$$anonymous$$d of just once?

avatar image SkaredCreations · Dec 31, 2014 at 11:55 AM 0
Share

It depends on where/when you call the coroutine, but I still don't understand why you're doing in a static function ins$$anonymous$$d of doing this in a script attached to the navmesh agent.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Lerp not working 1 Answer

How to use Vector3.Lerp without slow-down 3 Answers

Smoothly Rotate Object Towards Target Object 1 Answer

Lerp takes me to 0,0,0 2 Answers

Changing object location every 3 second 2 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