Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Eco-Editor · Jul 01, 2017 at 06:49 PM · c#rotationtransformpositionavatar

transform.forward doesn't work in my code

Hello,

EDIT: I just wanted to update that I've solved it by using: transform.forward - without lerping, as lerp will not work in my update function because it imminently goes to next state. To avoid avatar natural sway like rotation: navmeshagent updateRotation = false and then to true when needed. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I've been looking for the answer for more than a week now. It seems like the solution is simple and as usual it's hard to get to the solution.

 using UnityEngine;
 using System.Collections;
 
 public class PatrolState : IShopperState
 
 {
     private readonly StatePatternShopper shopper;
     public int nextWayPoint;  
 
     private bool enRoute = false;
     
     public PatrolState(StatePatternShopper statePatternShopper)
     {
         shopper = statePatternShopper;
     }
 
     public void UpdateState()
     {
         if (enRoute && shopper.navMeshAgent.remainingDistance <= shopper.arrivalDistance && !shopper.navMeshAgent.pathPending)
         {
             shopper.transform.forward = Vector3.Lerp(shopper.transform.forward, SceneIndex.instance.waypoints[nextWayPoint].forward, 10f*Time.deltaTime); //this is not working...
             ToReachPointState();
             enRoute = false;
         }
         else if (!enRoute)
         {
             NextPoint();
             Patrol();
         }
     }
 
     public void ToPatrolState()
     {
         Debug.Log("Can't transition to same state");
     }
 
     public void ToReachPointState()
     {
         shopper.currentState = shopper.reachPointState;
         shopper.reachPointState.Reach();
     }

     public void Patrol()
     {
         enRoute = true;
         shopper.animator.SetBool("Walk", true);
         shopper.navMeshAgent.destination = SceneIndex.instance.waypoints[nextWayPoint].position;
     }
 
     private void NextPoint()
     {
         int randomChance = 50;
         int random = Random.Range(0, 100);
 
         if (random > randomChance)
         {
             nextWayPoint = (nextWayPoint + 1) % SceneIndex.instance.waypoints.Length;
         }
         else
         {
             nextWayPoint = Random.Range(0, SceneIndex.instance.waypoints.Length);
         }
     }

This is the code. The Lerp function is not working alt text alt text

going-to-the-same-point-but-not-turning.jpg (52.3 kB)
going-to-the-same-point-but-not-turning1.jpg (27.4 kB)
Comment
Add comment · Show 1
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 Eco-Editor · Jul 16, 2017 at 10:07 AM 0
Share

I think that the Lerp is not working because it has no enough time to be executed as another function is called right away...

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by K_Tec · Jul 01, 2017 at 06:51 PM

:) use Transform.LookAt();

Comment
Add comment · Show 4 · 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 K_Tec · Jul 01, 2017 at 06:51 PM 0
Share

or Transform.Forward to get the blue axis

avatar image Eco-Editor K_Tec · Jul 01, 2017 at 07:16 PM 0
Share

hi @12ttttt , I've changed it to transform.forward still no go.

     private void TurnToShelf()
     {
         float smoothing = 5.0f;
         wayPoint = SceneIndex.instance.waypoints[nextWayPoint];
 
         shopper.transform.forward = Vector3.Lerp(shopper.transform.forward, wayPoint.forward, smoothing * Time.deltaTime);
         Debug.Log("Turning to shelf");
     }

I've debuged it and this is what it says: point is pointing to (1.0, 0.0, 0.0)direction, avatar is pointing to :(-0.1, 0.0, -1.0)

avatar image K_Tec Eco-Editor · Jul 02, 2017 at 07:04 AM 0
Share

Im going to try this out in my test scene.

Show more comments
avatar image
0

Answer by Eco-Editor · Jul 02, 2017 at 10:03 AM

Hi @12ttttt first of all you're right I need to use shopper.transform.LookAt(). I've encountered this interesting thing when I send the avatar to the next way point (bear with me here it's a little long):

  public void Patrol()
     {
         shopper.animator.SetBool("Walk", true);
         shopper.navMeshAgent.destination = SceneIndex.instance.waypoints[nextWayPoint].position;
                 Debug.Log("going to point :" + SceneIndex.instance.waypoints[nextWayPoint].name);
         shopper.navMeshAgent.Resume();
     }

When the avatar reaches this point he goes to a new state where it's doing some animations

  public void Reach()
     {
         if (_delayRoutine != null)
         {
             shopper.StopCoroutine(_delayRoutine);
             _delayRoutine = null;
         }
         shopper.animator.SetBool("Walk", false);
         shopper.transform.forward = SceneIndex.instance.waypoints[nextWayPoint].forward;
         Debug.Log("turning to point :" + SceneIndex.instance.waypoints[nextWayPoint].name);
         playAnimation = true;
        PickupItem();

Now, here's the button line, you'd expect the Debug.Log to give you the same "name" of the point right? Well, it actually gives me a different name. It patrols to point x but it's turning to point y, regardless of the point it was sent to. As if the new state overrides the destination it at, while turning. So my guess is that "SceneIndex.instance.waypoints[nextWayPoint].forward" actually "spawns" a new order instead of being synchronized. And good news it actually turns to the blue axis, but it stands at the wrong point when it does so.

Comment
Add comment · Show 4 · 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 K_Tec · Jul 03, 2017 at 01:34 PM 0
Share

Normally .forward does not spawn a new object- it is the blue axis like if you are using Transform.LookAt();. Thats weird :D im confused :DD

Im trying to find a solution, but i dont know how your game machanics are workihng, it will be very difficult.

Regards, T

avatar image Eco-Editor K_Tec · Jul 03, 2017 at 06:55 PM 0
Share

Hi T Look I've investigated the script, the .forward does work. BUT because I have two scripts, one that sends the avatar to patrol and the other sends the avatar to reach to a point, there's "double" booking" on the wayPoints. 1: shopper.nav$$anonymous$$eshAgent.destination = SceneIndex.instance.waypoints[nextWayPoint].position; - sends the avatar to wayPoint 2: shopper.transform.forward = SceneIndex.instance.waypoints[nextWayPoint].forward; - have the avatar to face forward indeed, but to the next in list way point.

I wish there was another parameter to pass in to these [ ] : SceneIndex.instance.waypoints[__]. I tries "this" as for this, current way point.

avatar image K_Tec Eco-Editor · Jul 04, 2017 at 06:52 PM 0
Share

Is it not possible to merge this scipts into one??

Regards, T

Show more comments
avatar image
0

Answer by Vollmondum · Jul 05, 2017 at 08:01 AM

"Lerp" only functions in Update(). Add a bool "turning", turn it on on need, and add your Lerp rotation in Update under if(turning)

Comment
Add comment · Show 4 · 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 Eco-Editor · Jul 15, 2017 at 08:29 PM 0
Share

Hello @Arpian

I've put a Lerp function in my update. But if you take a look at the photos I've placed in this question, you'll see that I have another problem now. the avatar is turning, but ins$$anonymous$$d of facing the current way point, it's facing the "next" waypoint.

avatar image Vollmondum Eco-Editor · Jul 15, 2017 at 09:03 PM 0
Share

Just add (nextWaypoint - 1) without modifying current code :)

avatar image Eco-Editor Vollmondum · Jul 16, 2017 at 09:23 AM 0
Share

Where do I add this?

I've tried:

 public void UpdateState()
         {
             if (enRoute && shopper.nav$$anonymous$$eshAgent.remainingDistance <= shopper.arrivalDistance && !shopper.nav$$anonymous$$eshAgent.pathPending)
             {                 
                 shopper.transform.forward = Vector3.Lerp(shopper.transform.forward, SceneIndex.instance.waypointTransforms[nextWayPoint - 1].targetTransform.forward, 6f * Time.deltaTime);
                 Debug.Log("Turning to point :" + SceneIndex.instance.waypointTransforms[nextWayPoint - 1].targetTransform.name);
             
                 ToReachPointState();
             }

Look at 4th line, and I get exception: array index out of range.

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

350 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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

How to make Camera position Independent of its Rotation? 1 Answer

How can I have 2 transforms have the same position and rotation 0 Answers

Position and rotate 3 sprites perpendicular to the edges of a triangle 1 Answer

An Efficient Way To Follow Transform's Position? 3 Answers

Flip over an object (smooth transition) 3 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