- Home /
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);
}
}
Do you have any error in the console?
Are you sure you have attached the script to a gameObject in your scene?
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.
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.
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); } } }
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
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?
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
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
Follow this Question
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