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 · Jan 28, 2019 at 03:04 PM · 2d-platformerplayer movementwaypointswasd

Player movement script problem

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Waypoints : MonoBehaviour
 {
     public float speed = 1f;
     private Transform target;
     private int wavepointIndex = 0;
 
        private void Start()
     {
         target = Red.points[0];
      }
 
     private void FixedUpdate()
     {
         float step = speed * Time.deltaTime;
 
         if (Input.GetKey(KeyCode.W))
         {
             transform.position = Vector2.MoveTowards(target.position,new Vector2(0 ,transform.position.y), step);
             wavepointIndex++;
             target = Red.points[wavepointIndex];
         }
 
         if (Input.GetKey(KeyCode.S))
         {
             transform.position = Vector2.MoveTowards(target.position  , new Vector2(0, -transform.position.y ), step);
             wavepointIndex--;
             target = Red.points[wavepointIndex];
         }
 
         if (Input.GetKey(KeyCode.D))
         {
             transform.position = Vector2.MoveTowards(target.position ,new Vector2(transform.position.x , 0), step);
             wavepointIndex++;
             target = Red.points[wavepointIndex];
         }
 
         if (Input.GetKey(KeyCode.A))
         {
             transform.position = Vector2.MoveTowards(target.position , new Vector2(-transform.position.x, 0), step);
 
             wavepointIndex--;
             target = Red.points[wavepointIndex];
         }
   }
 }
    

////////////Red script using System.Collections.Generic; using UnityEngine;

public class Red : MonoBehaviour {

 public static Transform[] points;

  void Awake()
  {
     points = new Transform[transform.childCount];
     for (int i = 0 ; i < points.Length ; i++)
     { 
         points[i] = transform.GetChild(i);
     }
 }

}

My problem is i'm trying to move player (blue circle) through those waypoints (red game objects) using WASD movement but the pleayer is moving too fast . For instance if i press A it goes to the last waypoint instead of going to the leftmost game object i have tried using raytracing but found no solution would like some insights on what im doing wrong.TY

image.png (23.1 kB)
Comment
Add comment · Show 20
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 sean244 · Jan 29, 2019 at 05:26 AM 0
Share

I made this project for you https://www.dropbox.com/s/03cmtrel9oxz9un/$$anonymous$$iteshk7.zip?dl=1 Download it and tell me if it helps. $$anonymous$$ake sure to open up the scene titled 'Default'.

avatar image Miteshk7 sean244 · Jan 29, 2019 at 07:19 AM 0
Share

this works only for 4 targets and if i want to make it work for multiple gameobjects then mapping WASD keys for every gameobject wont be possible , if i press W the player should go in 1 direction but in this case and even in my script player moves downwards , sidewards when pressed W

avatar image sean244 Miteshk7 · Jan 29, 2019 at 11:58 PM 1
Share

Here, I modified the project. Tell me what you think https://www.dropbox.com/s/tbvpvtwo5x5j104/$$anonymous$$iteshk7.zip?dl=1

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Snipe76 · Jan 28, 2019 at 04:26 PM

I see what you are trying to do.

But I think the problem is that you use Input.GetKey instead of Input.GetKeyDown in the FixedUpdate method.

Comment
Add comment · Show 6 · 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 · Jan 28, 2019 at 05:00 PM 0
Share

I'm using Input.Get$$anonymous$$ey only. and i tried using Input.Get$$anonymous$$eyDown and the player is not moving when pressed D and hardly moves when pressed W

avatar image Snipe76 Miteshk7 · Jan 28, 2019 at 05:08 PM 0
Share

Its because the method $$anonymous$$oveTowards needs to be called with each update.

and by putting it behind Get$$anonymous$$eyDown will make it be called only once.

You need to change the way the player moves towards the waypoints to achieve your goal.

avatar image Miteshk7 Snipe76 · Jan 28, 2019 at 05:54 PM 0
Share

would be more helpful if you can show how to do it in code

avatar image surfuay · Jan 31, 2019 at 12:14 AM 0
Share

put your movement method in Update() ins$$anonymous$$d of FixedUpdate()

i would also try vector3 ins$$anonymous$$d of vector 2, i know you're in a 2D game but my movement methods always work better in vector3 code ins$$anonymous$$d of vector2

avatar image Miteshk7 surfuay · Jan 31, 2019 at 06:20 AM 0
Share

as per your suggestion i used Vector3 but the player still moves in all directions when pressed any key and player gets stuck in the last gameobject and when S key is pressed at that point the player doesnt go backwards.

avatar image surfuay Miteshk7 · Jan 31, 2019 at 06:29 AM 0
Share

I'll test out your script as is tomorrow. going to be fir the night.

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

107 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

Related Questions

Player movement 1 Answer

how to move player with WASD keys? 2 Answers

How do I do a Snappy Jump in 2D with my 2D Jump Script? 1 Answer

Player Dash doesn't work properly 0 Answers

Getting my character to slide 2D platformer 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