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 dragonking300 · May 12, 2017 at 11:56 AM · c#collisionscripting beginnerplayer movement

My click to move to script moves instantly instead of steadily

No, the problem isn't that I need to use lerp or MoveTowards becuase I've tried both of those(right now Lerp is in my script) I know my character is not just moving very fast becuase a enemy that is supposed to kill my player on contact doesn't kill them when they should intersect while moving. I have a feeling the problem is caused by my makeshift collision system for my player movement because my player has to be kinematic(see the bottom part of the script) I don't know how else to implement the collision system. If you have any suggestions on that please give me some. Finally the way the player moves. There is a commented out selection of my lerp script that when enabled makes my lerp script lerp in a triangle fashion rather then in a straight line. I have no clue what i'm doing wrong here but if you have a suggestion I'd gladly accept.

my script

    using UnityEngine;
 using System.Collections;
 
 public class Dash : MonoBehaviour
 {
     public GameObject Previouscollided;
     private GameObject collided;
     public LayerMask Wall;
     Vector3 newPosition;
     public GameObject RecentWall;
     public float speed;
     [SerializeField]
     public int CurrentWall;
     private bool LerpToPosition;
     private bool LerpToX = true;[SerializeField]
     private bool LerpToY = true;[SerializeField]
     private bool LerpToZ = true;[SerializeField]
     public bool clicked;
     private Vector3 NewPositionX;
     private Vector3 NewPositionZ;
     private Vector3 NewPositionY;
 
     void Start()
     {
         newPosition = transform.position;
     }
 
     void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             RaycastHit hit;
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit, Wall.value))
             {
                 LerpToX = true;
                 LerpToZ = true;
                 LerpToY = true;
 
                 collided = hit.collider.gameObject;
                 LerpToPosition = true;
                 RecentWall = collided;
                 newPosition = hit.point;
                            
                 Previouscollided.layer = LayerMask.NameToLayer("Wall");
                 Previouscollided = hit.collider.gameObject;
                 CollidedCheck();
             }
         }
     }
     private void CollidedCheck()
     {
         if (collided.tag == "Wall")
         {
             StartCoroutine("LerpPos");
       }
     }
 
     private IEnumerator LerpPos()
     {
 
         NewPositionX = newPosition;
         NewPositionY = newPosition;
         NewPositionZ = newPosition;
         while (LerpToX == true || LerpToZ == true || LerpToY == true)
         {
             while (LerpToX == true)
             {
                 NewPositionX.y = 0;
                 NewPositionX.z = 0;
                 if (Vector3.Distance(transform.position, NewPositionX) <= 5)
                 {
                     LerpToX = false;
                     yield return null;
                 }
 
                 transform.position = Vector3.Lerp(transform.position, NewPositionX, speed);
             }
             /* while (LerpToY == true) // the commented out section I was talking about
              {
                  NewPositionY.x = 0;
                  NewPositionY.z = 0;
                  if (Vector3.Distance(transform.position, NewPositionY) <= 5)
                  {
                      LerpToY = false;
                      yield return null;
                  }              
                  transform.position = Vector3.Lerp(transform.position, NewPositionY, speed);               
              }*/
             while (LerpToZ == true)
             {
                 NewPositionY.x = 0;
                 // no this is not typo, if I don't have this as NewPositionY even 
                 //this shoiuld be NewPositionZ the cube will only move on the x and z/ x and y axis |I cant tell |
                 NewPositionY.y = 0;
                 if (Vector3.Distance(transform.position, NewPositionZ) <= 5)
                 {
                     LerpToZ = false;
                     yield return null;
                 }
                 transform.position = Vector3.Lerp(transform.position, NewPositionZ, speed);
             }
             yield return null;
         }
     }
 }
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 RobAnthem · May 12, 2017 at 05:23 PM 0
Share

Using a coroutine for movement is a bad choice, as @Nose$$anonymous$$ills said, you're your usage of the "while loop" basically moves you all the way there before a new frame. Which is one reason why you never get the colllission of the enemy. The other reason being that you aren't even moving the player with physics, so a collision will never occur.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by NoseKills · May 12, 2017 at 02:58 PM

You yield out of the method in the 3 inner loops only when the object is closer than 5 units away from the target position and at the same time you move on to lerp the next coordinate.

Only when you yield out of the method, your app can execute the rest of its code, including all of Unity's code, for example the part where the game gets drawn on screen.

I think you should move the yield commands out of the if-clauses in the loops so you move the objects only once every Update(), not all the way to the target coordinate.

I assume you are moving the object just 1 coordinate at a time on purpose so i won't cover that.

Another thing to check is the value of speed. If it's 1 or greater, this will all happen in 3 frames even if you fix the other problems.

It is true that it doesn't matter whether you use Lerp or MoveTowards, but whichever you choose, you have to understand what they do and what are their differences so you can achieve the result you want with them and know what to pass in as parameters.

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 dragonking300 · May 12, 2017 at 05:00 PM 0
Share

They execute one at a time? So should I have them in like 3 different scripts because the purpose of the 3 lerps is to replicate collision with a kinematic game-object. Or is there a better way to do that?

avatar image NoseKills · May 12, 2017 at 10:09 PM 0
Share

Each of the inner loops has an exit condition that's fulfilled by rolling in that loop until the Lerp has moved the object enough. The next inner loop can start executing only after the one before it has finished - yielding takes you out of the method but the same loop continues when you return back to it - so the object in this case will first move all the way on th X-axis and then move on to the loop that moves it on the Z-axis. After both the inner loops have finished LerpToX and LerpToZ are set to false, but LerpToY is true and keeps your coroutine forever trapped in the outer loop... doing nothing because x and z conditions are still met.

This will also cause a bug since the LerpTo variables are not local variables in the coroutine and the next time you click to start another coroutine, the previous, trappped, coroutine is still there waiting for you to turn LerpToX and Z back to true. Now you have 2 coroutines moving the object at double speed. So make sure the exit conditions can be met.

If you just need to move the object s$$anonymous$$dily from A to B in a coroutine, you don't need to Lerp each component of the vector separately. Thats exactly what Vector3.$$anonymous$$oveTowards and Vector3.Lerp are there for and thats why they take in and return a Vector3: they handle x, y and z all in 1 go.

All you should need to do is something like

  private IEnumerator LerpPos()
  {
      NewPosition = newPosition;
      while (Vector3.Distance(transform.position, NewPosition) > 5)
      {
          transform.position = Vector3.$$anonymous$$oveTowards(transform.position, NewPosition, speed);
          yield return null;
       }
  }

You could do it with Lerp too but to do it "properly" it would require a few lines more and this comment is long enough already. There's already so many threads here about Lerp vs. $$anonymous$$oveTowards that you should easily find info about them by searching if you need to :)

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

313 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 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

Multiple Cars not working 1 Answer

How do I make a car in my driving scene trigger the trotting of a deer (on collision with a box collider around another object) and then make the deer die when my car collides with the deer?, 0 Answers

Unity input system character control 1 Answer

Distribute terrain in zones 3 Answers

help to understand collision detection 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