Movement Script Problem

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);
    }
}

Thanks,
Denny

can you describe the problem?

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

You have one curly brace too much at the start of the Update method :wink:
Also on the Start method btw.

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.

I believe that is easily fixed by using localPosition and localRotation on the transform, as they use local space as opposed to world space.

So i need to change rig.MovePosition(transform.position+ movement); to rig.MovePosition(transform.localPosition+ movement);?

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:

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

namespace Assets.Scripts
{
    [RequireComponent(typeof(CharacterController))]
    public class FirstPersonController : MonoBehaviour
    {
        #region Serializable Fields

        [SerializeField]
        private float _movementSpeed = 5.0f;
        [SerializeField]
        private float _mouseSensitivity = 5.0f;
        [SerializeField]
        private float _maximumPitch = 60.0f;

        #endregion

        #region Private Fields

        private CharacterController _controller;
        private Camera _mainCamera;
        private float _pitch = 0.0f;

        #endregion

        #region Unity Methods
        private void Awake()
        {
            _controller = GetComponent<CharacterController>();
            _mainCamera = Camera.main;
        }

        private void Start()
        {

        }

        private void Update()
        {
            float yaw = Input.GetAxis("Mouse X") * _mouseSensitivity;
            transform.Rotate(0, yaw, 0);

            _pitch -= Input.GetAxis("Mouse Y") * _mouseSensitivity;
            _pitch = Mathf.Clamp(_pitch, -_maximumPitch, _maximumPitch);

            _mainCamera.transform.localRotation = Quaternion.Euler(_pitch, 0, 0);

            float forwardMovement = Input.GetAxis("Vertical") * _movementSpeed;
            float sideMovement = Input.GetAxis("Horizontal") * _movementSpeed;

            var movement = transform.rotation * new Vector3(
                sideMovement,
                0,
                forwardMovement);

            _controller.SimpleMove(movement);
        }

        #endregion

        #region Private Methods
        #endregion
    }
}

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.

Thanks you for trying but my charcter doesnt move now. do you got any idea why?

Do you get any errors? The script I provided should work, as I used it myself in a number of games as well :confused:

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.
2721955--193273--upload_2016-7-24_14-35-19.png

Nope no errors. i dunno what happend. Is theres anything else to do?

It should work if you use the same setup for the character as I’ve shown above :confused:

How were you originally rotating the camera?

Well i use this script

using UnityEngine;
using System.Collections;

public class FirstPersonCam : MonoBehaviour {

    public float speedH = 2.0f;
    public float speedV = 2.0f;

    private float yaw = 0.0f;
    private float pitch = 0.0f;

    void Update () {
        yaw += speedH * Input.GetAxis("Mouse X");
        pitch -= speedV * Input.GetAxis("Mouse Y");

        transform.eulerAngles = new Vector3(pitch, yaw, 0.0f);
    }
}

Try this, but notice that in the sample the yaw rotation is on the parent of the camera gameObject, so you might need to adapt the code a bit:

float yaw = Input.GetAxis("Mouse X") * _mouseSensitivity;
transform.Rotate(0, yaw, 0);

_pitch -= Input.GetAxis("Mouse Y") * _mouseSensitivity;
_pitch = Mathf.Clamp(_pitch, -_maximumPitch, _maximumPitch);
_mainCamera.transform.localRotation = Quaternion.Euler(_pitch, 0, 0);

Nope it doesnt work AND now i lost my camera movement 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.

This is how i typed your code in unity:

using UnityEngine;
using System.Collections;

public class FirstPersonCam : MonoBehaviour {

    float yaw = Input.GetAxis("Mouse X") * _mouseSensitivity;
    transform.Rotate(0, yaw, 0);

    _pitch -= Input.GetAxis("Mouse Y") * _mouseSensitivity;
    _pitch = Mathf.Clamp(_pitch, -_maximumPitch, _maximumPitch);
    _mainCamera.transform.localRotation = Quaternion.Euler(_pitch, 0, 0);

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).

Thanks for the advice but what with the actual problem?? whenever i move my mouse the movement changes so what am i supposed to do?

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 ?