When i type code to move my person i get errors
My code is here. Can I get help!!
moved from answer
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour {
public float moveSpeed = 10.0f;
public Vector3 moveVector;
void Update()
{
GetInput(Input.GetAxis ("Vertical"), Input.GetAxis("Horizontal"));
}
void GetInput(float vert, float horiz)
{
//apply directional vectors
moveVector = (transform.forward * vert + transform.right * horiz) * moveSpeed;
//cap speed
moveVector = Vector3.ClampMagnitude(moveVector, moveSpeed);
//apply to velocity
rigidbody.velocity = moveVector;
moved from comment
When I realized i deleted the curly brackets i added them and got an error that says, ridgidbody.velocity doesn't have a definition:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour {
public float moveSpeed = 10.0f;
public Vector3 moveVector;
void Update()
{
GetInput(Input.GetAxis ("Vertical"), Input.GetAxis("Horizontal"));
}
void GetInput(float vert, float horiz)
{
//apply directional vectors
moveVector = (transform.forward * vert + transform.right * horiz) * moveSpeed;
//cap speed
moveVector = Vector3.ClampMagnitude(moveVector, moveSpeed);
//apply to velocity
rigidbody.velocity = moveVector;
}
}
Because of the error being an "end-of-file" error, we are going to need to see the whole scrip in order to work out where the error is.
Downvoted because:
No proper question asked. In general way too short question.
Posting code as image
Posting the question in the wrong space as this clearly belongs into the "Help Room"
$$anonymous$$ake sure you read the user guide and FAQs before posting your next question.
I moved the question into the Help Room.
edit downvote revoked as the question has improved slightly
Answer by Bunny83 · Jan 21, 2017 at 06:32 PM
The error simply tells you that the compiler expected "more code" to follow but it hit the end of your file.
You removed the closing curly bracket that closes your GetInput method as well as the closing curly bracket that would close your class definition.
edit
Your second error is that you probably used an old example somewhere on the net. The rigidbody
shortcut property doesn't exist anymore. Those properties for built-in components have been removed to have a more consistent interface. The only one that still exists is the transform
property.
Anyways now you have to use GetComponent<ComponentName>()
to access another component on this GameObject. So in your case GetComponent<Rigdbody>()
.
It's usually a good idea to cache the rigidbody reference in Awake or Start and use the cached version everywhere else:
// [ ... ]
public Vector3 moveVector;
private Rigidbody rb;
void Awake()
{
rb = GetComponent<Rigidbody>();
}
// [ ... ]
void GetInput(float vert, float horiz)
{
// [ ... ]
rb.velocity = moveVector;
}
You clearly have trouble with the very basic syntax rules. Unity Answers is not a place to learn programming from 0. It's ment for questions that arise from the use of the Unity engine. Don't get me wrong. We'r happy helping newcomers if they hit a general logic problem or a syntax problem they don't understand, but we can't start explaining how to turn on a computer. Also questions should meet a certain "quality level". That includes (but is not limited to ^^):
writing a title that reflects the actual problem. If you got an error (runtime / compile time) that should be at least mentioned in the title.
actually writing a question and a proper description. The description should include everything that's necessary to understand your problem. The general rule: Include as much as necessary and as little as possible. So don't just post a script that has 2000 lines.
If you post code / code fragments make sure you post them as text and you mark it as code by selecting the code and pressing the "101/010" button.
this is my full code for my "$$anonymous$$ove" function. hope this helps?
Do not post an answer when you don't answer the question at the top. Either use comments or, what makes more sense when you add additional information, edit your question as it's relevant for the question. I moved your "answer" and the comment into your question.
I'll update my answer soon ^^.