Hello everyone
Basically, everything is in the question, but here’s further details:
Primary question : I have a cannon that rotates horizontally and vertically when I use the GetAxis (horizontal or vertical) with keys.
How to limit a rotation using transform.Rotate and Input.GetAxis. I need different angle limits for both axis (Horizontal : can turn between -80° and 80° ; Vertical : can turn between -45° and 80°).
It is a question that has been asked a lot as I can see, but the forever beginner I am cannot make it work…
Secondary question : At first, I was working with Input.GetKey, now I work with Input.GetAxis because I’ve read it was better. Is it really ?. What’s the best way to code a key input?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CanonBehaviour : MonoBehaviour {
public GameObject HorizontalRot;
public GameObject VerticalRot;
float rotSpeed = 7f;
void Update () {
if (Input.GetMouseButton (1)) {
rotSpeed = 35f;
} else {
rotSpeed = 7f;
}
HorizontalRot.transform.Rotate (0.0f, (Input.GetAxis ("Horizontal")) * rotSpeed * Time.deltaTime, 0.0f);
VerticalRot.transform.Rotate (0.0f, 0.0f, (Input.GetAxis ("Vertical")) * rotSpeed * Time.deltaTime);
}
}
If you think that what I have now is not the best way to go about it, let me know. I’m here to learn.
Thanks for any kind of help, that’s very much appreciated
EDIT : Just to be clear, I don’t expect a full script or anything, just a hint or a quick explanation of how that can be achieve is enough.
Have a nice day