How to stop cinemachine freelook camera rotation - unity3d

I am working on a third person parkour game with new input system and cinemachine(Freelook). You move with the right joystick and look around using right joystick(Cinemachine Input provider). For doing tricks I am using button with one modifier, you press R2 and move the right joystick in different directions for different tricks. But when I move the right joystick for a trick cinemachine rotates the camera.
Is a way to stop cinemachine from rotating the camera when other buttons are pressed.
Thank you in advance, Hemanth

You can simply disable the component.
public CinemachineFreeLook freeLook;
private void Lock() => freeLook.enabled = false;
Another way is to set the Mouse Axis Speed to zero. In this method, with the help of a tweener, you can gently disable the movement of the mouse.
private void Lock()
{
DOVirtual.Float(freeLook.m_XAxis.m_MaxSpeed, 0f, 1f, value => freeLook.m_XAxis.m_MaxSpeed = value);
DOVirtual.Float(freeLook.m_YAxis.m_MaxSpeed, 0f, 1f, value => freeLook.m_YAxis.m_MaxSpeed = value);
}

freeLook.m_YAxis.m_MaxSpeed = 0;
freeLook.m_XAxis.m_MaxSpeed = 0;
You can just set the values to 0

you can probably remove the component that will probably help

Related

Detect collisions between 2 objects

I'm trying to do a little game on mobile using Unity and I've got a problem with the rotation of a maze.
To add context :
When your moving your finger on the screen, the maze is rotating on himself. There is a ball in it and you need to make it go on a cell to win the level.
When the maze is rotating too fast, the ball falls down and go through the ground and I don't know how to fix it.
I tried to play with the gravity, colliders...
This is the same when the ball is jumping (when the maze is going up and down quickly).
For the moment I just reset the ball position when you're falling.
{
ball.transform.position = new Vector3(0, 2, 0);
maze.transform.position = Vector3.zero;
maze.transform.rotation = Quaternion.identity;
}
Do you guys have some ideas ? Thanks
I had a similar problem in a tilt maze mini-game I worked on. Ideally implementing jkimishere's solution will work but I assume the maze is moving too fast for the collisions to register properly. You'll need to smooth the maze's rotation with a Lerp. In our case we had pressure plates with a tilt value, so it doesn't directly translate to your mobile use but perhaps give you a nudge in the right direction. We used:
public GameObject maze;
private float _lerpTime;
private bool _isRotating;
private Quaternion _startingRot, _desiredRot;
private void Awake()
{
_startingRot = maze.transform.localRotation;
}
private void Update()
{
//Don't want to move the maze if we don't ask for it
if(!_isRotating)
return;
//Lerp the maze's rotation
_lerpTime = Mathf.Clamp(_lerpTime + Time.deltaTime * 0.5f, 0f, 1f);
maze.transform.localRotation = Quaternion.Lerp(_startingRot, _desiredRot, _lerpTime);
//Once the maze gets where it needs to be, stop moving it
if(affectedObject.transform.localRotation.Equals(_desiredRot)
_isRotating = false;
}
private void ActivateTilt()
{
//Set the new starting point of the rotation.
_startingRot = maze.transform.localRotation;
//However you want to calculate the desired rotation here
//Reset our lerp and start rotating again
_lerpTime = 0f;
_isRotating = true;
}
This will ease the rotation of your maze over time. So that the ball can adapt to the new collider positions.
In the rigidbody(for the ball), make the collision detection to continuous, and in the rigidbody for the maze(if you have one) set the collision detection to continuous dynamic. Hope this helps!
I think that is unavoidable if you allow the player to move the platform freely. I suggest you restrain the speed at wich the player can tilt the platform. This way, the ball will have more frames to adapt to the new slope

move object and it's children out of camera in unity2d

I have made a gameobject together with some children gameobject to represent the information to show up when specific circumstances occurred.
I have already ajusted the position of the information gameobject(together with its' children) in the cameraarea. The thing is that I want to move the gameobject(together with its' children) out of the camera, maybe on top or maybe on left. Following is the scratch to demonstrate the position I want to put it:
So that I could move the information gameobject and its' children (Red box) with some movement effect when needed, I have no problem with moving it back but could find an elegant way to move it out of the camera when the game started.
Mostly because I don't know how to calculate the position out of the camera.
Maybe find the upper border of the camera and the size of the gameobject and its children?
I know I could do this by maybe add a marker gameobject to represent the downer border of the information gameobject, and move it until it's not visible, but is there a more elegant way?
Any ideas?
For this one, I would use the following trick: use any method (animation, coroutine, Update method...) to move your item out of screen the way you desire. Then you can use the OnBecameInvisible event which is called when the item does not need to be rendered on any camera anymore. The event will there be used to detect that the parent object moved out of screen, and that you want to stop the current movement. You then just need to define in this event that you want to stop the current moving behavior, and you will be done.
void OnBecameInvisible() {
// Stop moving coroutine, moving in Update or current animation.
}
There are probably more elegant ways of doing it as you said, but I think this method should be enough for what you want to achieve.
It took me time, but I found this way for you, attach this script to your gameObject:
public Renderer rend;
//drag the camera to the script in the inspector
public Camera camera1;
Vector3 bottomleft;
Vector3 topright;
void Start()
{
rend = GetComponent<Renderer>();
//the top-right point of the camera bounds
topright= camera1.ViewportToWorldPoint(new Vector3(0, 0, transform.position.z));
//the bottom-left point of the camera bounds
bottomleft = camera1.ViewportToWorldPoint(new Vector3(1, 1, transform.position.z));
StartCoroutine(MoveUp());
}
IEnumerator MoveUp()
{
//while the position and the height are lower that the Y of top right
while (transform.position.y + rend.bounds.size.y < topright.y)
{
//move the object to the new position (move it up)
transform.position = new Vector3(transform.position.x, transform.position.y + .01f, transform.position.z);
//and wait for 1/100 of a second
yield return new WaitForSecondsRealtime(.001f);
}
}
you can play with the WaitForSecondsRealtime value to change the velocity of moving.

Unity2D: currentSelectedGameObject panel disabled

Okay, so I have a click to move game and a panel in which I do not want my player to move towards. However the panel is disabled at first (for an effect I'm doing) until the user clicks on a button to SetActive the panel. I used currentSelectedGameObject to block my player going to towards the panel but it isn't working, maybe because the panel was disabled in the first place, I not sure just spitting out ideas. Hopeful someone can help me.
using UnityEngine.EventSystems;
public GameObject currentSelectedGameObject;
public void Update () {
if (Input.GetMouseButtonDown (0)) {
if (EventSystem.current.currentSelectedGameObject)
return;
Vector3 mousePosition = Input.mousePosition;
mousePosition.z = 10; // distance from the camera
target = Camera.main.ScreenToWorldPoint (mousePosition);
target.z = transform.position.z;
}
transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.fixedDeltaTime);
}
Thank you. :)
Use the button to setactive the panel and if not , you are not using the public GameObject currentSelectedGameObject variable in your code , do this :
if (EventSystem.current.currentSelectedGameObject == currentSelectedGameObject) // now this is your variable that you declared being used in the if statement
I cant remember correctly but mb its like this EventSystem.current.currentSelectedGameObject.name

Unity Navmeshagent won't face the target object when reaches stopping distance

I'm trying to get the NPC to look at the main character when I'm talking to him. I need to make sure it looks natural and that he is facing me. I know I can do Transform.LookAt() but that is too instant and unnatural.
How do I rotate the navmeshagent to face my character when its stopped moving?
Try this out to control the body orientation - the slerp is adjustable to your desired rotation speed (https://docs.unity3d.com/ScriptReference/Quaternion.Slerp.html):
private void FaceTarget(Vector3 destination)
{
Vector3 lookPos = destination - transform.position;
lookPos.y = 0;
Quaternion rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, [fill in desired rotation speed]);
}
if(agent.remainingDistance < agent.stoppingDistance)
{
agent.updateRotation = false;
//insert your rotation code here
}
else {
agent.updateRotation = true;
}
This will rotate your agent when it's distance below the stoppingDistance variable. However it will look inhuman, so if you're going for a humanoid ai I'd recommend looking at the unity Mecanim demo (particularly the locomotion scene) as it has code and animations that will properly animate the agent.
Maybe try this Head Look Controller. It's very smooth and blends with animations!
Put the char in a game object and copy the navmesh from the char to the parent, uncheck enable in char. move any scripts up too.
Just spent 5 hours to find this.

Camera Zoom when Player Died and Focus the player position c#

i am developing endless game . when player dies then camera zoom and focus on player position. i try and almost done. To zoom the camera i use orthographicSize and focus to the player i use transform.LookAt() that focus the player position when player dies.but the problem is when camera Zoom the scene then entire scene gets rotate. i have created CameraScript and attached to the maincamera .
[SerializeField]
private Camera gameCam;
[SerializeField]
private Transform[] target;
IEnumerator ZoomIn(){
while (gameCam.orthographicSize > 3) {
yield return new WaitForSeconds (0.01f);
transform.LookAt (target [index]);
gameCam.orthographicSize -= 0.1f;
}
}
public void ZoomCam(){
StartCoroutine (ZoomIn ());
}
Help me to that Script if any mistake..Thanks..
I think it's happening because you're not setting the worldUp parameter when you're calling transform.LookAt(...).
I suggest using the camera's own current up vector as that parameter, i.e.
transform.LookAt (target [index], transform.up);
I hope that helps!
EDIT:
Of course, this would only work if your camera is originally oriented as you intend it to be. Else, you'll have to use the vector you choose as the second parameter there.