0 Replies - 458 Views - Last Post: 19 July 2017 - 10:25 PM Rate Topic: -----

#1 Falcoshin  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 44
  • Joined: 31-August 14

Custom movement script bug (Unity)

Posted 19 July 2017 - 10:25 PM

I'm trying to create a player movement script and, for the most part, I think I have something that works. He moves where the input tells him to go and he'll turn in that direction. The problem, however, is that when there's no input detected, he'll only point to one of the 4 cardinal directions even if he was moving diagonally. I tried using a Logitech controller's joystick to see if the problem still existed and, while sometimes he would remain at the angle he was left at, most of the time he'd still end up at one of the cardinal directions.

Here's a video explaining what I mean

And this is the code I have currently

public class PlayerController : MonoBehaviour
{

    CharacterController cc;
    float turnVector;
    Vector3 movement;
    Vector3 angleBase;
    public float turnSpeed;
    bool lookAhead;

    void Start()
    {
        cc = GetComponent<CharacterController>();
        turnSpeed = 90f;
        angleBase = Vector3.forward;
    }
    ///*
    void Update()
    {
        movement = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

        if (movement != Vector3.zero)
        //if (movement.x > 0.01f || movement.z > 0.01f)
        {
            turnVector = Vector3.Angle(Vector3.forward, movement);
            if (Input.GetAxis("Horizontal") < 0)
            {
                turnVector = -turnVector;
            }
        }


        transform.eulerAngles = new Vector3(0, turnVector, 0);
        cc.Move(movement * Time.deltaTime);
    }
    //*/
    

    void LateUpdate()
    {
        
    }
}


Is This A Good Question/Topic? 0
  • +

Page 1 of 1