Search Unity

Bug !st person object only moving in global coordinates

Discussion in 'Scripting' started by RadilMahbub, May 16, 2022.

  1. RadilMahbub

    RadilMahbub

    Joined:
    Jul 26, 2021
    Posts:
    8
    So I've been making a 1st person controller in unity. I want to make the player move in relation to where I am looking at (basically, on the local coordinates), however, when I run it, it keeps moving at the same direction(the global coordinates). Can somebody give me some help on the matter?

    here's the code:


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

    public class moveAround : MonoBehaviour
    {
    public CharacterController controller;
    public float speed = 10;

    void Update()
    {
    float Xinput = Input.GetAxis("Horizontal");
    float Zinput = Input.GetAxis("Vertical");

    Vector3 move = transform.right * Xinput + transform.forward * Zinput;
    controller.Move(move * speed * Time.deltaTime);
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,686
    See the line where you construct your
    move
    vector?

    Those
    transform.right
    /
    transform.forward
    shortcuts refer to where this script is.

    Instead, use the
    controller
    reference instead, as in
    controller.transform.right
    .

    Also, if you would prefer something more full-featured here is a super-basic starter prototype FPS based on Character Controller (BasicFPCC):

    https://forum.unity.com/threads/a-basic-first-person-character-controller-for-prototyping.1169491/

    That one has run, walk, jump, slide, crouch... it's crazy-nutty!!
     
  3. SourGummi

    SourGummi

    Joined:
    Nov 14, 2021
    Posts:
    96
    i may be wrong, but i think local means in relation to the parent object and global is the scene.
    if that is the case you want to be moving on the global anyways.
     
  4. RadilMahbub

    RadilMahbub

    Joined:
    Jul 26, 2021
    Posts:
    8
    your
    controller.transform.right
    idea didn't help, but I found that, if I made an object with my look around code (only horizontal portion) and then referenced it in the moving code, it actually worked, so thanks a bunch friend!