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 /
  • Help Room /
avatar image
0
Question by FreeStyled · Jun 03, 2016 at 12:15 PM · c#rotationtransformlerp

Movement and Direction script bugging out

Hi,

So I've been trying to create a script which randomly moves a capsule from startpoint to endpoint with a pause in between each lerp and resets the start and end point values. I've managed to make it so that the capsule moves from start to end point and pauses for a random amount of time, but when it sets off again the capsule starts to "bug out" for a bit and then it resumes as normal. the amount of time the capsule "bugs out" for is equal to that of the random time that's being set. Hopefully someone can pick up where I've gone wrong and explain it to me because so far I've found nothing like this on the forum.

 using UnityEngine;
 using System.Collections;

 public class TestMovement : MonoBehaviour {

 public Vector3 startPosition;
 public Vector3 endPosition;
 public Vector3 currentPosition;
 public float speed;
 private float startTime;
 private float journeyLength;
 public float waitTime;



 void Start()
 {

     currentPosition = transform.position;
     startPosition = new Vector3(Random.Range(-10f, 10f), 1.4f, Random.Range(-10f, 10f));
     endPosition = new Vector3(Random.Range(-10f, 10f), 1.4f, Random.Range(-10f, 10f));
     startTime = Time.time;
     journeyLength = Vector3.Distance(startPosition, endPosition);
     waitTime = Random.Range(1f, 10f);

 }



 void ResetMovement()
 {
  
     startPosition = transform.position;
     endPosition = new Vector3(Random.Range(-10f, 10f), 1.4f, Random.Range(-10f, 10f));
     startTime = Time.time;
     journeyLength = Vector3.Distance(startPosition, endPosition);

 }



 void Update()
 {

     currentPosition = transform.position;

     Vector3 targetRotation = endPosition - transform.position;
     Vector3 newDir = Vector3.RotateTowards(transform.forward, targetRotation, (10 * Time.deltaTime), 0.0f);

     if (currentPosition == endPosition)
     {
         Invoke("ResetMovement", waitTime);
     }

     transform.rotation = Quaternion.LookRotation(newDir);
     transform.position = Vector3.Lerp(startPosition, endPosition, ((Time.time - startTime) * speed) / journeyLength);

 }

 }
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
Best Answer

Answer by FreeStyled · Jun 03, 2016 at 05:12 PM

OK, I've managed to solve my issue by wrapping everything inside Update() in an if statement and control what's being done by a bool. My finished Update() function looks like this:

 void Update()
 {
     if (!isWaiting)
     {
         currentPosition = transform.position;
         Vector3 targetRotation = endPosition - transform.position;
         Vector3 newDir = Vector3.RotateTowards(transform.forward, targetRotation, (10 * Time.deltaTime), 0.0f);

         transform.rotation = Quaternion.LookRotation(newDir);
         transform.position = Vector3.Lerp(startPosition, endPosition, ((Time.time - startTime) * speed) / journeyLength);

         if (currentPosition == endPosition)
         {
             isWaiting = true;
             Invoke("ResetMovement", waitTime);
         }
     }
 }

Where the bool isWaiting is set to false on Start. Thanks to /u/darkon76 on Reddit for helping me out with this and suggesting I could also use a State Machine in order to have more control of the states.

Comment
Add comment · 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

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

170 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

Related Questions

How do you rotate the player and keep them there? 1 Answer

Rotate transform to match another, but not past 180° 0 Answers

Unity 2D - Rotating the controls themselves 0 Answers

Move rotating object 0 Answers

What's the best way to rotate an object relative to another? 0 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