- Home /
Problem with vector2 not rotating, only tilting.
im trying to setup a controller for a hovering robot using a controller and unitys new input system. and it just isnt working right. it looks up and down but instead of left and right it tilts left and right. i saw this on a brackeys video. https://www.youtube.com/watch?v=p-3S73MaDP8 Thats exactly what i have.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class MovenRotate : MonoBehaviour
{ PlayerControlls controlls;
Vector2 move;
Vector2 rotate;
private void Awake()
{
controlls = new PlayerControlls();
controlls.Gameplay.Move.performed += ctx => move = ctx.ReadValue<Vector2>();
controlls.Gameplay.Move.canceled += ctx => move = Vector2.zero;
controlls.Gameplay.Rotate.performed += ctx => rotate = ctx.ReadValue<Vector2>();
controlls.Gameplay.Rotate.canceled += ctx => rotate = Vector2.zero;
}
public GameObject Camera;
void Update() {
Vector2 R = new Vector2(rotate.y, rotate.x) * 100 * Time.deltaTime;
transform.Rotate(R, Space.Self);
Vector2 m = new Vector2(move.x, move.y) * 10 * Time.deltaTime;
transform.Translate(m, Space.Self);
}
void OnEnable()
{
controlls.Gameplay.Enable();
}
void OnDisable()
{
controlls.Gameplay.Disable();
}
}
Answer by Bunny83 · Jan 23, 2021 at 05:23 PM
Well your code is not exactly the same.
Your major issue is that you rotate around local space vectors while in brackeys tutorial he's rotating around world space axis. This is a huge difference as euler angles represent a hierarchical structure. You usually don't want to rotate around local y but global. For looking up and down you want to rotate around local x. In the tutorial he's rotating around world axis, so the rotation axes are not aligned with the object but the world.
If you want a fps or tps controller you probably want to do something like this:
transform.Rotate(0f, R.y, 0f, Space.World);
transform.Rotate(R.x, 0f, 0f, Space.Self);
So you want a worldspace rotation around y and a local space rotation around x. Note that usually you would split up the character body from the head / camera and only rotate the body around global y and just the head locally around x.
Your answer
Follow this Question
Related Questions
MMD How to export model and animations to Unity as 3rd person controller? 2 Answers
How do I use controller knob input like mouse position? (New Input System) 1 Answer
How do I use an Input axis instead of Input.mousePosition??? 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Player Controller Rotation Script Help 2 Answers