I have two game object.
Both gameobject positions are different...
I attached same animation clip to both gameobject.
In one game object I am don't want any change in animation clip.
I just want to use same animation clip on different gameobject but animation timing is different
& In other Gameobject I am just want to play animation at 0.30 s but I not knowing how set position of this gameobject in script.
I am assign below script to second game object.
using UnityEngine;
using System.Collections;
public class ani3 : MonoBehaviour {
void Start () {
animation ["#cube"].time = 0.30f;
//how to set position of gameobjet
}
void Update () {
gameObject.animation.Play("#cube");
}
}
you can use a empty game object and nest one of the game object as child and change the position of the empty game object ,it is work for me.
Related
I'm new to Unity and C#. I'm trying to make the sprite of "bird" change when he dies in unity. I tried following some tutorials but it doesn't work, so now I'm just trying to make the sprite change when "A" is pressed, but it still doesn't work. Is it a problem of the script or of the sprite?
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeSprite : MonoBehaviour
{
public Sprite deadBird;
void Start()
{
}
void Update()
{
if (Input.GetKeyDown(KeyCode.A))
{
GetComponent<SpriteRenderer>().sprite = deadBird;
}
}
}
Here is a screenshot: https://i.stack.imgur.com/PTd9W.png
I think #derHugo is correct, if you have an Animator then that will most likely overwrite any change you try and do. To fix this, you can use that animator and create an animation that is the deadBird. once you have that, connect it to the animator controller from the normal state. in that connection you can create and set a new animator boolean like "isDead" and set it to switch to the dead animation is the bool is true. then change your code from
GetComponent<SpriteRenderer>().sprite = deadBird;
To
anim = gameObject.GetComponent<Animator>() //place this instead of the bird sprite
anim.SetBool("isDead", true); //place this in the if statement
Hope that helps! it's much cleaner to go through the animator as it allows for easier changes as you build your game.
Developing a 3D VR application on Unity using OpenXR (2021.3.11f1).
I'm trying to make it so that a Canvas is hidden until a certain condition is met. That condition is that another 3D object's x position is under 45. Here is my script right now:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OptionsBox : MonoBehaviour
{
public GameObject canvas;
public GameObject playerObj;
void Start()
{
canvas.SetActive(false);
}
void Update()
{
if (playerObj.transform.position.x < 45){
canvas.SetActive(true);
}
}
}
I then made an empty GameObject and inputted the script into there:
However, when I run my scene, the canvas is still displayed. What have I done wrong?
No problem with the script. Problem was the other object started at a x value less than 45, so it was always set to True.
You can add a CanvasGroup to the Canvas and play with the opacity property called Alpha
i want to make the characters in my game collide with objects around them and stop moving. the thing is the characters just spawn and keep walking down the map until they're out of the Canvas view.
my characters have Constantmove script that i assigned to them which is this one:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterConstantMove : MonoBehaviour
{
GameManager Game_Manager;
void Start()
{
GameObject gameController = GameObject.FindGameObjectWithTag("GameController");
Game_Manager = gameController.GetComponent<GameManager>();
}
void Update()
{
transform.Translate(Game_Manager.moveVector * Game_Manager.moveSpeed * Time.deltaTime);
}
}
What do i do to make them stop around the desk and just stay idle when they collide there?
You are moving them with transform.Translate() function, which does not include physics. If you want them to collide, you would have to add Rigidbody2D component and move them using velocity property: https://docs.unity3d.com/ScriptReference/Rigidbody2D.html
In my Unity game I have some moving objects. There are some collectables ( triggers ) and whenever a moving object enters its trigger the collectable should
Replace itself with a moving object
Destroy itself
Unfortunately the current moving object collides with the new spawned moving object so it will be out of position ( very little ). I would like to avoid that, so one piece connects smoothly to the other one.
For reproduction purposes:
Moving
Create a cube GameObject
Add a Rigidbody component but disable the usage of gravity
Attach the following script to it
.
public class MoveForwardBehaviour : MonoBehaviour
{
private void FixedUpdate()
{
GetComponent<Rigidbody>().velocity = Vector3.forward; // just for testing purposes
}
}
Make this GameObject a prefab
Collectable
Create a cube GameObject
Add a Rigidbody component but disable the usage of gravity
Enable the collider trigger and modify the following values
Attach the following script to it
.
public class Collectable : MonoBehaviour
{
[SerializeField] private GameObject movingPrefab;
private void OnTriggerEnter(Collider other)
{
GetComponent<Collider>().enabled = false;
Instantiate(movingPrefab, transform.position, transform.rotation);
Destroy(gameObject);
}
}
Assign the moving prefab to the script
Make this GameObject a prefab
Now setup a sample scene like so
and start the game. After creating a new moving prefab you can see that the initial moving prefab is not in position anymore
I think this is because of the collision with the new instantiated prefab. Do you have any suggestions how to avoid this?
How can I add the game object from above (Roed Knap, Hand etc.) into the script given below (at the picture)?
This is an example project. I can't figure out to reference GameObject's and Colliders into a Script.
What I want to do is very simple.
Make a GameObject with a Collider, and trigger something when collision happens.
So What I have is basically a GameObject Cube that has a Collider sphere added and for this isTrigger is selected. I want this trigger when entering, modify the text. Can you help me with initializing, referencing and other stuffs necessary, see below code. This is the code I work with.
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
GameObject Cube;
GUIText Text;
Collider collision;
// Use this for initialization
void Start () {
collision = Cube.GetComponent<Collider> ();
Text = GetComponent ("GuiText") as GUIText;
}
// Update is called once per frame
void Update () {
}
void onTriggerEnter(){
Text.text = "Won"
}
}
Unity uses C# syntax for all of MonoBehaviour and engine namings. That is, a method starts with cap letter:
void OnTriggerEnter(Collider col){}