Need help trying my own Third person controls

I have been searching awhile now and am unable to find the right coding for my third person script.

I created a rough script that does almost exactly what I want; though reviewing it I found issues I wanted to resolve.

What I am trying to achieve is to have my player move in all directions(diagonals included) and face that direction. Then have my camera follow, go around my player and when I move my player it will move in respect to the where the camera faces.

One issue I found was my player diagonal movement speed was faster than forward or right and left. I think I needed to something to be normalized.

From that I found codes that would allow my player to move in all directions with same speed and face those directions but when I pressed W it would only move forward in the wolrd z axis.

I am in need of help, and am doubting what I want to code will work.

let me know if anyone can help.
thanks

2901779–213655–player_movement.cs (2.61 KB)

How about you post your script for third person controller, so that way we can see what you’ve done, and hopefully be able to point out what part may be causing your issues.

Also… try splitting up your thoughts into concise paragraphs for each part of the topic. It makes reading what you have to say much easier, rather than a big block’o’text.

Thank you for the advice, apologize first time posting.

Looked at your code and wrote this to help.

float hInput = Input.GetAxisRaw("Horizontal");
float vInput = Input.GetAxisRaw("Vertical");

Vector3 direction = new Vector3(hInput, 0f, vInput);

Vector3 fwd = Gc.TransformDirection(Vector3.forward);
fwd.y = 0;//Make sure we are on the x/z plane only
direction = Quaternion.LookRotation(fwd) * direction;//Quaternions need multiplication in a specific order so don't change this.
direction.Normalize();//Just in case

I am using Input.GetAxisRaw() so that it bypasses anything the input system does to the values like deadzone and lerping keyboard input to be more anologue.

I have tried out your code and do know about the GetAxisRaw though as I implemented and tested it, my player does not move though from the inspector I see the variables change with camera movement and wasd inputs.

Maybe add this at the end?

transform.Translate(direction * mspeed * Time.deltaTime);

That did the trick on moving, I added the speed and time in the wrong place. The only thing left to do is get the player to face the direction of movement. I feel I should lock the y rotation of P(player gameobject) and just code a script to rotate the character that is child of P.

Would there be another way?

I fiddled around and only got one last issue, I added two more line of code to get the character to turn to movement direction and incidentally have it facing inverted meaning the player moves forward and character faces backwards.

heres the two lines of code: (also I do not know how you add code to replies or posts)

M.transform.position = Vector3.Lerp (M.transform.position, Gc.transform.position, Time.deltaTime * Mspeed);

M.transform.rotation = Quaternion.Lerp (M.transform.rotation, Quaternion.LookRotation(M.transform.position - transform.position), Time.deltaTime * Mspeed);

Edit:
OK I also noticed with these lines my character rotates back to face work z direction after movement and would like to get it to stay facing the direction of last movement.

Thank you for all the help

Rotation, you already have the forward so why not transform.forward = direction;
Not sure what M is in your code so I don’t know why its inverted.

For the returning to default rotation you need one if statement.

Code tags are used for code, there is a button to the left of the disk icon in the reply area that allows you to use it.
Please make sure to use those when posting code.

Here is the untested code.

float hInput = Input.GetAxisRaw("Horizontal");
float vInput = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(hInput, 0f, vInput);

if (direction.sqrMagnitude > 0)
{
    Vector3 fwd = Gc.TransformDirection(Vector3.forward);
    fwd.y = 0;//Make sure we are on the x/z plane only
    direction = Quaternion.LookRotation(fwd) * direction;//Quaternions need multiplication in a specific order so don't change this.
    direction.Normalize();//Just in case
    transform.Translate(direction * mspeed * Time.deltaTime;
    transform.forward = direction;
}

This isn’t lerped like your rotation so I leave it up to you to work that in.

Ok apologizes I should have posted that I got the coding they way I wanted, I will post my final code on for review, but thank you a lot for everyones help!

using UnityEngine;
using System.Collections;
[RequireComponent(typeof(CharacterController))]

public class Player_Movement_07 : MonoBehaviour {
    public CharacterController Controller;
    public float Mspeed = 10f;
    public float Rspeed = 10f;
    public float Jspeed = 10f;

    public float Hinp;
    public float Vinp;
    public Vector3 Dirct;
    public Vector3 Jump;
    public Vector3 Fwd;
    public Transform Gc;
    // Gc reference object transform, GameObject_Camera
    public Transform P;
    // P reference object transform, Player
    public Transform M;
    // PM reference object transform, Model

    public float Vvert;
    public float Jforce = 10f;
    public float Grav = 14f;

    void Start () {
        Controller = GetComponent<CharacterController> ();
    }
   
    void FixedUpdate ()
    {

        Hinp = Input.GetAxisRaw("Horizontal");
        Vinp = Input.GetAxisRaw("Vertical");

        Dirct = new Vector3 (Hinp, 0f, Vinp);


        Fwd = Gc.TransformDirection (Vector3.forward * Time.deltaTime);
        Fwd.y = 0f;
        Dirct = Quaternion.LookRotation (Fwd) * Dirct;
        Dirct.Normalize();
        transform.Translate (Dirct * Mspeed * Time.deltaTime);

//        Jump.Normalize ();
        if (Controller.isGrounded)
        {
            Vvert = -Grav * Time.deltaTime;
            if (Input.GetKey (KeyCode.Space))
            {
//            transform.Translate (Vector3.up * Jump.y * Time.deltaTime);
                Vvert = Jforce;
            }
        }
        else
        {
            Vvert -= Grav * Time.deltaTime;
        }
        Jump = new Vector3 (0,Vvert,0);       
        Controller.Move (Jump * Time.deltaTime);


        M.transform.position = Vector3.Lerp (M.transform.position, Gc.transform.position, Time.deltaTime * Mspeed);

        if (Dirct != Vector3.zero) {
            M.transform.rotation = Quaternion.LookRotation (Dirct);
        }
//        M.transform.rotation = Quaternion.Lerp (M.transform.rotation, Quaternion.LookRotation (M.transform.position - M.transform.position), Time.deltaTime * Rspeed);
    }
}