My animations aren't playing when they should be - c#

So Basically, I am creating a wandering ai character in unity using c# and the wandering is all working fine, but when the certain animations are supposed to play, they don't. I will include the code I am using to make this happen. I am also using an animator component on the model and the animations are all properly named and align with the animation in the animator's names and the animations are from mixamo. Any help is much appreciated because I am completely stuck!
void Update()
{
if (isWandering == false)
{
StartCoroutine(Wander());
}
if (isRotatingRight == true)
{
gameObject.GetComponent<Animator>().Play("idle");
transform.Rotate(transform.up * Time.deltaTime * rotSpeed);
}
if (isRotatingLeft == true)
{
gameObject.GetComponent<Animator>().Play("idle");
transform.Rotate(transform.up * Time.deltaTime * -rotSpeed);
}
if (isWalking == true)
{
gameObject.GetComponent<Animator>().Play("waalk");
transform.position += transform.forward * moveSpeed * Time.deltaTime;
}
}

Really consider using Animator parameters and transitions, it will save a lot of headache later on.
Anyways, regarding the question: Your code is in Update, which means it runs every frame.
That means every frame you're telling the Animator to play the state with that name, so every frame it starts the animation of that state over again. The result would be that your object would be stuck on the first frame of whatever animation.
Instead, you should call Play just once, when the desired conditions change.
Incidentally, that's one example of when it would be more convenient to use Animator parameters, since with transitions you are querying for specific conditions that take the animator from one state to the other, so you would get this "run just once" behaviour for free.

SORRY I FOUND THE ANSWER I WAS DOING EVERYTHING TO A MODEL THAT WASN'T RIGGED 😅 IT'S ALL GOOD NOW THANKS FOR THE HELP :)

Related

Unity: Set RigidBody velocity in FixedUpdate() or Start()?

I'm developing a simple 2D game in Unity (balls hitting bricks), so i have a ball and i want to fire the ball to hit bricks, now to fire the ball i have to set Velocity on the RigidBody component attached to the ball's GameObject.
There are two ways to do that:
Method 1:
Set the velocity inside the Start() method
private void Start()
{
rb.velocity = direction * speed;
}
Method 2:
Write a FixedUpdate() method and put the statement in there. because:
Use FixedUpdate when using Rigidbody.(Unity Docs)
private void Start()
{
_setVelocity = true;
}
private void FixedUpdate()
{
if (!_setVelocity) return;
rb.velocity = direction * speed;
rb.angularVelocity = 0;
rb.gravityScale = 0;
_setVelocity = false;
}
But the thing is: i don't need the FixedUpdate() method for anything else, because i only fire the ball once in its script's lifespan, so after i fire the ball unity will continue to run the FixedUpdate()method and check for _setVelocity's value and return (exit) every time because it will always be false after i shoot the ball the first time.
And this will happen every fixed frame-rate frame which is expensive i guess for just setting the velocity only once.
SO:
Is it expensive to have a FixedUpdate() method that checks for false value and returns every frame?
Which of the two methods performs better in this case?
Is it expensive to have a FixedUpdate() method that checks for false value and returns every frame ?
A simple boolean condition such as yours is not that greedy so do not worry about that. However, any line of code that runs decrease your performances. The Update() method itself is more greedy in terms of performance than your "trivial" condition. Disabling your component script or removing the object itself is more optimize than keep it running for nothing. Just keep that in mind :)
Which of the two methods performs better in this case ?
The first one is more efficient because your velocity is fixed, so there is no point of using an update method (Update() or FixedUpdate()). You should initialize your rigidbody's velocity directly in your Start() method as you did in your post, so you can get rid of your boolean and its condition.
Unity's documentation is pretty clear about rigidbody and FixedUpdate() but I have the impression that you misunderstood some aspects.
They tell you that if you want to modify any data of your rigidbody during the run-time, prefer to use FixedUpdate() instead of Update() to avoid weird behavior.
But why should you use FixedUpdate() instead of Update() ?
Because Update() is called at each frame, so if your pc runs at 100 fps, Update() will be called 100 times. And if your pc runs at 200 fps, it'll be called 200 times. It is kind of problematic in some cases, especially when you interact with physics components.
In order to understand, lets admit that we want to apply a force to our rigidbody (rb.AddForce(100)). If you call it in your Update() method and your pc runs at 100 fps, you'll apply 10000 force in one second. However, if your pc runs at 50 fps, then you'll apply 5000 force in one second. It'll cause you some weird behavior that could be avoided thanks to FixedUpdate(), which is called at a fix number of times per second (see unity doc).
However, there is no point of using FixedUpdate() with your rigidbody component if you do not want to modify it. They tell you that if you want to update your rigidbody component, you should do it inside the FixedUpdate method instead of Update().
So for your question, you can simply do the following:
Awake() {
// getting your components
rb = GetComponent<Rigidbody2D>();
}
Start() {
// initiliaze your components
rb.velocity = direction * speed;
rb.angularVelocity = 0;
rb.gravityScale = 0;
}
Hoping that I've helped you, good luck with your project !

Lifecycle, Coroutine and deltaTime

Unity: 2017.3.0f3
Use case : Move an object on click.
I often see Coroutine like the following to move object.
private IEnumerator MoveTo(Vector2 position){
while (/*stop condition*/)
{
float newX = speed * Time.deltaTime;
transform.position = /*with newX*/
yield return null; // <-----------
}
}
I think it should be:
private IEnumerator MoveTo(Vector2 position){
while (/*stop condition*/)
{
yield return null; // <-----------
float newX = speed * Time.deltaTime;
transform.position = /*with newX*/
}
}
I think that in most use case we have to wait the next frame to execute the move action otherwise we take into account the deltaTime that already passed before we managed to move the object.
What do you think ? Am I totally wrong ?
PS: I know that we can use Update() and co. for this kind of use case.
What do you think ? Am I totally wrong ?
Yes, you are wrong. The first one is more correct.
Think of it more like this:
You want to move object from one position to another, the first thing to do is to calculate the distance to move the object then move to that position and then wait for a frame. You do this over and over again until you reach the destination position. This is what the first code is doing.
The second code on the other hand is waiting for a frame first then moving the object. Why wait for a frame before starting the movement? What will happen is that the movement will not start when that function is called until next frame which will introduce a delay. Unless this is explicitly what you want, you shouldn't go with it.
Not wrong at all, but I guess it doesn't make a noticeable difference most of the time? Your second version would be more accurate in some sense because I think that is really the expected behavior, that the object is rendered once at position 0 before moving on. If the moving object is a user controlled object I still think the first method is better suited because it would give the fastest feedback/response to the user without any "delay". The second method would be better for animations where it is preferred that the starting position is rendered as well (especially if the object isn't even visible before the animation starts),

Find and add the velocity of a Rigidbody that's directly below another? (Unity + C#)

I have an issue that I've not been able to solve for a few days now and without it working I can't move on with my project, so your help would be greatly appreciated!
What I'm trying to do is 'find' the velocity of an object that is directly below my 2D sprite (that also contains a Rigidbody and 2D box collider) and then add that velocity (in the same direction) to the object that is 'looking' for it.
I feel like ray-casting might be part of the answer but I'm not sure how to use that, especially in this context.
The idea behind this is I have a platform that can carry objects stacked on top of each other, so you move the mouse, it manipulates the platforms velocity, but this causes the objects on-top to fly backwards, no mater how high the friction is.
Here is the platform code:
void Update()
{
float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
mouseDelta = pos_move - lastMousePosition;
acceleration = mouseDelta.x * 0.4f;
platformVelocity += acceleration;
platform.velocity = new Vector2(platformVelocity, 0) * speed;
lastMousePosition = pos_move;
}
Thank you for taking the time to read this, I would very much appreciate any help you can give!
Is it necessary to 'find' that object? 'Finding' may be difficult & costing.
Can you hold the 'below-thing' reference and pass it to 'above-thing'?
I'm assuming you have some method on objects that checks whether it's on a moving platform. One option is to add an IsOnMovingPlatform flag on those objects. Then use a similar method to check if any object is on top of the "IsOnMovingPlatform == true" objects. Keep doing passes on that method until you don't come up with any more flags set to true.
So, i'm trying to understand your question and I think I might know what you're talking about.
Are you saying that you want your game object (2d sprite) to stay on the platform as its moving? I have had the same problem, where a platform is moving and my player (game object) slides off- and this is how I fixed it.
Add a box collider to the platform (trigger). When the player enters the platform, make the player's transform the platform. That way, when the platform moves- the player moves with it no matter what the speed is.
For example:
void OnTriggerEnter2D(Collider2d collider){
if(collider.tag.equals("Player")){ // you only want the player to work
// attach script to platform for "this.transform" to work
collider.transform.SetParent(this.transform);
}
}
then, to unparent the transform "otherwise, even when the player is off the platform, it will move with the platform.
void OnTriggerExit(Collider2d collider){
// assuming your player isn't supposed to have a parent!
collider.transform.parent = null;
}
Sorry if I misunderstood your question! If this is what you were going for, finding the velocity of the platform is not necessary. Also, this is uncompiled code- if you have issues let me know.
EDIT:
To add a bit of a give, for the entire duration that the player is in contact with the platform, we can add a little bit of a negative force to shove the player a little bit!
We do this by using OnTriggerStay2D (assuming your game is 2d, otherwise use OnTriggerStay).
For example:
void OnTriggerStay2D(Collider2d collider){
if(collider.tag.equals("Player")){
collider.getComponent<Rigidbody2D>().addForce(new vector2(forceToAdd, 0));
}
}
now, that "forceToAdd" variable should be based on the speed and direction that your platform is going. Does your platform have a rigid body on it? if so you can get your force variable by retrieving the rigid body from the platform (getcomponent()) and get the X velocity from it. With this velocity, experiment with dividing it by numbers until you find a good fit, multiply it by -1 (because you want to push the player in the opposite direction of the platform travel), then this will be your forceToAdd!
However, if your platform is not using a Rigidbody, then a way you can do this is record the x positions in your script (just two positions, and subtract these to find the distance in an update method), multiply by -1, try dividing by a number (experiment with this) and that can be your force to add variable.
Also, my code for the OnTriggerStay2d is rough, the spelling could be a bit wrong- but this will give you a rough idea for a solution! ALSO.. I don't think getting the component of a rigid body should be done in every frame, a way around this would to have your rigid body component as a global variable in the script, then set your rigid body on OnTriggerEnter2d, this way it will only retrieve it once (instead of every frame) which I assume would be more efficient!
Good luck! and if you have any questions il try to help!
This is a raycast2d version, I tested it and it works much better than the trigger collider version :).
using UnityEngine;
using System.Collections;
public class RaycastFeetDeleteLater : MonoBehaviour {
[SerializeField] private float raycastDist = 0.1f;
[SerializeField] Transform feet;
[SerializeField] private float xForceDampener = 0.1f;
private Rigidbody2D rb2d;
// Use this for initialization
void Start () {
rb2d = GetComponent<Rigidbody2D> ();
}
// Update is called once per frame
void FixedUpdate () {
RaycastDown ();
}
private void RaycastDown(){
RaycastHit2D hit = Physics2D.Raycast (feet.position, Vector2.down, raycastDist);
if (hit.collider != null) {
print (hit.transform.tag);
if (hit.collider.tag.Equals ("Platform")) {
transform.SetParent (hit.transform);
Vector2 force = new Vector2 ((hit.transform.gameObject.GetComponent<Rigidbody2D> ().velocity.x * xForceDampener) * -1, 0);
print (force);
rb2d.AddForce (force);
} else if (transform.parent != null) {
print ("UNPARENT");
transform.parent = null;
}
} else {
if (transform.parent != null) {
transform.parent = null;
}
}
Debug.DrawRay (feet.position, Vector3.down, Color.green, raycastDist);
}
}
This script is attached to the player! No need to have scripts on the platforms, which was the main reason my previous method was a pain :P
Basically this script does the following:
- Gets rigidbody2d of the player on start (we will be adding forces to the player with it soon, so might as well store it in a variable!
On FixedUpdate (Different than Update, FixedUpdate is called on Physics update, where as Update is called every frame) I have read that raycast should use FixedUpdate, I suppose because it is a Physics method!
RaycastDown shoots a 2D raycast directly down from the feet position (to get the feet, create a child object of the player, put the position of the feet a tiny bit below the player, NOT ON THE PLAYER- I will explain why in a second why that will mess up everything! Then drag the feet GameObject on to the "feet" slot of the script.)
Raycast shoots from feet directly down at a distance that is changeable in the editor (raycastDist) I found a distance of 0.1f was perfect for my needs, but play around with it for yours! You can actually see a drawing of your Ray by using Debug.DrawRay as seen in the code, the ray is only visible in the editor- not game view! But this will help, especially with positioning the feet.
Checks if it hit an object with a collider, if so check if the objects tag is a Platform (set the platforms to a new tag and call it Platform).
If it hits a platform set the parent of the player to the platform (like the last script) and start applying a little force to the player in the negative direction that the platform is traveling (we get the rigid body of the platform to find its velocity).
If the raycast does not hit a platform, unparent it so the player can move freely.
Thats basically it, however...
Please note:
This script needs to be on the player, not any of the players child objects, since this script will unparent the player from any platforms- if you have the script on a child object of the player it will unparent from the player- which is horrible. So The script MUST be on the root game object of the player. There are ways around this if for some reason you can't.
As mentioned before, the "feet" child object must be a little bit under the player- this is because when the Raycast2d "fires" it will find the FIRST object with a collider it hits- so if you have the start of the raycast hit a collider on the player then it will always show the raycast hitting the player, not a platform. So to get around this have the raycast fire from the feet- and have the feet position a tiny bit below the player. To help with this I added a print statement that will print to the console which object the raycast is hitting, if it doesn't say "Platform" then you need to lower the feet! :)
Furthermore, I'm not sure how you are moving your platforms- but if the Velocity of the platform always prints "0,0" then its because you are moving the platforms position directly, instead of moving it by forces (this happened to me when I was testing this script). I got around this by moving the platform by "AddForce". Also depending on the "Mass" of your platforms, you may need to drastically increase the "xForceDampener" which I should of actually labeled "xForceMultiplier" as I found while testing I needed to set it much higher- like at 60, instead of 0.25 which I previously thought would be okay..
Hope this helped :D
EDIT: Linking the unity docs for 2D Raycasts in case you need
http://docs.unity3d.com/ScriptReference/Physics2D.Raycast.html

AddForce Method Not Working

I am programming a top-down hack-and-slash game for a class. We want a mechanic where if you get hit by an enemy, or an enemy hits you, you get 'knocked back'. Unfortunately, no matter what I have tried, I cannot get either the enemy or the player to react to the force.
Here is a list of things I have checked that have been suggested in other questions like this:
Player/Enemy is NOT Kinematic
Tried with both Gravity on and off
No positions/rotations are frozen
Player/Enemy have Rigidbodies attached
Player/Enemy have colliders attached, one with and one without the 'isTrigger' function checked.
Tried both OnCollisionEnter and OnTriggerEnter
The force value is high, the mass, drag, and angular drag are low
I have run out of ideas. Any and all support you can give is greatly appreciated.
Here is a snippet of the code from a script from the player Object:
public void OnTriggerEnter(Collider col)
{
if (col.gameObject.tag == "EnemyHit" && !invincible)
{
Debug.Log("The player has been hit!");
//sets player as invincible
invincible = true;
// Set the damaged flag so the screen will flash.
hit = true;
timeOfHit = Time.time;
// Reduce the current health by the damage amount.
currentHealth -= 1;
GetComponent<Rigidbody>().AddForce(transform.forward * recoilThrust, ForceMode.Force);
Debug.Log("This is the force that is being added to the player when it is hit. : " + -transform.forward * recoilThrust);
//...
}
}
I can prove (using the Debug.Log) function, that the code reaches there, and the force is calculated.
To summarise the comments:
Rigidbody.AddForce doesn't work when the object has a CharacterController besides the rigidbody. In this case the effect has to be "faked". Possible ways can be found here:
The last couple of posts
Another solution, C# and Javascript
Basically you need to apply the force using CharacterController.Move.
Is Kinematic enabled? Doing so will make the object ignore physic forces.

Set gameObject active Unity C#

A game object has force applied to it in the positive direction, then after some time it has a force applied to it in the negative direction.
If the force is applied in the negative direction, this means game over and I want to display a totally different gameObject which is the game over gameObject called icetextureONfile. My method is not working I get error "type 'UnityEngine.GameObject' does not contain a definition for icetextureONfile". I am having a hard time refe
public void FixedUpdate() {
// No action happened yet
gameObject.icetextureONfile.SetActive (false);
// Increase the kick timer
kickTimer += Time.fixedDeltaTime;
// If the next kick time has came
if (nextKick < kickTimer) {
// Schedule the kick back corresponding to the current kick
nextKickBacks.Enqueue (nextKick + 100f);
// Apply the kick force
rb.AddForce (transform.up * thrust, ForceMode.Impulse);
// Plan the next kick
nextKick = kickTimer + Random.Range (MinKickTime, MaxKickTime);
}
// If there are more kick backs to go, and the time of the closest one has came
if (0 < nextKickBacks.Count) {
if (nextKickBacks.Peek () < kickTimer) {
// Apply the kick back force
rb.AddForce (-transform.up * thrust, ForceMode.Impulse);
// Game object was kicked down, set active game over object
gameObject.icetextureONfile.SetActive (true);
// Dequeue the used kick back time
nextKickBacks.Dequeue ();
}
}
}
If your wanting to deactivate one and then activate the other you could just add this to the class make sure its not inside the function
public GameObject iceTexture;
then drag and drop that object into the spot shown in the script in unity called iceTexture. Then just make sure you deactivate the object that the script is attached to and activate the iceTexture object.
gameObject.SetActive(false);
iceTexture.SetActive(true);
This page might help you.
This syntax worked for me
GameObject.Find ("icetextureONfile").SetActive(false);
As opposed to what is found in the Unity docs
gameObject.SetActive(false);
The first worked for me, the second did not. I am not a great programmer, so maybe it is just a matter of context.
Edit
It is commonly known that it is difficult to SetActive(true) after the object has already been SetActive(false). This is because the attached script becomes deactivated too. See Unity forums for details on why this is a nightmare.
To overcome this I have chosen a different route that accomplishes the same thing.
I set the size of the object to 0 until I needed it.
GameObject.Find ("icetextureONfile").transform.localScale = new Vector3(0, 0, 0);
GameObject.Find ("icetextureONfile").transform.localScale = new Vector3(0.02f, 0.02f, 0.02f);
Note that 0.02 is the size of my object in particular and may not be the exact same size as yours.

Categories