[Solved]Top-Down Movement Issues

Hey guys, so I’m just prototyping a generic top down shooter, and I’m running into some issues. For simplicity reasons, the camera is static at a fixed position (See gif below)

The movement worked fine until I tried adding look rotation. Currently, the players forward is being set to the mouse position? The rotation and movement should be independent of eachother. Here I demonstrate whats going on:

As you can see, the rotation works fine. However pressing forward (W) causes the player to move towards the mouse. W should always move the player forward on the Z axis, not towards the mouse pos.

Heres the script attached to the player:

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

public class PlayerController : MonoBehaviour
{

    public float speed = 1f;
    Vector3 lookPos;
    Camera cam;
    void Start()
    {
        cam = FindObjectOfType<Camera> ();
    }

    void Update()
    {
        Ray ray = cam.ScreenPointToRay (Input.mousePosition);
        RaycastHit hit;
   
        if (Physics.Raycast (ray, out hit, 100))
        {
            lookPos = hit.point;
        }

        Vector3 lookDir = lookPos - transform.position;
        lookDir.y = 0;
        transform.LookAt (transform.position + lookDir,Vector3.up);

        float moveVert = Input.GetAxisRaw ("Vertical") * Time.deltaTime * speed;
        float moveHoriz = Input.GetAxisRaw ("Horizontal") * Time.deltaTime * speed;

        Vector3 movement = new Vector3 (moveHoriz, 0, moveVert);
        //movement.Normalize ();
        transform.Translate(movement);

    }


}

So can someone help me understand whats going on here? I tried following a yt video, but obviously its not really working as intended. I would ask questions on some of the parts I dont understand, but those may be the parts that are causing the issues so ill save those questions for when this is solved.

I think what you want is : transform.Translate(movement, Space.World);

Geeeeeez I feel stupid now LOL
Thanks!!
Well now that thats working, I can ask the question I had.

This part right here:

        Vector3 lookDir = lookPos - transform.position;
        lookDir.y = 0;
        transform.LookAt (transform.position + lookDir,Vector3.up);

Can you (or someone) explain to me whats going on here? Im not understanding its logic

no problem :slight_smile:
just looks at the location you specified.
I think your code could do without the + and - (of transform.position)

Thanks! Those 2 parts were actually specifically what was confusing me lol… I couldnt really think of any reason as to why it was necessary to begin with, just assumed it was something out of my range of knowledge

Thanks again!

No problem. :slight_smile: You’re welcome.