- Home /
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
I think that the Lerp is not working because it has no enough time to be executed as another function is called right away...
Answer by K_Tec · Jul 01, 2017 at 06:51 PM
:) use Transform.LookAt();
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)
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.
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
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.
Is it not possible to merge this scipts into one??
Regards, T
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)
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.
Just add (nextWaypoint - 1) without modifying current code :)
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.
Your answer
Follow this Question
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