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 Miteshk7 · Dec 18, 2018 at 07:13 AM · 2d-platformerplayer movementwaypoints

how to move player with WASD keys?

alt text

i want to move my player (blue player) through those waypoints (blue gameobject) and reach the end point (orange gameobject) by using WASD keys. i tried using MoveTowards but was able to move player in only one direction and i dont know how to move the player through all those waypoints. Here's my code

public float speed = 1f; public Transform GameObject;

 private void Update()
 {
     float step = speed * Time.deltaTime;


     if (Input.GetKey(KeyCode.D))
     {
         transform.position = Vector2.MoveTowards(transform.position, GameObject.position, step);
     }

     
 }
image.png (15.6 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by LCStark · Dec 18, 2018 at 08:41 AM

transform.position = Vector2.MoveTowards(transform.position, GameObject.position, step);

This code will only move your object in the direction of the attached GameObject transform. You will have to either find a better way to specify your target, or you will have to define more attached transforms and switch them as the targets in your movement functions.

Vector2.MoveTowards takes three parameters: current position, target position and maximum movement step (in this single frame). It returns the new position after moving towards the target. If you compare that returned position to your target, you will be able to tell when your object has reached the target. At that point you can choose to stop moving for now and change the target coordinates to the next waypoint position.

Check out the MoveTowards documentation here, it has a nice example of setting the target to the position of a mouse click, it should give you some ideas on how to solve this.

Oh, and you might want to change the name of your transform. GameObject is already used by Unity, if you'll want to use its functionality in the future, make sure your transform doesn't override that name.

Comment
Add comment · Show 3 · 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 Miteshk7 · Dec 18, 2018 at 09:26 AM 0
Share

Ty for your answer. Since i'm new to unity and coding i would like to see an example for what you have said so i can understand it better. Ty

avatar image LCStark Miteshk7 · Dec 18, 2018 at 10:03 AM 1
Share

Have you checked this link? It has a pretty decent and simple example of modifiable target.

Try something like this:

private Vector2 currentTarget;

private void Update() { float step = speed * Time.deltaTime; if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.D)) { transform.position = Vector2.$$anonymous$$oveTowards(transform.position, currentTarget, step); if (transform.position == currentTarget) { // Set a new `currentTarget` } } }
You could set your currentTarget to some initial value in the Start method, or have it public and set the starting value from the Inspector, or get its value from another object (like your waypoint transforms).

You could add all of your waypoints to a list for easy access. Ins$$anonymous$$d of a single public Transform GameObject put all of them in a public List<Transform> waypoints. That way you'll be able to get them all in your Update method (`waypoints[0].position, waypoints[1].position, ...).
avatar image Miteshk7 LCStark · Dec 24, 2018 at 02:46 PM 0
Share

Ty for your answer but i'm still unable to figure out how to move player in 4 directions. Right now the player moves only to the left when pressing 'A" key . Here's my code: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Waypoints : $$anonymous$$onoBehaviour { public float speed = 10f; private Transform target; private int wavepointIndex = 0;

 private void Start()
 {
     target = Red.points[0];
 }

 private void Update()
 {
     float step = speed * Time.deltaTime;

     if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.A))
     {
         transform.position = Vector2.$$anonymous$$oveTowards(transform.position, target.position, step);

         if (Vector2.Distance(transform.position, target.position) <= 0.010f)
         {
             wavepointIndex++;
             target = Red.points[wavepointIndex];
         }

     }

  }

}

And the player goes a little ahead of the waypoint whenever i press the key.alt text

player.png (38.5 kB)
avatar image
0

Answer by thleosw · Dec 18, 2018 at 08:11 AM

Hi, you could check out the function Input.GetAxis: https://docs.unity3d.com/ScriptReference/Input.GetAxis.html

It reads the input from the arrows or wasd keys

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

106 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

Related Questions

Player movement 1 Answer

Player movement script problem 1 Answer

How to make Jump 2D Action Stop after finite amount of Time? 1 Answer

Unintended bounce on 2D platformer 1 Answer

Character animation jerks when moving 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