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 /
avatar image
0
Question by Ahmad_aglan993 · Dec 05, 2016 at 07:32 AM · pathfindingrigidbody.velocitypath-finding

nonlinear rigidbody velocity

I'm working on drawing path game that the player draw path for the character and the character follow the path the problem is the path isn't being drawn correctly because I Instantiate list of points first I need to correct the nodes shape and the don't walk correctly on the line because the velocity is linear it moves from the first point to the final point directly I need it to trace all the points this is my code.

 using UnityEngine;
     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine.SceneManagement;
     public class FollowPath : MonoBehaviour
     {
         public Vector2 target;
         public float speed;
         float tr;
         int score = 0;
         public Animator anime;
         public GameObject node;
         public List<GameObject> drawnNodes;
         public Rigidbody2D rb;
         private Vector2 currentNodePosition;
         public static bool moving = false;
         public int drawnNodesLength = 0;
         public float moveSpeed;
         public static bool startMoving = false;
         private Vector2 dir;
         // Use this for initialization
         void Start()
         {
             rb = GetComponent<Rigidbody2D>();
             moving = false;
             startMoving = false;
             anime = GetComponent<Animator>();
             drawnNodes = new List<GameObject>();
     
     
         }
         // Update is called once per frame
         void Update()
         {
     
             if (moving)
             {
                 anime.SetInteger("State", 1);
     
             }
             else
             {
                 anime.SetInteger("State", 0);
             }
         }
     
         // Drawing the path when the screen touched
     
         void OnMouseDrag()
         {
             
             {
                 StartCoroutine("DrawLine");
             }
     
     
         }
     
         // Drawing the line
     
         public IEnumerator DrawLine()
         {
     
             Vector2 nodePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
     
             if (!Node.isHitted)
             {
                 drawnNodes.Add(Instantiate(node, nodePosition, Quaternion.identity) as GameObject);
                 currentNodePosition = nodePosition;
     
             }
     
             yield return new WaitForSeconds(.5f);
     
     
             Vector2 playerpos = transform.position;
             dir = currentNodePosition - playerpos;
             rb.velocity = dir * moveSpeed;
             moving = true;
             startMoving = true;
     
             yield return new WaitForSeconds(.21f);
             if (moving)
             {
                 if (drawnNodesLength >= 0 && drawnNodesLength < drawnNodes.Count)
                 {
                     Destroy(drawnNodes[drawnNodesLength]);
                     drawnNodesLength++;
                 }
             }
     
             MovePlayer();
     
     
     
         }
     
         // Make the player move around the nodes of the path
         public void MovePlayer()
         {
             moving = true;
     
             {
     
                 if (currentNodePosition.x < transform.position.x)
                 {
                     transform.rotation = Quaternion.Euler(0, 180, 0);
     
     
                 }
                 if (currentNodePosition.x > transform.position.x)
                 {
                     transform.rotation = Quaternion.Euler(0, 0, 0);
     
     
                 }
     
             }
     
             if (drawnNodes.Count == drawnNodesLength)
             {
     
     
                 moving = false;
                 rb.velocity = Vector3.zero;
                 rb.angularVelocity = 0;
                 drawnNodesLength = 0;
                 drawnNodes.Clear();
             }
     
     
     
         }
         // When the player hits anything
         void OnCollisionEnter2D(Collision2D hit)
         {
     
             if (hit.gameObject.tag == "Finish")
             {
                 if (score >= 3)
                 {
                     Invoke("LoadNextLevel", 2);
                     gameObject.SetActive(false);
     
     
                 }
             }
             if (hit.gameObject.tag == "Enemy")
             {
                 SceneManager.LoadScene("demo");
             }
     
         }
         void OnTriggerEnter2D(Collider2D hit)
         {
             if (hit.gameObject.tag == "Obstacles")
     
             {
                 drawnNodesLength = drawnNodes.Count;
     
     
                 foreach (GameObject g in drawnNodes)
                 {
                     Destroy(g);
                 }
                 moving = false;
                 rb.velocity = Vector2.zero;
                 rb.angularVelocity = 0;
                 drawnNodes.Clear();
             }
             if (hit.gameObject.tag == "Coins ")
             {
     
     
                 score++;
                 Debug.Log(score);
             }
     
     
         }
         private void LoadNextLevel()
         {
             SceneManager.LoadScene("Movie");
     
         }
     }
     
 
 

Comment
Add comment · Show 1
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 Mikael-H · Dec 06, 2016 at 11:23 AM 0
Share

What is your question?? And please check your formatting.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by edcx · Dec 05, 2016 at 04:28 PM

So you want to have a waypoint follow system and I believe that Unity Standart Assets should have one. You can try to have one target point that goes on your line ahead of your character and your character will move towards to that target point. So your character will not care about your path. It will just follow that target.

The point that moves on your line will not care about rotation and you will just move it on your path.

To be honest, I did not understand your problem clearly but your currentnode might have set to your final point quickly. Debugging might help you about this.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Pathfinding on multiple targets 1 Answer

Find a dynamic path from a start point to end point in a mxn matrix 0 Answers

Array of variable-length arrays (JS) 1 Answer

Find a path with specific steps (moves) on a grid 0 Answers

Entegrating AIFollow.cs and Wayfinder.js (A* Pathfinding Aron) 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