So hey guys… i made this script to aim with key board, the ideia is that his is atached to a camera and it can look 4-ways, but the problem is, unlike a fps, when i look all four ways the camera starts to getting out of axis… do you guys know how can i fix this?
function Update () {
if ( Input.GetButton ("right")) {
transform.Rotate(0,40*Time.deltaTime,0);
}
if ( Input.GetButton ("left")) {
transform.Rotate(0,-40*Time.deltaTime,0);
}
if ( Input.GetButton ("up")) {
transform.Rotate(-1,0*Time.deltaTime,0);
}
if ( Input.GetButton ("down")) {
transform.Rotate(1,0*Time.deltaTime,0);
}
//testando uma parada
}
im using Unity 3 forgot to mention that
Not sure why you would want to multiple anything by 0 or use unity 3. Do you mean you are using Unity 5.x free edition or are you actually using a unity version from 5 years ago?
yes i am using literaly unity 3.4.0f5 … its complicated to explain but i prefer this version , its where i learned how to code with js. and as you can see im still not the best on it, i do my codes very spacely and didatic…
i will send a video to explain my problem
I haven’t coded in js for a long time but I would control the x rotation on the camera and the y rotation on the character. You can use this c# script;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KeyboardLook : MonoBehaviour {
public bool SmoothLook = true;
public float SmoothTime = 5f;
public GameObject Camera; //Drag Camera in the inspector. Ideally is would be a child of the character gameobject
private Quaternion _CharacterRotation;
private Quaternion _CameraRotation;
public void Start()
{
_CharacterRotation = transform.localRotation;
_CameraRotation = Camera.transform.localRotation;
}
public void Update()
{
float yRotation = 0;
float xRotation = 0;
if (Input.GetKey(KeyCode.UpArrow))
{
xRotation = -1;
}
else if (Input.GetKey(KeyCode.DownArrow))
{
xRotation = 1;
}
if (Input.GetKey(KeyCode.RightArrow))
{
yRotation = 1;
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
yRotation = -1;
}
_CharacterRotation *= Quaternion.Euler(0f, yRotation, 0f);
_CameraRotation *= Quaternion.Euler(xRotation, 0f, 0f);
if (SmoothLook)
{
transform.localRotation = Quaternion.Slerp(transform.localRotation, _CharacterRotation, SmoothTime * Time.deltaTime);
Camera.transform.localRotation = Quaternion.Slerp(Camera.transform.localRotation, _CameraRotation, SmoothTime * Time.deltaTime);
}
else
{
transform.localRotation = _CharacterRotation;
Camera.transform.localRotation = _CameraRotation;
}
}
}
Hey i must thank you a lot!! thanks for the help, although i tried and tried to convert your script to js because my game is all in js, i could not do it, but you gave me a grea idea that i never considered, making the camera move Y and the object move X, it works perfectly, thanks a lot