0
\$\begingroup\$

I'm new to Unity, and already dealing with a problem using the default third person controller script. I am trying to move and animate the player, but I have a problem, as when I try to move the character it moves beautifully but not only vertically and horizontally - it also moves up on the y axis! It just keeps going higher and higher, and though it's quite funny, it really messes the gameplay.

Here are screenshots of what happens, and yes, I do have gravity turned on: Screenshot of character on the ground, then in the air after pressing forward

\$\endgroup\$
6
  • \$\begingroup\$ Welcome to the Community! Can you please add some details in your question. Like your code snippet, screenshots etc? \$\endgroup\$ Commented Jan 7, 2016 at 14:53
  • \$\begingroup\$ Thanks :) I added the link to code it's a standard Unity script so I don't know why it wouldn't work.. I'll screenshot what happens to the character as well.. \$\endgroup\$
    – dj-neza
    Commented Jan 7, 2016 at 15:05
  • \$\begingroup\$ I think you mean the Y axis. X and Z are horizontal ("ground"). Interesting problem, though. \$\endgroup\$ Commented Jan 7, 2016 at 15:14
  • 2
    \$\begingroup\$ Please add the code snippet not a link to the file. And only the interesting part :) \$\endgroup\$
    – Vaillancourt
    Commented Jan 7, 2016 at 15:20
  • \$\begingroup\$ I am not sure what part could be responsible for this problem so I added whole code and part of it wasn't recgonised so it wasn't really useful that's why I added the link :) and yeah I meant Y but don't have good typing skills apparently.. \$\endgroup\$
    – dj-neza
    Commented Jan 7, 2016 at 15:32

1 Answer 1

1
\$\begingroup\$

Since they original code is not posted on this site, I am not going to click on the link. Instead, I will give you a very simple 3rd person controller script that illustrates how movement can be handled on a basic level. And to be honest, animations are much better handled using the Animation Controller Component. It uses a state machine to control the animations and you can add int parameters. In addition to the simple movement in 3rd person view, I will also comment on how you could take advantage of the Animation Controller in scripting.

using UnityEngine;
using System.Collections;

public class ThirdPersonCamera : MonoBehaviour
{
    public Camera cam;
    public GameObject character;
    public Animator anim; // Gets the Animator component and
    // is better used when it is attached to the character

    void followCharacter()
    {
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            cam.transform.position -= new Vector3(1, 0, 0);
            //anim.SetInt(1); if the parameter is an integer
            // it will move to that specific animation
        }

        else if (Input.GetKey(KeyCode.RightArrow))
        {
            cam.transform.position += new Vector3(1, 0, 0);
        }

        else if (Input.GetKey(KeyCode.UpArrow))
        {
            cam.transform.position += new Vector3(0, 0, 1);
        }

        else if (Input.GetKey(KeyCode.DownArrow))
        {
            cam.transform.position -= new Vector3(0, 0, 1);
        }
    }

    void Update()
    {
        followCharacter();
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Nice it is much appreciated! :) I'll do my best to implement this so I guess wish me luck and thanks a lot :) \$\endgroup\$
    – dj-neza
    Commented Jan 11, 2016 at 11:23
  • \$\begingroup\$ Note that while the original default controller uses rigidbody to move with physics, this is a more direct approach, that will not incorporate physics as such. You would also want to at least multiply the movement by Time.deltaTime (eg. new Vector3(0, 0, (1*Time.deltaTime))), as without it, movement may be jittery, and be (frame dependant?). That is, users with more processing power will have the Update() function run more frequently than other users, and in turn will move faster. \$\endgroup\$
    – Gnemlock
    Commented Aug 26, 2016 at 0:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .