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 Pathojen · Feb 19, 2019 at 09:11 PM · basic programming

What's wrong with my script?

Hi! I have been trying to make my player character move forwards and backwards, and turn left and right. However nothing I do to my script seems to fix it. Any advice would be appreciated.

Here's the code.

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Player_Movement : MonoBehaviour { // Start is called before the first frame update void Start() {

 }

 // Update is called once per frame
 void Update()
 {
     transform.Translate(0f, 0f, Input.GetAxis("Vertical")* Time.deltaTime);
     transform.Rotate(Input.GetAxis("Horizontal") * Time.deltaTime, 0f, 0f);
 }

}

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 Hellium · Feb 19, 2019 at 06:10 PM 1
Share
  1. Do you have any error in the console?

  2. Are you sure you have attached the script to a gameObject in your scene?

  3. Are you sure the gameObject isn't moving? Select the gameObject in the hierarchy and check the valued of the transform component in the inspector. Your object may move very slowly.

avatar image Pathojen · Feb 20, 2019 at 11:53 AM 0
Share

Please understand that I'm new. I'm not saying that what was said is wrong. I'm just saying that it's not working for me yet.

Here is what the errors that I'm getting say.

Assets\Player_$$anonymous$$ovement.cs(8,10): error CS0116: A namespace cannot directly contain members such as fields or methods

Any help would be appreciated.

avatar image Pathojen · Feb 20, 2019 at 01:28 PM 0
Share

I found this script from someone else, and it works.

using System.Collections; using System.Collections.Generic; using UnityEngine;

[RequireComponent(typeof(CharacterController))] public class $$anonymous$$ovement : $$anonymous$$onoBehaviour { public float Speed = 3.0F; public float RotateSpeed = 3.0F; void Update() { CharacterController controller = GetComponent(); if (transform != null) { transform.Rotate(0, Input.GetAxis("Horizontal") RotateSpeed, 0); var forward = transform.TransformDirection(Vector3.forward); float curSpeed = Speed Input.GetAxis("Vertical"); controller.Simple$$anonymous$$ove(forward * curSpeed); } } }

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by SharkoFR · Feb 19, 2019 at 04:16 PM

Hey, verify that your player have a rigidbody and this script should work:

      void Update () {
          if(Input.GetKey(KeyCode.W)) {
              transform.position += Vector3.forward * Time.deltaTime * movementSpeed;
          }
          else if(Input.GetKey(KeyCode.S)) {
              rigidbody.position += Vector3.back * Time.deltaTime * movementSpeed;
          }
          else if(Input.GetKey(KeyCode.A)) {
              rigidbody.position += Vector3.left * Time.deltaTime * movementSpeed;
          }
          else if(Input.GetKey(KeyCode.D)) {
              rigidbody.position += Vector3.right * Time.deltaTime * movementSpeed;
          }
  
          if(Input.GetKey(KeyCode.E)) {
              transform.Rotate(0, Time.deltaTime * clockwise, 0);
          }
          else if(Input.GetKey(KeyCode.Q)) {
              transform.Rotate(0, Time.deltaTime * counterClockwise, 0);
          }
      }

And you can adapt it with your own desires

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 Pathojen · Feb 20, 2019 at 10:35 AM

Thanks, but I can't get this script to work, either... I also get a ton of errors. However, the strangest one is that it says this doesn't derive from monobehaviour (when it actually does). Please help?

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 zereda-games · Feb 20, 2019 at 10:50 AM 0
Share

do you script name and your class names match, do you have using UnityEngine at the top of the code? Do you have an error in another code edit -preventing the codes to compile, one causing the error to give a false warning? erm.. yeah check that while i think

avatar image zereda-games zereda-games · Feb 20, 2019 at 10:52 AM 0
Share

Edit -my eyes didn't focus on the void update apparently..... but No it does not derive from $$anonymous$$onoBehaviour as it is. it would require this:

  public class $$anonymous$$yClass :$$anonymous$$onoBehaviour {
  }


Not!!

  public class $$anonymous$$yClass :monoBehaviour {
  }

so please do not confuse others with this wrong answer, also do not down vote people because you do not understand the code.. As far as i can tell at a quick glance. all is correct. and it goes in a C# scripts not a Javascript

Change void To function for java

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

101 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

Related Questions

2 hand weapons, using dynamic hands with the razer hydra controllers 0 Answers

Help for basic board game but for me it's hard.. 0 Answers

TextMeshPro Array 1 Answer

How should I code a basic timer script in C#? 1 Answer

need to create delay, for something that will execute every frame, for a bit of time 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