Hello,
I accidently deleted my movement script so now im making a new one but there is some kind of problem in it that i cant tell what is it. please help
Here’s the Script:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public float speed = 10;
private Rigidbody rig;
// Use this for initialization
void Start () {
{
rig = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
{
float hAxis = Input.GetAxis("Horizontal");
float vAxis = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(hAxis, 0, vAxis) * speed * Time.deltaTime;
rig.MovePosition(transform.position + movement);
}
}
It says i need to fix all complier errors before it. also when i hover my mouse over the hAxis in the Vector3 part it says error CS0103: The name hAxis does not exist in the current context. And thats it
THANKS! now i can play!
But i got another problem if you mind helping me. I also got a camera rotation code that works perfectly fine but whenever i turn the camera it changes the movement. like up is right and such. thanks for your help.
You could check if you can use the Character Controller, is solves a bunch of things as far as movement of characters go for first person and third person games, so it might be useful for you if you are making such a game.
Here is a sample code for movement using the Character Controller:
Notice here that that camera and the character are both attached to each other (the camera being the child of the character). The SimpleMove function of the Character Controller makes sure that my character moves according to it’s local rotation (e.g. his forward movement is always the direction it’s own z-axis is pointing towards). I suspect your problem is that it moves on the direction of the world z-axis, which basically is North. so even if your camera rotates, the forward movement of you character will still be according to world space.
Do you get any errors? The script I provided should work, as I used it myself in a number of games as well
Here is an image of a small setup in which the script works flawlessly. As You can see I have the Main camera childed to the Player GO, which has a Character Controller and the script.
Can you post all relevant code and an image of the setup within Unity? The sample I provided yesterday should have worked, and if you have everything setup correctly the pitch and yaw code I just posted should also work, so I am wondering if there might be something else going wrong in your project.
Never just copy some code and expect it too work out of the box. All de variables with an _ are member fields that should be declared at the top of the the class as private fields. As this code is attached to your main camera, you can drop the _mainCamera variable.
Also, I suggest you install and use Visual Studio 2015 Community as Visual Studio has code analysis tools, and would have notified you of the errors in it’s error list (Though I believe Monodevelop also underlines errors with a red line).
What are you trying to make? 3rd person controller? Like in most RPG games. As in the character walks in the forward direction of the cam? And the cam can orbit when standing still ?