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
-1
Question by DubstepDragon · Apr 02, 2013 at 06:35 PM · errormovementvector3

Error with code, cannot figure out solution...

A few errors are popping up in unity when I tried to compose a simple movement script to move the player right and left. Can someone identify the problem and/or present me with an open solution?

Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class Movement : MonoBehaviour {
     
     public int speed = 5;
     
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if(gameObject.tag == "Player") {
             if(Input.GetKeyDown(KeyCode.A)) {
                 transform.Translate(Vector3(-1, 0, 0) * Time.deltaTime * speed);
             }
         }
 
         if(gameObject.tag == "Player") {
             if(Input.GetKeyDown(KeyCode.D)) {
                 transform.Translate(Vector3( 1, 0, 0) * Time.deltaTime * speed);
             }
         }
     }
 }
 

Thanks in advance!

Comment
Add comment · Show 4
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 Loius · Apr 02, 2013 at 06:38 PM 0
Share

what errors?

there's no need to check this object's tag - just only put this script on the player object

avatar image DubstepDragon · Apr 02, 2013 at 06:46 PM 0
Share

Sorry, did not explain in detail... The errors are as follows:

  • Assets/Scripts/$$anonymous$$ovement.cs(17,53): error CS0119: Expression denotes a type', where a variable', value' or method group' was expected

  • Assets/Scripts/$$anonymous$$ovement.cs(17,43): error CS1502: The best overloaded method match for UnityEngine.Transform.Translate(UnityEngine.Vector3)' has some invalid arguments - Assets/Scripts/$$anonymous$$ovement.cs(17,43): error CS1503: Argument #1' cannot convert object' expression to type UnityEngine.Vector3'

  • Assets/Scripts/$$anonymous$$ovement.cs(23,53): error CS0119: Expression denotes a type', where a variable', value' or method group' was expected

  • Assets/Scripts/$$anonymous$$ovement.cs(23,43): error CS1502: The best overloaded method match for UnityEngine.Transform.Translate(UnityEngine.Vector3)' has some invalid arguments - Assets/Scripts/$$anonymous$$ovement.cs(23,43): error CS1503: Argument #1' cannot convert object' expression to type UnityEngine.Vector3'

Hope that is helpful! P.S. I need this urgently, as I am working under a deadline...

avatar image Hotshot10101 · Apr 02, 2013 at 06:55 PM 0
Share

Another note about your usage of the Get$$anonymous$$eyDown. This will only return true on the frame that the key was pressed. It will be false after that until it is pressed again. If you want to check if it is still down and as long as it is down (which I think you want for this case) then you need to use Get$$anonymous$$ey ins$$anonymous$$d.

avatar image s_guy · Apr 03, 2013 at 04:19 AM 0
Share

A good methodology when learning to script (or write code in general) is to only implement a line or two at a time, then see the compiler errors (if any) and the runtime effects of your code.

That way, you can deal with (and learn) each issue at a time. It also makes it more reasonable to post questions about a single, specific problem. It's considered bad form to post code with a bunch of errors and ask people to debug it.

All of that said, you will receive better help if you post what kind of thing the player is that you're attempting to control. For example, does it have a CharacterController? A collider? A Rigidbody? Is it set $$anonymous$$inematic? This will yield better advice about the best ways to move it, because the correct answer is circumstantial.

1 Reply

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

Answer by robertbu · Apr 02, 2013 at 06:50 PM

When creating a new Vector3, you need the 'new' keyword in C# (not necessary in Javascript). Also you use GetKeyDown(). This will only fire for a single frame, so it only move once for each key press. If you want continuous movement, use GetKey() instead. And as @Loius indicated, the tag comparison is not necessary.

 using UnityEngine;
 using System.Collections;
  
 public class Bug16 : MonoBehaviour {
  
     public int speed = 5;
  
     // Use this for initialization
     void Start () {
  
     }
  
     // Update is called once per frame
     void Update () {
        //if(gameObject.tag == "Player") {
          if(Input.GetKeyDown(KeyCode.A)) {
           transform.Translate(new Vector3(-1, 0, 0) * Time.deltaTime * speed);
          }
        //}
  
       // if(gameObject.tag == "Player") {
          if(Input.GetKeyDown(KeyCode.D)) {
           transform.Translate(new Vector3( 1, 0, 0) * Time.deltaTime * speed);
          }
        //}
     }
 }


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

13 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

Related Questions

Error... that I don't understand. 3 Answers

Movement script trouble 0 Answers

dont work maxdistance 0 Answers

Expecting ':' Found '=' Error 1 Answer

addition of vector3 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