I'm having trouble clamping the barrel's degrees plus its parent's(the tank's hull) degrees as well so I can get the same result like it does right of the picture below.
So the script is simple it rotates the barrel(turretBarrel) with the Y_Mouse Input and rotates the upperPart(turret) of the tank with the X_Mouse Input, along with barrel stabilization.
Script
//Note this.transform is the tanks hull
public Transform turret;
public Transform turretBarrel;
float xRot;
float angleY;
float rotY;
float minTurretRotY;
float maxTurretRotY;
Vector3 mRot;
Vector3 rot;
public void Update(){
xRot = Input.GetAxis("Mouse X") * 0.5f;
turret.transform.localRotation *= Quaternion.Euler(turret.transform.localRotation.x, xRot, turret.transform.localRotation.z);
angleY -= Input.GetAxisRaw("Mouse Y") * 0.5f;
angleY = Mathf.Clamp(angleY, minTurretRotY, maxTurretRotY);
mRot = turret.transform.rotation.eulerAngles;
rot = turretBarrel.transform.rotation.eulerAngles;
rotY = angleY;
rotY = Mathf.Clamp(rotY, minTurretRotY, maxTurretRotY);
// I'm using Quaternion.Euler for barrel Stabilization
turretBarrel.transform.rotation = Quaternion.Euler(new Vector3(rotY, mRot.y, mRot.z));
//Debug.Log(rotY +" | " + angleY + "/ Min : " + minTurretRotY+ " ; Max : " + (maxTurretRotY));
}
You need to change the min and max Y according to the Y rotation of the main body(tank) also. Update like this:
minY = YRotationOfParent;
maxY = YRotationOfParent + someOffSet;
Related
I am making a script to rotate my camera around a sphere but I need to clamp the y axis so the camera does not co over the polls of the sphere I am using rotate around to move my camera.
Thanks!
My current code
public float sensitivity = 1;
public float moveSpeed = 10;
public float maxUp = 45f;
public float maxDown = -45f;
public Transform target;
void Update()
{
transform.LookAt(target);
float HorizontalAxis = Input.GetAxis("Horizontal") * moveSpeed;
float VerticalAxis = Input.GetAxis("Vertical") * moveSpeed;
if (HorizontalAxis >= 1 || VerticalAxis >= 1 || HorizontalAxis <= -1 || VerticalAxis <= -1)
{
Quaternion targetPos = transform.rotation;
targetPos.x += HorizontalAxis * sensitivity;
targetPos.y += VerticalAxis * sensitivity;
transform.RotateAround(target.position, Vector3.left, targetPos.y);
transform.RotateAround(target.position, Vector3.up, targetPos.x);
}
}
Your code makes no sense to begin with.
You do
Quaternion targetPos = transform.rotation;
targetPos.x += HorizontalAxis * sensitivity;
targetPos.y += VerticalAxis * sensitivity;
Just to then use these as parameters in
transform.RotateAround(target.position, Vector3.left, targetPos.y);
transform.RotateAround(target.position, Vector3.up, targetPos.x);
A Quaternion has not three but four components x, y, z and w and they move in ranges between -1 and 1. You never touch the individual component of a Quaternion except you really know exactly what you are doing!
You rather simply want to use the HorizontalAxis and VerticalAxis directly as the parameters to RotateAround.
You could rather simply remember and clamp how much you already rotated like e.g.
private float rotatedY;
private void Update()
{
transform.LookAt(target);
// why two different multipliers anyway though?
var HorizontalAxis = Input.GetAxis("Horizontal") * moveSpeed * sensitivity;
var VerticalAxis = Input.GetAxis("Vertical") * moveSpeed * sensitivity;
// would a positive rotation exceed the maxUp?
if(rotatedY + VerticalAxis > maxUp)
{
// then rotate only so much that you terminate exactly on maxUp
VerticalAxis = maxUp - rotatedY;
}
// would a negative rotation exceed the maxDown?
else if(rotatedY + VerticalAxis < maxDown)
{
// then you rotate only that much that you terminate exactly on maxDown
VerticalAxis = maxDown - rotatedY;
}
transform.RotateAround(target.position, Vector3.left, VerticalAxis);
transform.RotateAround(target.position, Vector3.up, HorizontalAxis);
// sum up how much you already rotated vertically
rotatedY += VerticalAxis;
}
I'm working on a script which rotates the camera diagonally (3D x & 3D y axis) around an player object. Inputs are following:
mouse z-axis for the y-axis rotation around the object
mouse x-axis for the diagonal "over-the-shoulder" rotation, thus modifying both y and x of camera rotation:
demo of my current camera script
It works, however, I can't get to clamp the y-axis rotation for the camera. X-axis works as wished. Watch the video above to see the problem at the end. It overrotates on the y axis so that the camera faces the player in the complete wrong direction. Maybe someone with a functioning brain can come up with a solution? Would be awesome as hell, thanks in advance!
Here's my script:
void Update() {
mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
mouseY = -(Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime);
rotationXAxis += mouseY;
rotationXAxis = ClampAngle(rotationXAxis, -30f, 30f);
float rotationYAxis = rotationXAxis;
rotationYAxis = ClampAngle(rotationYAxis, 0f, 30f);
Quaternion fromRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y, 0);
Quaternion toRotation = Quaternion.Euler(rotationXAxis, transform.rotation.eulerAngles.y - rotationYAxis * Time.deltaTime, 0);
Quaternion rotation = toRotation;
Vector3 negDistance = new Vector3(xPosOffset, yPosOffset, -distance);
Vector3 position = rotation * negDistance + player.position;
transform.rotation = rotation;
transform.position = position;
player.Rotate(Vector3.up * mouseX);
mouseY = Mathf.Lerp(mouseY, 0, Time.deltaTime);
}
float ClampAngle(float angle, float min, float max) {
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
I try to explain you in pseudo.
Sinus and cosinus are basicaly same but are offset by 90degrees.
What this function are producing is -1 .. 1 number what is -100% .. 100%
basicaly what you need is
var _x,_y,_z; //original position
var deg; // 0..360
x = _x * sin(deg);
y = _y * cos(deg);
z = _z;
This was rotate around world [0,0,0]
If you want to rotate around another axis
var a_x,a_y,a_z;
x = _x + a_x * sin(deg);
y = _y + a_y * cos(deg);
z = _z + a_z;
Biggest key is to preserve default location [_x,_y,_z]
Do not do this mistake
x = x * sin(deg);
Precisely it is a matrix
_x _y _z
x [cos -sin 0]
y [sin cos 0]
z [0 0 1]
x = _x * cos(deg) + _y * -sin(deg) + _z * 0;
y = _x * sin(deg) + _y * cos(deg) + _z * 0;
z = _x * 0 + _y * 0 + _z * 1;
Look at 3d matrix for your axis.
https://en.wikipedia.org/wiki/Rotation_matrix#In_three_dimensions
Try some thing like this
void Update(Player player) {
_position = player.position;
mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
mouseY = -(Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime);
_delta_x = mouseX - _position.x;
_delta_y = mouseY - _position.y;
_delta_z = 0;
_rot_x = ClampAngle(mouseX, mouseY, -30f, 30f);
_rot_y = ClampAngle(mouseX, mouseY, -30f, 30f);
_rot_z = 0;
Vector3 position = new Vector3(_delta_x, _delta_y, _delta_z);
position = position.rotate (rot_x, rot_y, rot_z);
position = position.add(_position.x,_position.y,_position.z);
player.position = position;
}
I started a FPS project. I have a capsule as a Player, an empty GO as the head and the camera is the child object of the head.
On the vertical Axis, I clamp my y-rotation to -70 (min) and 70 (max). Surprisingly the value does not get clamped.
[SerializeField]
private Transform playerHead;
float inputHorizontal;
float inputVertical;
float sensitivity = 50;
float yMin = -70;
float yMax = 70;
Vector2 direction; // movement direction
Transform playerTransform;
void Start()
{
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}
private void Update()
{
inputHorizontal = Input.GetAxisRaw("Mouse X");
inputVertical = -Input.GetAxisRaw("Mouse Y"); // Inverted Input!
direction = new Vector2(
inputHorizontal * sensitivity * Time.deltaTime,
Mathf.Clamp(inputVertical * sensitivity * Time.deltaTime, yMin, yMax)); // The horizontal movement and the clamped vertical movement
playerTransform.Rotate(0, direction.x, 0);
playerHead.Rotate(direction.y, 0, 0);
}
The value
direction.y
gets clamped but I can still turn around my head by 360 degrees..
You are clamping your delta rotation - not your actual rotation.
Said in another way, direction is not the final rotation, it is the change in rotation. What you are effectively doing, is limiting the rotation to 70 degrees per frame.
You probably want to limit the actual rotation of playerHead, for example by adding the following lines to the end of update:
Vector3 playerHeadEulerAngles = playerHead.rotation.eulerAngles;
playerHeadEulerAngles.y = Mathf.Clamp(playerHeadEulerAngles.y, yMin, yMax);
playerHead.rotation = Quaternion.Euler(playerHeadEulerAngles);
There is also no reason to create a direction vector, when you are using each component separately anyway.
Clamp the final Y direction value not the current mouse movement -
[SerializeField]
private Transform playerHead;
float inputHorizontal;
float inputVertical;
float sensitivity = 50;
float yMin = -70;
float yMax = 70;
Vector2 direction; // movement direction
float currYDir = 0,prevYDir = 0;
Transform playerTransform;
void Start()
{
playerTransform = GameObject.FindGameObjectWithTag("Player").transform;
}
private void Update()
{
inputHorizontal = Input.GetAxisRaw("Mouse X");
inputVertical = -Input.GetAxisRaw("Mouse Y"); // Inverted Input!
direction = new Vector2(
inputHorizontal * sensitivity * Time.deltaTime,
inputVertical * sensitivity * Time.deltaTime); // The horizontal movement and the clamped vertical movement
playerTransform.Rotate(0, direction.x, 0);
currYDir = prevYDir + direction.y;
currYDir = Mathf.Clamp(currYDir, yMin, yMax);
playerHead.Rotate(currYDir-prevYDir, 0, 0);
prevYDir = currYDir;
}
I am able to rotate a cube on the axis with the greater mouse input, but don't know how to block input of the minor axis until mouse up.
The script includes a snap to the nearest 90 degree angle. Not so elegant, but I'll leave that for another post.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class s_rotate_test : MonoBehaviour {
float mouseX;
float mouseY;
public float rotationSpeed = 100f;
public float smoothing = 2f;
// Use this for initialization
void Start () {
Debug.Log (transform.rotation.x + " | " + transform.rotation.y + " | " + transform.rotation.z);
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButton(0)) {
mouseX = Input.GetAxis("Mouse X");
mouseY = Input.GetAxis("Mouse Y");
float rotationY = -mouseX * rotationSpeed * Time.deltaTime;
float rotationX = mouseY * rotationSpeed * Time.deltaTime;
if (mouseX * mouseX > mouseY * mouseY) {
transform.Rotate (0, rotationY, 0 /*, Space.World*/);
} else if (mouseY * mouseY > mouseX * mouseX) {
transform.Rotate (rotationX, 0, 0/*, Space.World*/);
}
}
if (Input.GetMouseButtonUp (0)) {
// Y AXIS SNAPS -------------------------------------------------------------------------------------------
if (transform.rotation.eulerAngles.y > 0 && transform.rotation.eulerAngles.y <= 45) {
Quaternion targetRotation = Quaternion.Euler (0, 0, 0);
transform.rotation = Quaternion.Lerp (transform.rotation, targetRotation, smoothing);
}
}
}
//------------------------------------------------------------------------
}
You need something to keep track of the current dragging state.
public enum State{
Normal,
DraggingX,
DraggingY
}
// This is a field
State draggingState = State.Normal;
With this setup, you check the biggest input only when the state is Normal.
After you determined the correct axis, you set the state to DraggingX (or Y).
When the mouse is pressed, you do this:
switch(dragginState){
case State.Normal:
// Get Greatest input (suppose it's X)
draggingState = State.DraggingX;
break;
case State.DraggingX:
// Use only X input
break;
case State.DraggingY:
// Use only Y input
break;
}
When it's released you set: draggingState = State.Normal; again.
I'm doing the 2D game with bullets moving in parabola, but before I shoot the projectile I want to be able to calculate its trajectory.
float velocity = Mathf.Min(Mathf.Max(distance, 1), 2.5f);
float cos = Mathf.Cos( (gameObject.transform.localEulerAngles.z + 90) * Mathf.Deg2Rad);
float sin = Mathf.Sin( (gameObject.transform.localEulerAngles.z + 90) * Mathf.Deg2Rad);
newGO.GetComponent<Rigidbody2D>().AddForce(velocity * new Vector3(cos, sin))
This code adds the rigidbody2d to my bullet (newGO). I try to count a point after 1 sec of flying using it:
Debug.Log(new Vector3(source.transform.position.x + velocity * 1 * cos, source.transform.position.y + velocity * sin * 1 + Physics2D.gravity.y * 0.5f * 1));
Unfortunately it doesn't return the correct result. What should I do?
Thanks for help and sorry for my English.
I think being a projectile you should use Rigidbody2D.velocity to set the initial velocity of the projectile rather than Rigidbody2D.AddForce. (Although the documentation does not recommend)
This simple script predicts where the attached object will be in a time determined by the variable "timeLimit" and moves a "target" gameobject to that point.
public class Velocity : MonoBehaviour {
public GameObject target;
private float HoriInitialSpeed = 5;
private float VertInitialSpeed = 5;
private float timeLimit = 2;
private float timeElapsed=0;
void Start ()
{
float HoriPredict = HoriInitialSpeed * timeLimit;
float VertiPredict = VertInitialSpeed* timeLimit + (0.5f * Physics2D.gravity.y * timeLimit * timeLimit);
target.transform.Translate(HoriPredict, VertiPredict,0);
GetComponent<Rigidbody2D>().velocity = new Vector2(HoriInitialSpeed, VertInitialSpeed);
}
void Update () {
timeElapsed += Time.deltaTime;
if(timeElapsed >= timeLimit)
{
//stop the bullet
GetComponent<Rigidbody2D>().velocity = new Vector2(0, 0);
GetComponent<Rigidbody2D>().gravityScale = 0;
}
}
}
Note: the bullet and the target must be in the same point inicially