Versions: Unity v2019.2.4f1, Oculus Utilities v1.41.0, OVRPlugin v1.41.0, SDK v1.42.0
I am in the process of creating an Oculus Rift game where the player can use buttons to toggle between walking around on the ground and looking around in a bird's eye view of the environment. To accomplish this, I am using the following script to toggle between two different OVRPlayerControllers, one positioned on the ground and one positioned in the air looking down:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraSwitcher : MonoBehaviour
{
public GameObject firstPersonCameraPrefab;
public GameObject overheadCameraPrefab;
void Update()
{
bool isDownOne = false;
bool isDownThree = false;
isDownOne = OVRInput.GetDown(OVRInput.Button.One);
isDownThree = OVRInput.GetDown(OVRInput.Button.Three);
if (isDownOne || isDownThree)
{
switchCam();
}
}
public void switchCam()
{
overheadCameraPrefab.SetActive(!overheadCameraPrefab.activeInHierarchy);
firstPersonCameraPrefab.SetActive(!firstPersonCameraPrefab.activeInHierarchy);
}
}
At the start of the game, the first person player is active, and the overhead player is not active. When I use this script, the headset toggles between looking through the two cameras. However, the connection between the game and the movement controls on the controllers gets severed after the first switch. When I toggle back down to the ground, I can no longer use my controllers to move my player around.
How can I switch between players and keep the movement controls in tact?
This is because you have more than 1 OVRManager in your scene, the prefab OVRCameraRig has 1 attached, so if you have more than one of this prefabs, like 2 players, Oculus won't work correctly.
Put the component in an empty prefab that never turns off, and set switch between your player controllers freely.
Related
currently i'm trying to animate an object by clicking or pressing a key. Unfortunately, script won't work and I have tried soooooo many other ways to do it.
Here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TurnBoard : MonoBehaviour
{
public Animator anim;
void Start()
{
anim = GetComponent<Animator>();
}
void OnTriggerStay(Collider player) {
if (player.tag == "Player" && Input.GetKeyDown(KeyCode.G)){
//Debug.Log("touchyy");
anim.SetTrigger("turn");
}
}}
I have assigned this script file to object itself. Object has a box collider. Animation has a trigger named "turn". When player enters to the collider zone, I want player to be able to activate animation of the object with a click/or keypress.
I do get "Debug.Log" when player enters the zone. So I believe that there is no problem in detecting the collision. But just can't manage to animate object in any way.
Any help? Thank you!
OnTriggerStay is run like FixedUpdate (since it involves physics), so none of the Input events will work correctly inside it for the same reason that they won't work right in FixedUpdate. All Input functions must only be used in Update method.
I would like to use a characters tongue so instead of shooting a bullet, it goes toward the enemy, licks it, and comes back. I got this wording from this question: Unity shooting with Tongue 2d game (hasn't been answered and is 4+ years old). The only difference is my character moves.
I have this code from looking at a shooting tutorial so when you click the tongue prefab generates and is at the correct angle. I need it to grow on click and shrink back.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LineController : MonoBehaviour
{
public GameObject player;
private Vector3 target;
public GameObject crosshairs;
public GameObject tonguePrefab;
// Start is called before the first frame update
void Start()
{
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
target = transform.GetComponent<Camera>().ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z));
crosshairs.transform.position = new Vector2(target.x, target.y);
Vector3 difference = target - player.transform.position;
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
if (Input.GetMouseButtonDown(0))
{
shootTongue(rotationZ);
}
}
IEnumerator shootTongue(float rotationZ)
{
GameObject t = Instantiate(tonguePrefab) as GameObject;
t.transform.position = new Vector2(target.x, target.y);
t.transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ);
yield return new WaitForSeconds(2000);
t.SetActive(false);
}
}
I was also trying to make it disappear after whatever time and with that it doesn't work at all?
If the prefab is just a line with a simple texture I suggest using a line renderer:
Create an empty transform that starts on the mouth and then it move untill it reaches the crosshair. The movement is done using Vector2.MoveTowards.
Your tongue will no be a line renderer component. This line renderer will have 2 points, the start which is static, and the end which you have to update on the Update() for example, to correspond to the positions of the empty transform in item 1.
If you really wish to expand a tongue gameobject instead, then you are going to need a bit of math, this answer has what you need but in 3D:
https://answers.unity.com/questions/473076/scaling-in-the-forward-direction.html
1) I suggest creating sprite/sprite sheet animations with Mecanim or relatively new Skeletal Animation with Anima2D. With these systems you can create nice animations, transitions, even animate the sphere collider to trigger collisions and actions, change active state of your objects, etc. and control the animation with very little code. This way you can get best effects in my opinion.
As tongue is not a bullet... :) You only need one. I don't think you want to Instantiate/Create prefabs every time you press the lick button. Instead you should just turn your tongue object on/off (gameObject.SetActive). You can also change your object active state within the animation. So if you don't know how to code it you can do most of it in the Animation window and use very simple code to play the animation when you press the lick button. Whenever a sphere collider touches something you can tell the Animator Controller to play a 'roll back' animation and it will transition nicely to the start position.
There are many tutorials about Mecanim, Animations, 2D Animations, Anima2D, Animator Controller out there.
2) If you need very good control over the tongue you could create a custom mesh and control it via script but this is far more difficult.
3) The reason why your object is not turning off is probably because you wrote WaitForSeconds(2000) so it will turn off after 2000 seconds - more than half an hour. You should also call it with StartCoroutine(shootTongue()) as it is a Coroutine. Again if you want to turn off the object don't create new ones every time. If you want to keep creating new objects you should Destroy the objects instead. Otherwise you will end up with a lot of deactivated tongues in your scene and I don't think you needs that many tongues.
I want to create an analog glitch effect in unity for may main camera when my game character/ camera looks at a certain object.
I have seen it in this youtube video, but the part I am struggling with was not explained.
I am using this Glitch .unitypackage from Github (link).
I attached the AnalogGlitch Script from the package to my main camera game object, but this will apply the glitch effect the entire time.
Instead I want to enable the glitch effect when my character/ camera faces this certain object. I have a working animator controller, which enters a "facing state" when my camera looks at the object and a "Not facing state" when looking somewhere else (as seen in the next image).
Ideally I would just enable/ disable the AnalogGlitch.cs Script from my main camera game objects when this animation starts. I tried setting up a EnableGlitch.cs script for this, but it will not let me reference any game object in my scene. What am I doing wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnableGlitch : StateMachineBehaviour
{
public GameObject maincamera;
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
//maincamera.GetComponent<AnalogGlitch>().enabled = true;
}
}
Also is there a better way to control the AnalogGlitch.cs script from the animator controller? I would also like to control the variables within this script to modify the effect while in this animation state.
I want to set my PS4 controller R2 trigger to shoot bullets in the VR game i am making for google cardboard.
I have paired the controller to the phone and i am already using it to move around and jump with the FPSController Asset and i made a shooting script
using System;
using UnityEngine;
using UnityStandardAssets.CrossPlatformInput;
using UnityStandardAssets.Utility;
public class Firing : MonoBehaviour
{
//Drag in the Bullet Emitter from the Component Inspector.
public GameObject Bullet_Emitter;
//Drag in the Bullet Prefab from the Component Inspector.
public GameObject Sphere;
//Enter the Speed of the Bullet from the Component Inspector.
public float Bullet_Forward_Force;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("joystick button 0"))
{
//The Bullet instantiation happens here.
GameObject Temporary_Bullet_Handler;
Temporary_Bullet_Handler = Instantiate(Sphere, Bullet_Emitter.transform.position, Bullet_Emitter.transform.rotation) as GameObject;
//Sometimes bullets may appear rotated incorrectly due to the way its pivot was set from the original modeling package.
//This is EASILY corrected here, you might have to rotate it from a different axis and or angle based on your particular mesh.
Temporary_Bullet_Handler.transform.Rotate(Vector3.left * 90);
//Retrieve the Rigidbody component from the instantiated Bullet and control it.
Rigidbody Temporary_RigidBody;
Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
//Tell the bullet to be "pushed" forward by an amount set by Bullet_Forward_Force.
Temporary_RigidBody.AddForce(transform.forward * Bullet_Forward_Force);
//Basic Clean Up, set the Bullets to self destruct after 10 Seconds, I am being VERY generous here, normally 3 seconds is plenty.
Destroy(Temporary_Bullet_Handler, 3.0f);
}
}
}
I want to boot the "game" on the phone and to press R2 to shoot the bullets
I am new to coding, so please go easy and try to make this as simple as possible. I am surprised that what I am trying to do appears to be so difficult.
I have two objects, A and B. They each have a different animation, 1 and 2. I want the user to click on A and watch animation 1, or to click on B and watch animation 2. I also want each object to play their relevant animations and then change to different scenes.
Can this be done just in the Animator window without any code using bools or triggers and transitions? (I have tried multiple ways and can't work it out.)
If it needs a code attached to objects A and B, I have a trigger animation script (attached below) but it only plays one of the animations and is just if the mouse button is clicked, not by clicking on object A or B.
I can successfully do what I need with a change scene script: objects A and B have the change scene script attached and two different scenes to change to. How is the animation I want to play different and seemingly so complicated to implement? I haven't needed to declare any gameobjects in that script. Perhaps I need to GetComponent and something to do with PlayAnimation, but I have searched and searched and am surprised no one else has had this issue, or perhaps I'm not asking in the correct way. There are many tutorials that tell you how to play one animation, but not multiple.
using UnityEngine;
using System.Collections;
/// <summary>
/// The TriggerAnimation class activates a transition whenever the Cardboard button is pressed (or the screen touched).
/// </summary>
public class TriggerAnimation : MonoBehaviour
{
[Tooltip("The Animator component on this gameobject")]
public Animator animator;
[Tooltip("The name of the Animator trigger parameter")]
public string triggerName;
void Update()
{
// If the player pressed the cardboard button (or touched the screen), set the trigger parameter to active (until it has been used in a transition)
if (Input.GetMouseButtonDown(0))
{
animator.SetTrigger(triggerName);
}
}
}
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class ChangeScene : MonoBehaviour
{
public void GoToScene(string sceneName)
{
Debug.Log("I'm going to change scenes!");
SceneManager.LoadScene(sceneName);
}
}
Thank you so much in advance. I'm really stuck. I am trying to learn by following tutorials and using Sololearn, but I'm finding it very difficult to translate what I'm learning into things I want to do.
Here's the updated code:
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
/// <summary>
/// The TriggerAnimation class activates a transition whenever the Cardboard button is pressed (or the screen touched).
/// </summary>
public class TriggerAnimation : MonoBehaviour
{
public Animator animatorVR;
public Animator animatorAR;
public void OnPointerClick(PointerEventData pointerEventData)
{
Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);
}
void Update()
{
// If the player pressed the cardboard button (or touched the screen), set the trigger parameter to active (until it has been used in a transition)
//OnPointerClick;
{
animatorVR.SetTrigger(onPressVR);
animatorAR.SetTrigger(onPressAR);
}
}
}
I know this doesn't work but am not sure where to go from here.
Detecting a click on an object
Input.GetMouseButtonDown only checks the current state of the button, without any respect as to where the mouse is pointing. You're executing it within the Update event method, which runs every single frame.
The easiest way to detect if you're clicking an object is to put a Collider component (of any shape) on the object and use the OnMouseDown event function.
As noted in the comments by Galandil, OnMouse methods shouldn't be used for touch controls.
Running animations for two different objects
The Animator component can control a tree of objects (the object with the component, but also its children). You can do what you want without any additional code if you put objects A and B under a common parent object with Animator. Otherwise, yeah, you'll have to reference the other object somehow, e.g put something like this on both objects:
[Tooltip("The Animator component on this gameobject")]
public Animator animator;
[Tooltip("The Animator component on the other gameobject")]
public Animator otherAnimator;
...
animator.SetTrigger(triggerName);
otherAnimator.SetTrigger(triggerName);