Search Unity

character movement in 8 directions

Discussion in 'Navigation' started by kintobor, May 26, 2019.

  1. kintobor

    kintobor

    Joined:
    Feb 28, 2018
    Posts:
    1
    Hello i am current programing a top down adventure game. The object i am trying to accomplish is the ability to face and move in 8 directions and face the direction he is traveling in so far the player will move in all 8 directions. However the player will go right threw all walls .If i call the movement from either a rigid body or character controller . The movement completely breaks or the player still goes threw walls.

    here is what i have so far and what i have tries to do to fix it.

    public class Compassmovement : MonoBehaviour
    {
    public float velocity = 5;
    public float turnspeed = 10;
    Vector2 input; //=Vector3.zero;
    float angle;
    Quaternion targetRoation;
    Transform cam;
    //private CharacterController controller;
    Rigidbody player;
    private void Start()
    {
    cam = Camera.main.transform;
    // controller = GetComponent<CharacterController>();
    player = GetComponent<Rigidbody>();
    }
    private void Update()
    {
    GetInput();
    if (Mathf.Abs(input.x) < 1 && Mathf.Abs(input.y) < 1) return;
    CalculateDirection();
    Roate();
    Move();
    }
    void GetInput()
    {
    input.x = Input.GetAxisRaw("Horizontal");
    input.y = Input.GetAxisRaw("Vertical");
    ///s input.z = Input.GetAxisRaw("Vertical");

    // (input.z, -input.z);
    // input.y = 0;//Input.GetAxis("Vertical");
    //input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxis("Vertical"));
    }
    void CalculateDirection()
    {
    angle = Mathf.Atan2(input.x, input.y);
    angle = Mathf.Rad2Deg * angle;
    angle += cam.eulerAngles.y;
    }
    void Roate()
    {
    targetRoation = Quaternion.Euler(0, angle, 0);
    transform.rotation = Quaternion.Slerp(transform.rotation, targetRoation, turnspeed*Time.deltaTime);
    }
    private void Move()
    {
    //controller.Move(input);
    transform.position += transform.forward * velocity * Time.deltaTime;
    //player.velocity = transform.position += transform.forward * velocity * Time.deltaTime;
    //PLayerControllerTransfrom.add
    //player.AddForce(transform.forward * velocity);
    // player.MovePosition(transform.position+(transform.forward));
    }
    }
     
  2. Yandalf

    Yandalf

    Joined:
    Feb 11, 2014
    Posts:
    491
    If your character just walks through walls, either the walls have no collision, or the character doesn't.
    Keep in mind, without proper navigation tools your character will just keep walking against objects instead of moving around them. If you want the latter, look at the NavMesh and NavMeshAgent (which is what this subforum is used for).