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 $$anonymous$$ · Sep 27, 2012 at 12:30 PM · c#movementvector3translate

How to get this object to move

I'm having a method in C# that finds the position of the player and the end position. The object jumps to the right position, but I would like the object to "walk" there over a period of time. How do I do that?

The code that actually moves the player is `player.transform.position = Vector3.Lerp(playerPos,endPos, Mathf.SmoothStep(0.0f, 1.0f, 1.0f));`

I've also tried to rotate this object towards this coordinate, but it doesn't rotate all the way towards it.

The code snippet I used for that is `player.transform.rotation = Quaternion.Slerp(player.transform.rotation, Quaternion.LookRotation(endPos), rotationSpeed * Time.deltaTime);`

Here's my code:

 public Vector3 MoveToCoordinate(int[] matrix)
     {
         int x_pos = (int)(matrix[0] * square[0,0].getMWidth());
         int z_pos = (int)(matrix[1] * square[0,0].getMLength());
         
         x_pos +=(int)square[0,0].getMWidth()/2;
         z_pos +=(int)square[0,0].getMLength()/2;
         
         Vector3 playerPos = player.transform.position; 
         Vector3 endPos = new Vector3(x_pos,0,z_pos);
         
         WalkToCoordinate(playerPos, endPos);
         
         
         player.transform.position = Vector3.Lerp(playerPos,endPos, Mathf.SmoothStep(0.0f, 1.0f, 1.0f));
         
         return endPos;
     }

Thanks in advance.

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 $$anonymous$$ · Sep 27, 2012 at 04:04 PM 0
Share

I've put the code an a method, and calls this method from Update(), is this possible? It don't work here… The code I'm using when trying to move the player to a given coordinate, it won't move… I can get them to jump to their coordinate, but would like to move them ins$$anonymous$$d.

player.transform.position = Vector3.Lerp(startPos, endPos, $$anonymous$$athf.SmoothStep(0.0f, 1.0f, 1.0f));

avatar image kmeboe · Sep 27, 2012 at 06:26 PM 0
Share

As a debugging step, have you tried simply using "player.transform.position = endPos;"? Is endPos really a position, or is it actually an offset from the playerPos? One last question: why are you calling a "WalkToCoordinate" method, and then also modifying the transform directly? $$anonymous$$ight these be conflicting?

avatar image $$anonymous$$ · Sep 28, 2012 at 08:13 AM 0
Share

endPos is a Vector3 position, not an offset from the player, I'm able to make the player "jump" to this coordinate, I just can't animate that jump. The jump is near instant.

Sorry for the confusing code, the WalkToCoordinate() method is created in a debug purpose. I'm (un)commenting the player.transform.position = (…) and WalkToCoordinate() method just to learn how these things works.

2 Replies

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

Answer by kmeboe · Sep 28, 2012 at 03:09 PM

Ok, now I think I see the problem. You are using the "Lerp" method, but you're essentially telling it to instantly jump to the end position. There are three things you need to do to accomplish your goal:

  1. Instead of passing SmoothStep to your Lerp, pass Time.deltaTime. You can multiply it by a factor to change the movement if you like -- just keep the factor constant.

  2. Make sure that "startPos" represents the player's position for each call to Lerp(). You can do this by grabbing the transform's position each time, if you want.

  3. To find out when you're done, you can compare the player's current position with the end position, and either see if it is equal (not sure if this will work exactly), or see if it's "close enough," using a distance.

There may be more efficient ways to do this, and maybe others can pipe in if this is the case.

If you need specific help on any of these steps, let me know; I'll be glad to explain further. Good luck!

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 $$anonymous$$ · Sep 28, 2012 at 08:10 PM 0
Share

It worked! ッ

I've updated my code to: "player.transform.position = Vector3.Lerp(player.transform.position,endPos, walkSpeed * Time.deltaTime);" The object now stops when it has reached it's destination, no further program$$anonymous$$g where needed. Have not tested and evaluated the code yet, but so far so good! Thanks!

avatar image kmeboe · Sep 28, 2012 at 09:20 PM 0
Share

You're welcome. Glad you got it to work.

avatar image
0

Answer by $$anonymous$$ · Sep 28, 2012 at 08:59 AM

Appriciate all the comments so far, thanks.

To make things more clear, I'm posting more of my code.

Here is my Update() method:

 void Update () {
         
         if(counter == 0)
         {
             counter++;
             getObject();    
         }
         if(move)
         {
             WalkToCoordinate(startPos, endPos);
         }
     }


The player is set to move once the dice is ready, this bool is not set to false again, since I don't know how this object can report that it has arrived at its destination. This is the WalkToCoordinate method:

 public void WalkToCoordinate(Vector3 startPos1, Vector3 endPos1)
     {
         int rotationSpeed =15;
         int moveSpeed = 1;
         Debug.DrawLine(startPos1, endPos1, Color.magenta);
         Vector3 moving = endPos1;
         
         Transform goTransform = this.GetComponent<Transform>();
         
         //Look at square
         player.transform.rotation = Quaternion.Slerp(player.transform.rotation, Quaternion.LookRotation(endPos1), rotationSpeed * Time.deltaTime);
         
         //Move towards target
         player.transform.position = Vector3.Lerp(startPos, endPos, Mathf.SmoothStep(0.0f, 1.0f, 1.0f));
         
         //move = false;
     }

I don't understand how to make this object move in a linear space to the given coordinate, and then return something when it has arrived.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

How to get our character controller script to make our player move? 1 Answer

Distribute terrain in zones 3 Answers

3D Character Controller slowing down the higher the slope angle (both up and down) 1 Answer

EnemyHover Script Error? 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