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 SuperSparkplug · Dec 09, 2013 at 10:25 AM · movementmove an objectwaypointcutscenevector3.lerp

Problems moving CharacterController from point to point.

Hi there,

I have a cutscene-like moment in my game where the first scene guides the player on rails in a first-person view through an environment. Unfortunately, I'm having quite a bit of trouble getting my Character to move from point to point.

Getting it to move from point A to point B is relatively easy, but not when it involves moving him to point C, D, F, G, etc. I've somehow managed to get it to move between 3 points, but after that it messes up. When it hits the 3rd point, it begins looping between point 2 and point 3. As soon as Point 3 is reached, it jumps back to Point 2 to lerp to Point 3 and repeats. I cannot figure out why it refuses to go to Point 4 and above. All my waypoints are defined and inputted. The code looks good to me. I tried changing the transform.position to adapt the endPoint once it gets there. Nothing seems to change this.

I'll post the code. Does anyone have any idea what's wrong here?

 using UnityEngine;
 using System.Collections;
 
 public class IntroMovement : MonoBehaviour {
     
     public Transform waypoint;
     public Transform waypoint2;
     public Transform waypoint3;
     public Transform waypoint4;
     public Transform waypoint5;
     public Transform waypoint6;
     
     private bool point1 = false;
     private bool point2 = false;
     private bool point3 = false;
     private bool point4 = false;
     private bool point5 = false;
     
     Vector3 endPoint1;
     Vector3 endPoint2;
     Vector3 endPoint3;
     Vector3 endPoint4;
     Vector3 endPoint5;
     Vector3 endPoint6;
 
     public float duration = 1.0f;
     
     private Vector3 startPoint1;
     
     private float startTime;
     
     // Use this for initialization
     void Start () {
         startPoint1 = transform.position;
         startTime = Time.time;
         endPoint1 = waypoint.position;
         endPoint2 = waypoint2.position;
         endPoint3 = waypoint3.position;
         endPoint4 = waypoint4.position;
         endPoint5 = waypoint5.position;
         endPoint6 = waypoint6.position;
     }
     
     // Update is called once per frame
     void Update () {
         if (point1 == false) {
             transform.position = Vector3.Lerp(startPoint1, endPoint1, (Time.time - startTime)/ duration);
         }
         if (transform.position == endPoint1) {
             startTime = Time.time;
             point1 = true;
         }
         if (point1 == true) {
             transform.position = Vector3.Lerp(endPoint1, endPoint2, (Time.time - startTime)/ duration);
         }
         if (transform.position == endPoint2) {
             startTime = Time.time;
             point2 = true;
         }
         if (point2 == true) {
             transform.position = Vector3.Lerp(endPoint2, endPoint3, (Time.time - startTime)/ duration);
         }
         if (transform.position == endPoint3) {
             startTime = Time.time;
             point3 = true;
         }
         if (point3 == true) {
             transform.position = Vector3.Lerp(endPoint3, endPoint4, (Time.time - startTime)/ duration);
         }
         if (transform.position == endPoint4) {
             startTime = Time.time;
             point4 = true;
         }
         if (point4 == true) {
             transform.position = Vector3.Lerp(endPoint4, endPoint5, (Time.time - startTime)/ duration);
         }
         if (transform.position == endPoint5) {
             startTime = Time.time;
             point1 = true;
         }
         if (point5 == true) {
             transform.position = Vector3.Lerp(endPoint5, endPoint6, (Time.time - startTime)/ duration);
         }
         
     }
 }
 
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

Answer by mujpir · Dec 09, 2013 at 02:47 PM

Hi @SuperSparkplug your code logic seems to be true but its too long to debug it . First try to make it simple : you can reduce your code like this :

 public class IntroMovement : MonoBehaviour
 {
     //use an array to store your waypoints
 
     public Transform[] waypoints;
 
     //use and index to store next target
     public int _index = 0;
 
     //use treshold to compare distance
     public float treshold = 0.1F;
 
     public float duration = 1;
     private float startTime;
 
     void Start()
     {
         startTime = Time.time;
     }
 
     private void Update()
     {
         Vector3 nextTarget;
         if (_index == waypoints.Length-1)
             nextTarget = waypoints[0].position;
         else
             nextTarget = waypoints[_index + 1].position;
 
 
         transform.position = Vector3.Lerp(waypoints[_index].position, nextTarget,
             (Time.time - startTime)/duration);
 
         //check whether it reach to current target
         if (Vector3.Distance(transform.position, waypoints[_index].position) < treshold)
         {
             _index++;
             startTime = Time.time;
 
         }
 
         //loop movement
         if (_index == waypoints.Length)
             _index = 0;
     }
 }

use a variable to check if your character has reached to its target, Do not use equal operation to compare positions since its not precise .

hope it can help

Comment
Add comment · Show 2 · 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 SuperSparkplug · Dec 09, 2013 at 04:32 PM 0
Share

Now I'm having a similar problem with your code. It just moves from point 2 - 3 and then stops forever. What seems to make even less sense is that if I up the duration say to 12 for example, it will now start at Point 3 and move to Point 4 and then stop...

... I am really confused right now...

avatar image mujpir · Dec 17, 2013 at 08:09 AM 0
Share

yeah . you are right . it need some fixes , I post new code when I fix it.

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

17 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

Related Questions

Move player to mouse position never go to exact position 0 Answers

Problem with MoveToWaypoint 1 Answer

How to drag a game object with a mouse (along x axis)? 1 Answer

Moving an object on the X axis corresponding to mouse movment 0 Answers

Moving objects problem 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