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 Ximithie · Oct 30, 2017 at 06:18 PM · physicsplayer movementcodepage

Player wont move

im having trouble moving my character it wont move any way at all the private float shows up in my inspector and i changed it to 5 and it still wont move here is the code im using please help using im also watching this video https://www.youtube.com/watch?v=1WG791jyZ6M&list=PLQXZd-rtXInDxFkJFxtXdTSrMNmPpjBBZ∈dex=42 to learn to code System.Collections; using System.Collections.Generic; using UnityEngine;

public class Player : MonoBehaviour {

 [SerializeField]
 private float speed;

 private Vector2 direction;

 // Use this for intialization
 void Start()
 {

 }

 // Update is called once per frame
 void Update()
 {
     GetInput();
     Move();
 }

 public void Move()
 {
     transform.Translate(direction * speed * Time.deltaTime);
 }

 private void GetInput()
 {
     direction = Vector2.zero;

     if (Input.GetKey(KeyCode.W))
     {
         direction += Vector2.up;
     }
     if (Input.GetKey(KeyCode.A))
     {
         direction += Vector2.left;
     }
     if (Input.GetKey(KeyCode.S))
     {
         direction += Vector2.down;
     }
     if (Input.GetKey(KeyCode.D))
     {
         direction += Vector2.right;
     }
 }

}

Comment
Add comment · Show 2
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 TreyH · Oct 30, 2017 at 07:26 PM 0
Share

Dumb comment inco$$anonymous$$g, but what is your Speed set to in the inspector?

avatar image Zeefles · Oct 30, 2017 at 09:01 PM 0
Share

I would try printing something to the console. See what values are changing and what aren't. Put this line of code in the bottom of your Update() method:

Debug.Log("Speed is: " + speed + " Direction is: " + direction);

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Optiknights · Oct 30, 2017 at 06:21 PM

I could be wrong but transform.translate takes a Vector3 instead of a Vector 2. Try:

transform.translate(new Vector3(direction.x, direction.y, 0) speed Time.deltaTime);

Comment
Add comment · Show 1 · 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 Ximithie · Oct 30, 2017 at 09:09 PM 0
Share

it doesn't work :(

avatar image
0

Answer by Zeefles · Oct 30, 2017 at 09:18 PM

I tried running your script and it worked perfectly :)

Perhaps you need to set your Speed value in the inspector like so:

alt text



Also, in reference to @Optiknights comment: Good thought, he can use a Vector2 so long as he is doing a side scroll and not a top down. If this is a top-down controller he would want to use a Vector3 and change the X and Z axis instead of the X and Y with the Vector 2.

@Ximithie this should work just fine so long as you have it attached to your player and declare a value in the inspector.


screen-shot-2017-10-30-at-21135-pm.png (57.4 kB)
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
avatar image
0

Answer by vancisesam · Oct 30, 2017 at 09:24 PM

As a suggestion for someone learning to code, its also worth thinking about how you can debug this. Start by printing messages everywhere to see what is going on. Is direction being set to a non-zero vector? What is your position before translate is called. What about after? Is it moving, and perhaps, your scale is too large?

By the looks of it, this code should work. I would double check that your scene is set up correctly.

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

130 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

Related Questions

Similar movement to a game (look description) 1 Answer

Issue with Instantiate and the instant that spawns on a moving object that rotates 1 Answer

Wall Jump Scripting Help 2 Answers

Trying to use colliders on trees to interact with player 1 Answer

Inconsistent jumping with character controller 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