How can I control each blend shape value from a script using the blend shape name not by index? - unity3d

For example I want to change the mouth blend shape value from 0 to 23 randomly to simulate talking when I'm showing text. It's no really the lips will move like the text it's just to simulate talking when the text show so I want to make something more or less randomly values between 0 and 23.
I tried this but it's not working it does nothing. It's not changing the mouse blendshape value at all.
The scripts is attached to the object with the blendshapes.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BlendShapesController : MonoBehaviour
{
private SkinnedMeshRenderer bodySkinnedMeshRenderer;
// Start is called before the first frame update
void Start()
{
bodySkinnedMeshRenderer = GetComponent<SkinnedMeshRenderer>();
}
// Update is called once per frame
void Update()
{
int rand = Random.Range(0, 23);
bodySkinnedMeshRenderer.SetBlendShapeWeight(0, rand);
}
}

Solution : Since the object with the SkinnedMeshRenderer is a child and the parent have a Animator component I had to change the Animator component settings on the parent object to Animate Physics and Always Animate.
I'm using by index not by name but it's working so I will consider it for the meanwhile as a solution since it's working.

Related

Making a sprite move

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnitMove : MonoBehaviour
{
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Vector3 MousePos = Input.mousePosition;
GetComponent<Transform>().position = (MousePos);
}
}
}
What is above is my code and every time I build and run the game the sprite will not go to the position of my mouse clicks but instead just disappears.
Input.mousePosition provides a screen coordinate. This ranges from (0,0) to (Screen.width, Screen.height). The Transform component uses world coordinates. Try looking at its position value after clicking the sprite and see if the X and Y values look like pixel counts.
Use Camera.ScreenToWorldPoint() to transform screen coordinates into world space.

Unity3D: Raycaster.Raycast on UI, get Worldposition of the hit point

I am in UNity3D. I am trying to raycast on UI Element and get the world position of the hit. I have the small test code here. It gives me the name of the target UI Element, but not the position. The world position of the hit is always (0,0,0). Please, can someone suggest, how i can get it right? thank you a lot.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class RaycasterRayScriptTest : MonoBehaviour
{
[SerializeField] private GraphicRaycaster m_Raycaster;
private PointerEventData m_PointerEventData;
private EventSystem m_EventSystem;
[SerializeField] private Vector3 HitPosition;
// Update is called once per frame
void Update()
{
m_PointerEventData = new PointerEventData(m_EventSystem);
m_PointerEventData.position = Input.mousePosition;
List<RaycastResult> results = new List<RaycastResult>();
m_Raycaster.Raycast(m_PointerEventData, results);
foreach (RaycastResult result in results)
{
Debug.Log("Hit " + result.gameObject.name);
HitPosition = result.worldPosition;
Debug.Log("HitPosition " + HitPosition.ToString());
}
}
}
I just solved this problem. You have to set Canvas Render Mode: Screen Space - Camera. Select the camera, that you have to use for the UI. With this UI-Plane will appear as an GameObject in the world. But the ui-plane position is not adjustable, so you have to move the camera back and adjust the field of view of the camera.
And because the UI-Plane is now an Object, the code
HitPosition = result.worldPosition;
works now.
without the changes on the canvas and camera, the code doesn't work, because plane of ui is virtual and it's position set to the camera position. So if you try to get the worldPosition, it uses the distance betwenn camera and UI-Plane, and since it is zero, you will get zero for the worldPosition as well.

Unity3D Getting position of OVRCameraRig

I want to attach an object to the OVRCameraRig and then use its position, which is offset from the rig.
However, my object is always static, irrespective of where the headset is.
This only happens with the OVRCameraRig. If I use a normal MainCamera I get the right data. But I'm not getting other aspects, like floor level, of the OVRCameraRig!
Is there some way to get the actual position of the OVRCameraRig?
Afaik the OVRCameraRig itself doesn't move.
What you probably want to get is the position of the centerEyeAnchor instead
// somehow get the reference e.g. using GetComponent
OVRCameraRig overCameraRig;
var position = overCameraRig.centerEyeAnchor.position;
Regardless of the value of usePerEyeCameras the centerEyeAnchor's position is always updated.
Try the following code to get the OVRCameraRig position using the centerEyeAnchor attribute.
using UnityEngine;
public class OVRCameraPosTest : MonoBehaviour {
[SerializeField] private OVRCameraRig overCameraRig;
void Start() {
Vector3 cameraPos = GetCameraPos();
Debug.Log("Camera Position: " + cameraPos);
}
Vector3 GetCameraPos() {
// Remove this line if you are refering the OVRCameraRig component
// through the inspector to the script.
overCameraRig = GameObject.Find("OVRCameraRig").GetComponent<OVRCameraRig>();
return overCameraRig.centerEyeAnchor.position;
}
}

Have slider value set both by script and in UI using slider handle

So I have a UI Slider on my Canvas. I am also changing the value of the slider (and therefore position of the handle) from a script. But now the slider cant be move in the UI on runtime anymore. Once I remove the line in my code, it works again. Is there any way to change the value both from the script and in the UI via mouse at runtime, i.e. stop unity from blocking it in the UI when changing it in the script?
#Immersive was absolutely right. The value was changed within Update() on every frame. Changed it so it only is changed when necessary.
Thanks a lot.
using UnityEngine;
using UnityEngine.UI;
public class SliderExample : MonoBehaviour
{
public Slider slider;
public Text displaySliderValue;
private float sliderValue;
private void Update()
{
displaySliderValue.text = sliderValue.ToString();
sliderValue = slider.value;
}
}

Adding Animation to Inputfield and save the transform

I wanted to add a simple search animation to my input field in my unity UI.
This is my InputField and the idea is, when I select it, it should expand slowly, and when I deselect it, it should shrink back to its normal form.
This is the rect Transform Component of this inputfield. I added to the input field and Event Trigger Component and an Animator. I created two Animations called SearchAnimation and DeselectAnimation and added them to my AnimationController called 'SearchController'.
This is how I designed my SearchController:
I set the Transitions between the defaultState and SearchAnimation to be listening to the SelectBool and DeselectBool (the name already describes its purpose).
Then I added the following script to my input Field so that those two booleans will be set accordingly to the event trigger:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class OnClickScript : MonoBehaviour {
Animator anim;
void Start()
{
anim = GetComponent<Animator>();
}
public void OnSelect()
{
anim.SetBool("SelectBool", true);
anim.SetBool("DeselectBool", false);
GetComponent<RectTransform>().sizeDelta = new Vector2(450, 50);
GetComponent<RectTransform>().localPosition.Set(-275, 0, 0);
}
public void OnDeselect()
{
anim.SetBool("DeselectBool", true);
anim.SetBool("SelectBool", false);
GetComponent<RectTransform>().sizeDelta = new Vector2(200, 50);
GetComponent<RectTransform>().localPosition.Set(-130, 0, 0);
}
}
But after the animation is played the inputfield is set back to its inital size and location. How do I fix this?
Easy way:
Create one more clips. The clip is made of only one key which is the Size Delta of the SelectedState. Then make this clip the animation clip of the state.
I put my test project here.
With above approach, once you want to change the size of default state and selected state, you will have to change all four animation clips manually.
Hard way
Using AnimationClip.SetCurve to create animation from the script. With this approach, you can create more maintainable animations. But it's not easy to create complex animations with scripts.
Suggestions:
Using Pivot:
In the script, you are changing local position to prevent the input field moving up. Instead of changing the local position value, you can set the pivot Y to 1 if you want the input field to expand downward.
Using trigger:
Instead of using two bool variables, you can simply use one Trigger to trigger the animation start and move to next state.