1

however, when its moving up is not up anymore, its up plus/minus the y rotation

here is the code

if (joystick.Horizontal != 0 || joystick.Vertical != 0)
        {
            transform.Translate(transform.forward * Time.deltaTime * speed * joystick.Vertical);
            transform.Translate(transform.right * Time.deltaTime * speed * joystick.Horizontal);
            transform.LookAt(transform.localPosition + new Vector3(joystick.Horizontal, 0, joystick.Vertical));
        }
        else
        {
            _rb.velocity = Vector3.zero;
        }

The player is moving in circles. How do I fix this so it looks normal instead? (I understand why it doesnt work but i have no idea how to fix it!)

LOOK AT THIS BECAUSE I HAVE NO IDEA HOW TO EXPLAIN IT


Here is an example

If i turn an object 90° for example and want it to move to the right a little, it wont move to the right when i press [D]. It will apparently move downwards, even if its moving to the right


Also, here is the old controls that worked (now commented)

if (Input.GetKey(KeyCode.W))
        {
            transform.Translate(Vector3.forward * Time.deltaTime * speed);
        }
        if (Input.GetKey(KeyCode.S))
        {
            transform.Translate(Vector3.back * Time.deltaTime * speed);
        }
        if (Input.GetKey(KeyCode.A))
        {
            transform.Translate(Vector3.left * Time.deltaTime * speed);
        }
        if (Input.GetKey(KeyCode.D))
        {
            transform.Translate(Vector3.right * Time.deltaTime * speed);
        }
//[...]

transform.LookAt(target);

^^^in void Update()

Vladutzu27
  • 55
  • 9
  • 1
    Please edit your question to clarify it. At the moment it's *really* hard to understand. For example, I have very little idea what you mean by "HOWEVER, when its moving up is not up anymore, its up from object POV". (I'd also suggest making your *title* brief, but putting more detail into the *body* of the post.) – Jon Skeet Aug 18 '22 at 14:48
  • @JonSkeet I added a drawing, do you get it now? – Vladutzu27 Aug 18 '22 at 15:04
  • Not really, to be honest. But maybe someone with more Unity experience will understand it. (I'd suggest editing again to make the title shorter and put more information in the body though, *and* remove the all-caps that are widely regarded as equivalent to shouting.) – Jon Skeet Aug 18 '22 at 15:44
  • Why are you using transform translate and rigidbody at the same time? – Voidsay Aug 18 '22 at 16:03
  • @Voidsay I am using transform translate for easier to understand movement and rigidbody just once, to stop the player – Vladutzu27 Aug 19 '22 at 07:07
  • 1
    @Vladutzu27 Doing so is entirely unnecessary, unless you're adding a force somewhere, which might be the thing that is messing with you. – Voidsay Aug 19 '22 at 08:48
  • @Voidsay I removed it, ok, but it certainly isnt the thing that is messing with my project. The problem is that if i turn an object 90° for example and want it to move to the right a little, it wont move to the right when i press [D]. It will apparently move downwards, even if its moving to the right – Vladutzu27 Aug 19 '22 at 20:12
  • @Vladutzu27 I didn't expect it to be the problem either, it is just something that makes me weary of the rest of your script. What are other things you do in this script? – Voidsay Aug 20 '22 at 10:22
  • @Voidsay * set variables * check collisions * spawn enemies * shoot enemies I made this script beforehand, and i now only want to port it to mobile. WASD/mouse controls worked flawlessly – Vladutzu27 Aug 20 '22 at 10:40
  • @Vladutzu27 that is quite a number of very different things in one script. Can you post the working controls as a point of reference? – Voidsay Aug 20 '22 at 10:48
  • @Voidsay I did update the question – Vladutzu27 Aug 20 '22 at 19:42
  • @Vladutzu27 what's `target`? How do you calculate it? – Voidsay Aug 21 '22 at 09:21
  • @Voidsay Well its the position of the cursor raycasted on the ground, so it can look towards the cursor, and btw, i figured it out, while explaining it to my father. Rubberducking is a miracle! I will now edit the question so if anyone has the same problem, they can use my example! – Vladutzu27 Aug 21 '22 at 11:42
  • @Vladutzu27 it's great that you've found a solution. I'd recommend that you put it as an actual answer, so that you can mark it as accepted. – Voidsay Aug 22 '22 at 07:58

2 Answers2

0

I have figured it out! Here's how: Because the joystick makes it move towards x degrees but also makes it rotate towards x degrees, it will spin in circles. The solution is stupidly simple:

Make it only move forward and have it only rotate around itself with t he joystick. A well known example of this is how you move in minecraft and in most first person games: you press [W] or tilt the joystick forwards and look around using the mouse/the right joystick.

This is the code (In void Update() obv):

if (joystick.Horizontal != 0 || joystick.Vertical != 0)
        {
            if(Mathf.Abs(joystick.Horizontal) > Mathf.Abs(joystick.Vertical)){
                transform.Translate(Vector3.forward * Time.deltaTime * speed * Mathf.Abs(joystick.Horizontal));
            }
            else if(Mathf.Abs(joystick.Horizontal) < Mathf.Abs(joystick.Vertical)) {
                transform.Translate(Vector3.forward * Time.deltaTime * speed * Mathf.Abs(joystick.Vertical));
            }
            //transform.Translate(Vector3.forward * Time.deltaTime * speed);
            Debug.Log(Mathf.Abs(joystick.Horizontal) + " " + Mathf.Abs(joystick.Vertical) + " " + Mathf.Abs(joystick.Horizontal) * Mathf.Abs(joystick.Vertical));
            transform.LookAt(transform.localPosition + new Vector3(joystick.Horizontal, 0, joystick.Vertical));
        }
Vladutzu27
  • 55
  • 9
0

hi I know this is one year old but you could just make a cube and size it to 2 on the y axes then make a capsule then take away the colliders from both the cube and capsule add a character controller to the cube move the cubes position y to 1.08 then change the capsules position y to 1 then put a transparent material on the cube and make three scripts called: Movement, MoveCam, CameraFollowPlayer and add this in the movement script

public float speed = 1000f;

public CharacterController cc;

void FixedUpdate()
{
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 forward = transform.forward * moveVertical * speed * Time.deltaTime;
    Vector3 right = transform.right * moveHorizontal * speed * Time.deltaTime;
   
    cc.Move(forward + right);
    cc.SimpleMove(Vector3.forward * 0);
}

then add this in the MoveCam script

public float mouseSensitivity;

private float xRotation;
private float yRotation;

public Transform rb;

void FixedUpdate()
{
    float mouseX = Input.GetAxis("Debug Horizontal") * mouseSensitivity * Time.deltaTime;
    float mouseY = Input.GetAxis("Debug Vertical") * mouseSensitivity * Time.deltaTime;

    yRotation += mouseX;
    xRotation -= mouseY;
    xRotation = Mathf.Clamp(xRotation, -90, 90);

    transform.localRotation = Quaternion.Euler(xRotation * -1, yRotation, 0.0f);

    rb.rotation = Quaternion.Euler(0.0f, yRotation, 0.0f);
}

then add this in the CameraFollowScript

public Transform tr;

void Update()
{
    transform.localPosition = new Vector3(tr.position.x, tr.position.y + 0.34f, tr.position.z);
}

put the capsule in the cube for the camera variables in inspector drag the cube for game object into the section on the script on camera follow player that you will put in camera then put the MoveCam script in the camera which for rb you will put the cube again in inspector then in the MovePlayer script put the Character creator then adjust the settings to your heart content i am using this know and it works for both xbox and keybourd though you will need to go in player settings Input Manager Axes scroll down to Debug vertical and Debug Horizontal and open both of them change both of the gravity dead and sensitivity in both of them to 0 0.19 1 then just change the axes in the vertical one into 5 and the one in horizontal to 4 thats it have fun with your new First person controller joystick movement

  • As you have mentioned, this is old and has been fixed. Also, I find the code kinda pointless for what i was trying to achieve – Vladutzu27 Jun 05 '23 at 14:17