- Home /
How to Slerp in wider angles
How do I get a gameobject to slerp in a wider angle? At first I thought the object wasn't moving from point a to b in an arc at all, but then I realized that it looked like it was slerping in a straight line because its distance to its target was generally close.... I am lost here.
Here is the code I am using
transform.position = Vector3.Slerp(transform.position, leader.position + leader.forward *2, Time.deltaTime);
Any help is appreciated, Thanks
Answer by Jesse Anders · Feb 21, 2011 at 11:27 AM
Slerp() treats the input as vectors, so the arc that's swept out will be determined both by the distance between the two points and by the distance from each point to the origin. This is unlikely to produce intuitive results. (For example, the farther from the origin the two objects are, the more 'straight' the path traveled will be in general.)
I'm not exactly sure what behavior you're after here, but this is sort of an atypical use of Slerp(). Not to say you can't use it in this context, but if you want to be able to determine the angle swept out by the arc yourself, you'll most likely need to shift the origin appropriately, perform the Slerp(), and then shift back (which, while fairly straightforward, does involve a little math).
seems like superventure could try just increasing the time scale as well. Using Time.deltaTime alone means that the slerp occurs in 1 second regardless of the distance. You could make the time variable based on the distance between the two objects.
Hmm...I'm not really following. What I'm trying to do is this: I have the hero and an Npc that tags along. I want the npc to come to the front of the hero when I press a button, much like when you would call a pet/ child to come to you (they come to your front). Slerp works, but depending on where the npc is when I call it, it will sometimes plow through the character to get to the heros front. I 'thought' that maybe I could solve this by using slerp so that the npc will arc towards and AROUND the players front ins$$anonymous$$d of plow through it.
Do you have any other ideas? And how would I calculate the distance between the two for the time? Thank you for the help!
Slerp() isn't really the best tool for this, I don't think. What you're describing is essentially a pathfinding/navigation problem, and can be anywhere from fairly simple to very complex depending on the requirements and the nature of the environment. At the very least though, what I think you'll want to do is build a path for the NPC that moves to the specified position while avoiding the player. Can't really describe it in a comment, but it should be possible to build the path with a little trig and vector math.
Answer by Mike 3 · Apr 15, 2011 at 01:42 AM
You're using it wrong - it uses Vector3.zero as the pivot point, so you need to subtract the pivot point you want from both the start and end vectors, e.g. :
transform.position = leader.position + Vector3.Slerp(transform.position - leader.position, leader.forward *2, Time.deltaTime);