- Home /
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!
what errors?
there's no need to check this object's tag - just only put this script on the player object
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 expectedAssets/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 convertobject' 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 expectedAssets/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 convertobject' expression to type
UnityEngine.Vector3'
Hope that is helpful! P.S. I need this urgently, as I am working under a deadline...
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.
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.
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);
}
//}
}
}
Your answer
Follow this Question
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