0

Here is my code for moving a 2D top-down object using the built in virtual joystick:

void Update () 
    {
        Vector2 moveVec = new Vector2(CrossPlatformInputManager.GetAxis("Horizontal"), 
            CrossPlatformInputManager.GetAxis("Vertical"));


        Vector3 lookVec = new Vector3(CrossPlatformInputManager.GetAxis("Horizontal"), 
            CrossPlatformInputManager.GetAxis("Vertical"), 4000);

        transform.rotation = Quaternion.LookRotation(lookVec, Vector3.back);
        transform.Translate(moveVec * Time.deltaTime * speed);  
    }

Without the rotation code, the movement was perfect, after adding the rotation code the movement got all messed up, if the direction is down, it moves up. But the rotation is exactly how it should.

Kardux
  • 2,117
  • 1
  • 18
  • 20
Abdou023
  • 1,654
  • 2
  • 24
  • 45

1 Answers1

0

Simply change the transform.Translate(moveVec * Time.deltaTime * speed); line to transform.Translate(moveVec * Time.deltaTime * speed, Space.World); : by default, the translation is made relatively to the transform own space (Space.Self).

Kardux
  • 2,117
  • 1
  • 18
  • 20
  • This works perfectly, I don't want to be annoying, but if you could explain it more and when to use "Space.World" when not to, I would appreciate it very much. – Abdou023 Dec 06 '16 at 16:08
  • @Abdou023 Well it depends on the situation... In this case, the `Transform.Translate` [documentation](https://docs.unity3d.com/ScriptReference/Transform.Translate.html) states that the translation will be made in the local space of the transform : this means the translation will be affected by the object orientation. You can find the same type of behaviour in the ParticleSystem component : if you have let's say a flame particle generator moving around your scene, flame will remain straight when using Local space and will bend when using World space. Hope this helps :) – Kardux Dec 08 '16 at 07:18