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 BlacKatFever · Dec 03, 2011 at 05:29 AM · boolslerp

Slerpy Spider Won't Behave (Chaining Slerps)

Hey all,

I'm having problems getting my script to switch between two slerps depending on the state of a bool.

I've got a spider that swings from "Start" to point "End" along an arc. Start and End are both public Transforms that can be set through the inspector and I've put in two empty game objects (A and B) that have been placed in the scene (at (0,0,0) and (10,0,0).

If A→B is true, then the spider slerps towards point B.

alt text

If the distance between A and B drops beneath a certain point (I've tried a few values) then the spider slerps back from B to A. Problem is that while A→B works fine, once the B→A bool kicks in, the spider darts between points A and B (linearly) within the span of a frame and then continues to dart in a straight line between the two points.

alt text

Here is the code.

 using UnityEngine;

using System.Collections;

public class spiderTest : MonoBehaviour {

 public Transform start;
 public Transform end;
 
 public bool startToFinish;
 public bool finishToStart;
 
 public float speed;
 public float distance;
 
 GameObject spider;
 public GameObject A;
 public GameObject B;
 
 Vector3 spiderPos;
 Vector3 startPos;
 Vector3 endPos;
 
 // Use this for initialization
 void Start () 
 {
     spider = this.gameObject;
     
     startPos = start.transform.position;
     endPos = end.transform.position;
     
     startToFinish = true;
     finishToStart = false;
     
     speed = 0.1f;
     transform.position = start.position;
     //transform.position = end.position;
 }
 
 // Update is called once per frame
 void Update () 
 {
     
     if(startToFinish == true)
     {
         Vector3 center = start.position + end.position * 0.5f;
         center -= new Vector3(0,-1,0);
         Vector3 startRelCenter = start.position - center;
         Vector3 endRelCenter = end.position - center;
         
         distance = Vector3.Distance(transform.position, end.transform.position);
         if(distance >= 0.1)
         {
             transform.position = Vector3.Slerp(startRelCenter, endRelCenter, Time.time * speed);
             transform.position += center;
         }
         if(distance <= 0.1)
         {
             ResetA();
         }
     
     }
     
     if (finishToStart == true)
     {
         Vector3 center = (start.position + end.position) * 0.5f;
         center -= new Vector3(0,-1,0);
         Vector3 startRelCenter = end.position - center;
         Vector3 endRelCenter = start.position - center;
         
         distance = Vector3.Distance(transform.position, start.transform.position);
         if(distance > 0.1)
         {
             transform.position = Vector3.Slerp(startRelCenter, endRelCenter, Time.time * speed);
             transform.position += center;
         }
         if(distance <= 0.1)
         {
             ResetB();
         }
     }
 }
 
 void ResetA()
 {
     if(startToFinish == true)
     {
         startToFinish = false;
         transform.position = end.position;
         distance = 0; 
         finishToStart = true;        
     } 
 }
 
 void ResetB()
 {
     if(finishToStart == true)
     {
         finishToStart = false;
         transform.position = start.position;
         distance = 0;
         startToFinish = true;
     }
 }

}

Can you see where I'm going wrong? Is there a way to make the spider slerp back from B to A?

(On a side note, I did think about handling the whole thing through the physics engine via a hinge joint chain, but at this point, it seems like handling it through scripts would fit the project better.)

Thanks for your time!

Comment
Add comment · Show 3
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 Eric5h5 · Dec 03, 2011 at 05:33 AM 1
Share

It's much easier if you use coroutines. See the example code for $$anonymous$$oveObject (that's the general idea, not that it will do this exact thing).

avatar image BlacKatFever · Dec 03, 2011 at 06:38 AM 0
Share

Not sure what a coroutine is, but I'll take a look at your hyperlink! Thanks!

avatar image BlacKatFever · Dec 03, 2011 at 07:27 AM 0
Share

Did a bit of reading into this and looks interesting. I still have a bit more reading to do before I feel like I can use it well, but thanks for the link! :D

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by WillTAtl · Dec 03, 2011 at 06:43 AM

Eric's comment is a good one, and there are many little ways this code could be improved, but that's for later, right now, you just want to make this work; you can learn to make the code better later!

The specific reason you're seeing the behavior you describe is that you're using Time.time for your slerp time, and Time.time always goes up. It only works the first time because Time.time starts at 0 when you hit "play," but it never resets, so when it reaches the end and you switch directions, "Time.time * speed" is still greater than 1, so the following slerps happen instantly!

three quick steps to make it work like you expect:

1) add a variable of type float called slerpStartTime to the script, at the top next to speed.

2) in Start(), ResetA(), and ResetB() - all three! - set slerpStartTime = Time.time

3) in both lines that call Slerp, change the last parameter from "Time.time speed" to "(Time.time - slerpStartTime) speed"

That should get you past this problem so you can move on to the next challenge!

Comment
Add comment · Show 1 · 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 BlacKatFever · Dec 03, 2011 at 07:26 AM 0
Share

Thank you.

I've found that I can adjust the Y values (the depth to which the spider descends at the half-way point, (the radius?)) by messing "center -= new Vector3(0,-1,0);" line. Seems like the deepest I can go is about 0.01f before the spider starts wigging out. Would be nice to get a bit more though. Trying to make it easier for our developers to set the spider's path to their liking. ;D

First time for using slerps so I'm still trying to figure out what everything does but your answer worked perfectly for what I'm trying to do. Thanks again for your time!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Error : Animator has not been initialized. UnityEngine.Animator:SetBool(String, Boolean) 1 Answer

Unexpected Symbol 'public' 1 Answer

I'm having trouble setting a bool. 2 Answers

How to add WAKE_UP animation before the character enters IDLE state? 1 Answer

Setting a bool to false using CharacterControllerHit? 1 Answer


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