I'm new to IOS and just tried to make the simplest game just for test it on IOS Simulator.
Here is the all code
using UnityEngine;
using System.Collections;
public class Controller : MonoBehaviour {
private Rigidbody2D rb;
// Use this for initialization
void Start () {
rb = transform.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update () {
if(Input.GetMouseButton(0) || Input.touchCount > 0)
{
rb.AddForce(Vector2.up, ForceMode2D.Impulse);
}
}
}
As you see, when user press the button or just touch the screen, some object fly up.
Of course in Unity it works fine, but when I make IOS build (with IOS Simulator SDK) and try to run it in XCODE it runs, but when I press a mouse button - controlled object does not fly up.
Here is the video of all that process.
What could be the problem? Why it doesn't work?
Related
I'm trying to create a Unity 2018 1.4f1 project that will play a specific animation when a specific key is pressed and will play a copy of the animation if the same key is pressed while the first instance is still playing.
The idea is that the user can type a word and for every letter they input an animation is played to represent that letter.
I've tried using things like Animation.PlayQueued to queue up animations but to no success.
Here's what my basic code looks like (this is only trying to play an animation on a key press):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AnimateKey : MonoBehaviour
{
public Animator animator;
// Use this for initialization
void Start()
{
animator = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown("1"))
{
animator.Play("Take1");
}
}
}
Any help would be greatly appreciated.
From this thread
You can check if an Animator already is in a certain state using GetCurrentAnimatorStateInfo and only make the call if IsName returns false:
if (!animator.GetCurrentAnimatorStateInfo(0).IsName("Take1"))
{
animator.Play("Take1");
}
Note: The parameter of GetCurrentAnimatorStateInfo is the layer index. So when you work with multiple layers you'll have to adopt that.
For multiple parallel Animations however you might want to checkout the Animation component maybe.
There you don't have to handle States but rather can simply start and stop AnimationClips. You would than do
public Animation animation;
private void Awake()
{
animation = GetComponent<Animation>();
}
private void Update()
{
if(!Input.GetKeyDown("1")) return;
if(animation.IsPlaying("Take1")) return;
animation.Play("Take1");
}
Using Unity2017.3.1f1 Personal (64 bit) to build a VR app for Android, using Cardboard VR SDK. The purpose of the app is to allow users visualize data in an immersive way.
Currently want to do something similar to the scene view where one can move forward by going where one's looking but with the Cardboard single button i'm just gonna give the ability to move forward.
Built a Canvas where user can select what type of locomotion he/she wants: teleport or walk.
The teleport works fine as you can see here.
When the user selects walk, the following error shows in the Console:
NullReferenceException: Object
reference not set to an instance of an
object
Player.TryWalk () (atAssets/Player.cs:44)
Player.Update () (at Assets/Player.cs:37)
My Player script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public enum InputMode
{
NONE,
TELEPORT,
WALK
}
public class Player : MonoBehaviour {
public static Player instance; //Singleton design pattern: only one instance of the class appears
public InputMode activeMode = InputMode.NONE;
[SerializeField]
private float playerSpeed = 3.0f;
void Awake()
{
if(instance != null)
{
GameObject.Destroy(instance.gameObject);
}
instance = this;
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
TryWalk();
}
public void TryWalk()
{
if(Input.GetMouseButton(0) && activeMode == InputMode.WALK)
{
Vector3 forward = Camera.main.transform.forward;
Vector3 newPosition = transform.position + forward * Time.deltaTime * playerSpeed;
transform.position = newPosition;
}
}
}
The Player script was added as component of Player:
When the button Walk is pressed, the Active Mode changes to WALK, as you can see in the next image.
Still, even though this happens, the user is not able to Walk.
What can I do to solve this?
Camera.main; was returning null.
In order to fix it, had to have the camera in my scene tagged MainCamera as you can see in the next image.
See it working here.
The states and animations not my own.
I have a prefab of a character that have Animator component attached to it. The character in the Hierarchy have Animator with some states and blends and also have some animation clips.
Now when i play/run a State of the Animator is that meaning i'm playing animation? Or the animation clips is something else ?
And why i can't playing the animation clips ? I'm not getting errors or exceptions it's just not playing it.
Here is a script that attached to this character and i also added to this character a Animation component. So now the character object have both Animator and Animation components.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class SwitchAnimations : MonoBehaviour
{
private Animator animator;
private int index = 0;
private static UnityEditor.Animations.AnimatorController controller;
private UnityEditor.Animations.AnimatorState[] an;
// Use this for initialization
void Start()
{
animator = GetComponent<Animator>();
an = GetStateNames(animator);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
animator.Play(an[index].name);
if (++index == an.Length)
index = 0;
}
}
private static UnityEditor.Animations.AnimatorState[] GetStateNames(Animator animator)
{
controller = animator ? animator.runtimeAnimatorController as UnityEditor.Animations.AnimatorController : null;
return controller == null ? null : controller.layers.SelectMany(l => l.stateMachine.states).Select(s => s.state).ToArray();
}
private void RollSound()
{
}
private void CantRotate()
{
}
private void EndRoll()
{
}
private void EndPickup()
{
}
}
Inside the variable an there are 9 states:
Aiming
Death_A
Use
Grounded
Roll
PickupObject
Reload_Rifle
Shoot_Rifle
Empty
When i click on A key it's playing the states one by one each time i click on A Everything is working fine i can play the 9 states by clicking on A.
what i don't understand with the States is inside the editor when i'm in the hierarchy selecting the character and in the menu make: Window > Animator i see the 9 states on the LegsLayer:
But then if i click on state Grounded double click i see a blend tree and i'm not sure if this is also states or maybe this is animation clips ? Then how can i get access to them ?
Ok this is one part i don't understand yet about the Animator and what and how i can play with it.
The second part is related to AnimationClip/s and why i can't play them ?
The script for animations clips:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
public class SwitchAnimations : MonoBehaviour
{
private Animation animation;
private AnimationClip[] animationClips;
// Use this for initialization
void Start()
{
animation = GetComponent<Animation>();
animationClips = AnimationUtility.GetAnimationClips(gameObject);
foreach (AnimationClip clip in animationClips)
{
animation.AddClip(clip, clip.name);
}
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
animation.clip = animationClips[6];
animation.Play(animationClips[6].name);
}
}
private void RollSound()
{
}
private void CantRotate()
{
}
private void EndRoll()
{
}
private void EndPickup()
{
}
private void FootStep()
{
}
}
And in the variable animationClips i have 9 animations:
Rifle_Aiming_Idle
Rifle_Aiming_Walk_F_RM
Rifle_Aiming_Walk_B_RM
Rifle_Aiming_Walk_L_RM
Rifle_Aiming_Walk_R_RM
Rifle_Idle
Rifle_Walk_F_RM
Rifle_Run_F_RM
Rifle_Sprint_F_RM
When i'm running the game and clicking on A this time i see in the Inspector inside the Animation component all the animation clips and in Animation i see the animation clip to play.
I'm not getting any errors or exceptions but it's just not playing it. The character stay still idle. Even if i choose another animation clip index to play 5 or 3 or 1 nothing is playing.
I wonder why it's not playing the animations when i click A. In this case animation number 6 from the List but still not playing.
The Animation component is a legacy component, kept in Unity to ensure backward compatibility with older projects, so you should never use it for animations.
Basically, the system works this way:
First of all, you need an Animator Controller asset. This Animator is a Finite State Machine (with substates etc.), and every single state can have an Animation Clip (you assign it via script of via Inspector in the Motion field). When the FSM enters a specific state, it will play the Animation Clip assigned.
Clearly the Animator, in order to be used by a game object, has to be assigned to that object via the Animator component.
Animation Clips are assets which contain the actual animation, they're never assigned as components to game objects, but instead they must be referenced by an Animator to be played, as I said before.
Blend trees are used to blend different animations in a seamless way by using linear interpolation. They're a "special" state of the Animator, which has multiple Animation Clips that you can interpolate to display transitions from a state to another, for example when you need to change the animation among running forward, running left and running right.
The argument is very broad, you can start to get into it by reading the official documentation about Animators, Animation Clips and Blend Trees here:
https://docs.unity3d.com/Manual/AnimationSection.html
I'm creating a project using the Gear VR, where you can rotate an object and display information based on the swipe and tap controls on the side of the headset.
Everything works great, I can rotate and select stuff when I use the touchpad on the side of the Gear VR, but when I change scenes and return to the main menu, and then go back into the scene I was just on, the functionality stops working.
I am using this script I've made:
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using System;
public class GearVRTouchpad : MonoBehaviour
{
public GameObject heart;
public float speed;
Rigidbody heartRb;
void Start ()
{
OVRTouchpad.Create();
OVRTouchpad.TouchHandler += Touchpad;
heartRb = heart.GetComponent<Rigidbody>();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
SceneManager.LoadScene("Main Menu");
}
}
void Touchpad(object sender, EventArgs e)
{
var touches = (OVRTouchpad.TouchArgs)e;
switch (touches.TouchType)
{
case OVRTouchpad.TouchEvent.SingleTap:
// Do some stuff
break;
case OVRTouchpad.TouchEvent.Up:
// Do some stuff
break;
//etc for other directions
}
}
}
I've noticed that when I start my game, an OVRTouchpadHelper is created. I don't know if that has anything to do with my problem.
The error I am getting is:
MissingReferenceException: The object of type 'GearVRTouchpad' has
been destroyed but you are still trying to access it. Your script
should either check if it is null or you should not destroy the
object.
BUT I have not referenced this script anywhere else.
When I check my scene in play mode, the script is still there with the variable assignments still present.
Any help would be great!
OVRTouchpad.TouchHandler is a static EventHandler (so it will persist through the lifetime of the game). Your script is subscribing to it when it's created but isn't unsubscribing when it's destroyed. When you reload the scene the old subscription is still in the event but the old GearVRTouchpad instance is gone. This will result in the MissingReferenceException next time the TouchHandler event fires. Add this to your class:
void OnDestroy() {
OVRTouchpad.TouchHandler -= Touchpad;
}
Now, whenever a GameObject with the GearVRTouchpad behaviour is destroyed, the static event in OVRTouchpad will no longer have a reference to it.
Still learning here and starting of with basics (just a plain Plane and moving as the first person around)
However i can run the app and look around and up and down etc. but can;t make the camera move when touching the touchpad on the GearVR headset.
I have created the script (c#) and attached to the camera in unity:
using UnityEngine;
using System.Collections;
public class Moving : MonoBehaviour {
// Use this for initialization
void Start()
{
if (Input.GetButton("Tap"))
{
// Do something if tap starts
}
if (Input.GetButtonUp("Tap"))
{
// Do something if tap ends
}
}
// Update is called once per frame
void Update () {
if (Input.GetButton("Tap"))
{
// Do something if tap starts
}
if (Input.GetButtonUp("Tap"))
{
// Do something if tap ends
}
}
}
But it still doesn't seem to work. When i build and run the app it does nothing :-(
I know i am doing something wrong but not sure what.
Handling Single Tap in Oculus Gear VR:
void Start()
{
OVRTouchpad.Create();
OVRTouchpad.TouchHandler += OVRTouchpad_TouchHandler;
}
void OVRTouchpad_TouchHandler (object sender, System.EventArgs e)
{
OVRTouchpad.TouchArgs touchArgs = (OVRTouchpad.TouchArgs)e;
OVRTouchpad.TouchEvent touchEvent = touchArgs.TouchType;
if(touchArgs.TouchType == OVRTouchpad.TouchEvent.SingleTap)
{
// Your response to Tap goes here.
}
}
Tap and Hold to Move:
You need to register Tapkey in your input following this
tutorial.
Add OVRPlayerController prefab to move around the scene. This will
let you move using keyboard W/A/S/D keys in Unity Editor.
Next you need to integrate Tap key with OVR Player controller script
to move in forward direction.
here are some useful links:
http://forum.unity3d.com/threads/gear-vr-touchpad-fps-script.373103/
http://rifty-business.blogspot.co.uk/2014/04/unity-pro-4-using-ovrcameracontroller.html