Unity - Joystick to movement and rotate camera same time - c#

I have a problem.
I want to use joystick to movement player.
I want to rotate camera if player click ANYWHERE ELSE on screen.
Here is picture
My problem:
If the player use joystick then camera rotate too!
I tried IsPointerOverGameObject not good, because player press the joystick and drag it to the screen then joystick still work, but camera rotate again :(
So I think if player use joystick then I disable rotate camera, but not good, because If player use two fingers it is possible both at the same time. (one finger joystick and one finger screen)
public float speed = 2.0f;
private float X;
private float Y;
void Update() {
if (Input.GetMouseButton (0)) {
if(!EventSystem.current.IsPointerOverGameObject ()) {
transform.Rotate (new Vector3 (Input.GetAxis ("Mouse Y") * speed, -Input.GetAxis ("Mouse X") * speed, 0));
X = transform.rotation.eulerAngles.x;
Y = transform.rotation.eulerAngles.y;
transform.rotation = Quaternion.Euler (X, Y, 0);
}
}
}
So another solution is needed.
If player use the joystick, do not move the camera.
But if player use it with two fingers (one with the joystick and the other with the camera allowed!)
I hope you understand. (similar to other shooting games).
Thanks!

Related

Unity FPS Controller camera only moves when mouse movement stops

Using unity FPSController asset, here's the code for actually moving the camera called in LateUpdate() method.
private void CameraRotation()
{
// if there is an input
if (_input.look.sqrMagnitude >= _threshold)
{
//Don't multiply mouse input by Time.deltaTime
float deltaTimeMultiplier = IsCurrentDeviceMouse ? 1.0f : Time.deltaTime;
_cinemachineTargetPitch += _input.look.y * RotationSpeed * deltaTimeMultiplier;
_rotationVelocity = _input.look.x * RotationSpeed * deltaTimeMultiplier;
// clamp our pitch rotation
_cinemachineTargetPitch = ClampAngle(_cinemachineTargetPitch, BottomClamp, TopClamp);
// Update Cinemachine camera target pitch
CinemachineCameraTarget.transform.localRotation = Quaternion.Euler(_cinemachineTargetPitch, 0.0f, 0.0f);
// rotate the player left and right
transform.Rotate(Vector3.up * _rotationVelocity);
}
}
At first, I thought I was just having FPS issues, but the game is completely smooth when the camera is not moving, as soon as movement starts, the camera is extremely jittery. further testing it feels like the camera only moves when I stop moving my mouse. Like it knows where it should be but doesn't get there smoothly, just gets there when you're done inputting your movement. I've tried lowering the rotation speed variable to make sure its not a sensitivity thing. Same problem.

Limited rotation towards mouse

I have 2D game (unity, c# scripts),
where is a submarine with tower (parent) and gun (child).
look at the screenshot
Tower can rotate (when I press A or D) around submarine body. (works fine)
Problem is the gun on tower should rotate towards mouse, but only in angle limitation. Angle limitation (by parent) now works fine, but gun doesn't look towards mouse.
void Update() {
Vector3 mousePosition = Input.mousePosition;
mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
Vector2 direction = new Vector2(mousePosition.x -
transform.localPosition.x,mousePosition.y - transform.localPosition.y);
float angle = (Mathf.Atan2 (direction.y , direction.x) * Mathf.Rad2Deg);
transform.localRotation = Quaternion.Euler (new Vector3(0, 0, Mathf.Min(
Mathf.Max( Mathf.Abs(angle),40 ),140 )));
}
Here is a video where you can see how it "works" now: https://youtu.be/1pm54cjzYxA
Thanks for help!
Consider Using LookAt(); function

Unity jittery movement

I have the following simple lines of code that moves the camera similar to the camera movement in zig zag game ( moving vertically forever ). I also have very simple cube structure similar to zig zag game. In original zig zag game, there is no jittery movement in iPad Mini. But the below lines of code causing jittery movement..
I tried project settings->Time setting to 0.03 and targetFrameRate to
Tried in Update
Tried in FixedUpdate
Tried in LateUpdate...
But none works, I can always reproduce the jittery movement
void Update() {
float calculatedSpeed = speed * Time.deltaTime;
transform.Translate( direction * calculatedSpeed);
}
void LateUpdate() {
if( !dead ) {
Vector3 position = transform.position;
float halfX = position.x;
float halfZ = position.z;
float distance = Vector3.Distance (position, playerInitPosition);
Vector3 newPos = Main.Instance.camReference.forward * distance;
Main.Instance.camReference.position = newPos;
}
}
In Update, transform.position updates player move in zig zag pattern,
In LateUpdate, camera moves vertically based on the distance the player moves
You can check android game play here:
https://play.google.com/store/apps/details?id=com.angryeggstudio.games.zigzagpenguin
This smoothness is not there in iPad Mini
Update 1:
Android that works without any flaw...its smooth and fine :
iPad Mini that has jittery movement:
Note: Check the cube edges while camera moves... you can spot its jittery movement in iPad Mini...

Camera shaking on rotating player while crouching in unity

I am having a problem with the camera following the player. When the player moves, the camera shakes and this effect is more noticeable when the player is in the crouched position. I am using root motion for the player with animations by mixamo.
Player Script:
Transform cameraT;
void Start () {
cameraT = Camera.main.transform;
}
void FixedUpdate ()
{
float sideMotion = Input.GetAxis("SideMotion");
float straightMotion= Input.GetAxis("StraightMotion");
if (Mathf.Abs(sideMotion) > 0 || Mathf.Abs(straightMotion) > 0)
{
transform.eulerAngles = new Vector3(transform.eulerAngles.x,
cameraT.eulerAngles.y);
}
}
Camera Script:
public float distanceFromPlayer=2f;
public float mouseSensitivity=6;
public Transform player;
public Vector2 pitchConstraint= new Vector2(-30,80);
Vector3 rotationSmoothVelocity;
Vector3 currentRotation;
public float rotationSmoothTime = 0.2f;
float yaw; //Rotation around the vertical axis is called yaw
float pitch; //Rotation around the side-to-side axis is called pitch
private void LateUpdate()
{
yaw += Input.GetAxis("Mouse X") * mouseSensitivity;
pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
pitch = Mathf.Clamp(pitch, pitchConstraint.x, pitchConstraint.y);
currentRotation = Vector3.SmoothDamp
(currentRotation, new Vector3(pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
transform.eulerAngles = currentRotation;
transform.position = player.position - transform.forward * distanceFromPlayer;
}
For public transform I just added an empty game object to player chest level and parented it to the player. I got the rotating camera smoothly trick from an online tutorial but that exact thing won't work on player rotation though.
A character controller is attached to player without any rigidbody or collider.
You are going to want to use Lerp on the camera in order to smooth its motion from one position to another. The Scripting API has a good example of Vector3.Lerp.
So in your case, you could add a public variable for smoothing position as well. Something like positionSmoothTime. Then make a "Desired Position" variable, we'll call it destPosition.
Vector3 destPosition = player.position - transform.forward * distanceFromPlayer;
transform.position = Vector3.Lerp(transform.position, destPosition, positionSmoothTime);
This should effectively smooth it to where the stuttering should go away. Another thing that will help that someone else mentioned is using a part of the body that moves less (Like the head) as the target. This combined with the Lerp function will give you a smooth camera movement.
Another large help would be to move things into the Update() function. The reason being is FixedUpdate() doesn't get called every frame. So you could potentially get stutter as a result. If you move everything into Update() it will update every frame and help smooth things out. If you do this though you will need to multiply all movement by Time.deltaTime as shown in the example below.
Vector3 destPosition = (player.position - transform.forward * distanceFromPlayer) * Time.deltaTime;
For more examples, check the links to each function to see Unity's documentation on it. It has examples of everything I've shown here.

Rotate around an object to its direction of velocity

(For Unity 5.3.5f1)
Right now I am working on a 3D camera that is orbiting horizontally around the player. The player is a RigidBody rolling sphere. The player can freely rotate the axis horizontally but when there is no input I want the rotation to reset back to the direction of the velocity.
Right now all I need to know is how to situate the camera behind the player's direction of velocity by rotating the camera around the player from its previous rotation.
Here is a crude drawing of what I am looking for:
Currently to update the camera to orbit around the player I use this script on the camera (with comments):
using UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public GameObject Player;
//I assume you would use this PlayerRB to find data regarding the movement or velocity of the player.
//public Rigidbody PlayerRB;
private float moveHorizontalCamera;
private Vector3 offset;
// Use this for initialization
void Start ()
{ //the offset of the camera to the player
offset = new Vector3(Player.transform.position.x - transform.position.x, transform.position.y - Player.transform.position.y, transform.position.z - Player.transform.position.z);
}
// Update is called once per frame
void Update ()
{
moveHorizontalCamera = Input.GetAxis("Joy X"); //"Joy X" is my right joystick moving left, none, or right resulting in -1, 0, or 1 respectively
//Copied this part from the web, so I half understand what it does.
offset = Quaternion.AngleAxis(moveHorizontalCamera, Vector3.up) * offset;
transform.position = Player.transform.position + offset;
transform.LookAt(Player.transform.position);
}
}
Any help at all would be great!
I would suggest you use Vector3.RotateTowards (https://docs.unity3d.com/ScriptReference/Vector3.RotateTowards.html)
You take the Vector that is currently pointing from the Player to the camera (transform.position - Player.transform.position) and rotate it towards -Player.GetComponent<Rigidbody>().velocity.normalized * desiredDistance (the vector pointing in the opposite direction of the players velocity with magnitude corresponding the desired distance of the camera to the player). You can then use Vector3.RotateTowards to rotate the camera (smoothly or immediately) around the Player by setting the cameras position accordingly.
Something like:
transform.position = Player.transform.position + Vector3.RotateTowards(transform.position - Player.transform.position, -Player.GetComponent<Rigidbody>().velocity.normalized * desiredDistance, step, .0f);
where step if the angle update in radians. (Note: you should avoid calling GetComponent<Rigidbody>() every Update. You are better off storing it somewhere)
I hope I understood your question correctly and this helps.

Categories

Resources