How I can open a door with animations? - animation

I have this script:
Ray ray = new Ray (cam.transform.position, cam.transform.forward);
RaycastHit hit;
Debug.DrawRay (transform.position, ray.direction * 50f);
if (Input.GetKeyDown (KeyCode.E)) {
if (Physics.Raycast (ray, out hit, 50.0f)) {
if (hit.collider.gameObject.tag == "Door") {
Debug.Log ("YEAH");
}
}
}
How I can start animation open door?

There are a few things that you need to know before you can animate that door.
There are multiple ways to animate an object in Unity. For simple things like that you could decide to just rotate the object (but you need to understand that you will need a Coroutine or a Mathf.MoveTowards or Mathf.Lerp method to avoid instant moving when you call everything in the Update).
But you can also use an animation made by someone else in applications like Blender.
Or finally create an animation with the Unity in game editor and create an Animator to animate your door.
I think that you should use this way.
First follow this official manual:
https://docs.unity3d.com/Manual/animeditor-CreatingANewAnimationClip.html
to create the clip to open your door.
It's really intuitive and you don't even need to code.
After that you should create an Animator Controller:
https://docs.unity3d.com/Manual/class-AnimatorController.html
You will than create a new state with your animation and you will create a connection between the initial state to your "Open door" animation.
After that you just create a simple bool (in your Animator Controller).
And you will add
this.GetComponent().SetBool("nameofyourboolintheanimatorcontroller", true);
to your script (of course this is valid only if your Animator Controller is in the object assigned to your script... if not you should create a new Animator variable and assign it (for example you could make it public and assign it in the editor).

Related

Bullets not visible in the game view when I move left

So I am very new to unity. and I used this tutorial to create a shooting mechanic for my prototype. moving right and shooting projectiles works fine but when I move left the projectiles are not visible in the game view . I noticed they are in the scene when I run the game. I believe this has nothing to do with my code. since when I press the button it shoots and destroys. it's just not visible when I am moving left. is this a visual issue??
I have screenshots here to provide a better example. of what I'm experiencing. and the links to the movement and projectile code as well for any references. if anyone knows an easy fix it would be much appreciated. If you need to see my code i will include a follow up.
Movement:https://www.youtube.com/watch?v=n4N9VEA2GFo&t=20s[enter image description here]1
Projectile:https://www.youtube.com/watch?v=8TqY6p-PRcs&t=264s
#Nyssa Wootton Welcome to StackOverflow.Try the bullet spawning approach you no need to attach a bullet with the player. Just assign a bullet in inspector and it will spawn a bullet whenever the user clicks on the button. have a look at this code
public GameObject bulletPrefab;
public Transform spawnPos;
public float bulletSpeed = 100f;
void Update () {
if (Input.GetMouseButtonDown(0)){ // on fire btn press
var bullet = Instantiate(bulletPrefab , spawnPos , Quaternion.identity);
// make sure you're prefab have rigidbody on it
bullet.GetComponent<Rigidbody>().AddForce(transform.forward * bulletSpeed , ForceMode.Impulse);
Destory(bullet , 2); // destorys after 2 second
}
}

Modify current Object and not prefab

I'm really new to unity, This is my first project.
My problem is than when I create a new gameobject
public GameObject fancyName;
and then drag the prefab I want over the script window, if I make any changes on fancyName, instead of modifying what I see on the screen I get to modify my prefab...
What do I have to do in order to modify the Instantiate GameObject and not the prefab?
It is an expected behavior because fancyName holds a reference to your prefab so any changes you make to 'fancyName' is a direct modification to your prefab (remember you dragged & dropped your prefab to this variable right?).
You should instantiate/clone it:
var clone = (GameObject) Instantiate(fancyName);
and then
MakeChange(clone); // Do anything you want

How to animate this imported character in Unity?

I imported a zombie character asset from the unity store and it came with one animator controller and two animation scripts. I already have my movement script attached to my zombie player and it moves when I press the arrow keys. I want it to do the run animation while it goes forward.
I added the animator component to my player and fed it with the animator controller which came with the asset. But when I click play I only see the idle standing animation and not the running one. do I need to use additional scripts?
You need to call the methods in the Update method, something like this:
void Update() {
if(Input.GetButtonDown("W")||Input.GetButtonDown("S")){
Run();
}else{
OtherIdle();
}
}
Using input
You need to:
1. Detect input every frame (so in your Update() method).
2. Call the methodRun() to set the animation bool to true.
This will make the Run animation play when the "w" key is pressed if the Animator Controller is set up properly:
void Update () {
// If the forward key is pressed...
if (Input.GetKeyDown("w")){
// Play the Run animation
Run();
}
}
Using speed
Alternatively you could give a new parameter to your animation controller "speed" and set the bool for the animation to play when speed is above a certain value. Else if the speed is below the value, play the Idle animation. This way, if you have a Walk animation, you can play it when the speed is between running speed and stationary.

Not able to translate gameObject when Animation is already added to it using Animator in Unity2D Sprite

I am able to translate the gameObject from one point to other by the OnMouseDrag() Event have no problem in doing that. But when i have added Animation to the GameObject Using the Animator Im not able to handle its Dynamic translate property when the Drag event take place.
Details:-
The Animation that i have create for the Game object is that it gets Translate from the Outer side of the screen to a side of the screen, This happens on the launch of the scene. (This happens properly)
Now when i want to dynamically translate the game object on the mouse drag it doesn't translate. (if i have not added the animator to the object it translate with mouse drag with out an issue)
Can any Tell me why it is so ?
Thanks in advance.
Did you try making Gameobject with animation,a child of an empty Gameobject and then translating that parent gameobject.

Unity apply idle animation to model

I created a model in Blender and posed it the way I want it to look. The character will never move and no animations will ever change.
I exported the character with animations to an FBX and imported it into Unity 3D. After dropping the character into the scene, the character's pose is back to the default take.
How can I force the character to use the pose that I want? Do I have to do it in script or is there a setting that I can change to make it play the idle animation at all times?
Putting your model in the scene will not invoke idle animation, animation and rendering is two separate thing. To always play a certain animation on a model you have three option -
1. Via Legacy Animation -
Select you model in the scene, remove the default Animator component and add a new Animation component. (Animator and Animation are two different component type.) It has an option of selecting an animation clip. Drop your animation clip there and choose 'Play Automatically'.
2. Via Script -
You can add Animation component, attach multiple animation and select which one to play via script too.
You can also do this for meca-anim way which is explained in the next point.
3. Via Meca-Anime
Every model has a default Animator component. It needs an animation controller object to run. Create an animation controller (Click on create button in project view). Attach the controller to the Animator component. Double click on the controller to open Animator view. Drop your clip there and make it as default. The default animation plays automatically now. Using Meca-anime has many advantage over legacy way, which is beyond the scope of this answer. Check the following link instead -
Mecanim vs Legacy Animation

Resources