How do i code double jumps to character controller on unity?

heres my script. i just started coding like 3 days ago so idk much.

 public class playermovement : MonoBehaviour {    Vector3 velocity;     public float gravity = -9.81f;     public CharacterController controller;     public Transform groundCheck;     public float groundDistance = 0f;     public LayerMask groundMask;     bool isGrounded;     public float jumpHeight = 3f;             // Update is called once per frame     void Update()     {         velocity.y += gravity * Time.deltaTime;         controller.Move(velocity * Time.deltaTime);          isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);         if (isGrounded && velocity.y < 0)         {              velocity.y = -2f;         }         if (Input.GetButtonDown("Jump") && isGrounded)         {             velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);          }                } } 

Box collider not colliding with Character controller

I have a player with a CharacterController attached. The player can move around.

There are obstacles which the player can collide with. These obstacles have a BoxCollider and a Rigidbody which is set to "kinematic" as I don’t want these obstacles to be affected by physics and they don’t move.

Every time the player collides with an obstacle he receives some damage. To do that I used this piece of code on the obstacle.

private void OnCollisionEnter(Collision collision) {      print("hit");      if (collision.gameObject.tag == "Player") {             collision.gameObject.GetComponent<Player>().TakeDamage(damage);             print("hit player");         }     } 

But the obstacle doesn’t register collision events from the Player.

The player on the other hand is able to receive collision from the obstacle in the OnControllerColliderHit() and I can set the damage received in there but I’m not sure if that’s a good way.

I used bool parameter in my animator controller setting it to true by script but where should I set it back to false and when?

using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI;  public class FireEffect : MonoBehaviour {     public Transform player;     public Transform target;      private float timeElapsed = 0;     private float lerpDuration = 3;     private float startValue = 1;     private float endValue = 0;     private float valueToLerp = 0;      private Animator playerAnimator;     private bool prevFacing = false;     private bool stopped = false;     private bool move = true;     private bool rot = false;     private Vector3 currPos;      private void Start()     {         playerAnimator = player.GetComponent<Animator>();     }      private void Update()     {         var currFacing = IsFacing(target);         if (currFacing != prevFacing)         {             timeElapsed = 0;         }         prevFacing = currFacing;          var distance = Vector3.Distance(player.position, target.position);          if (IsFacing(target))         {             if (distance < 10 && move)             {                 if (timeElapsed < lerpDuration)                 {                     valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);                     playerAnimator.SetFloat("Forward", valueToLerp);                     timeElapsed += Time.deltaTime;                 }                 playerAnimator.SetFloat("Forward", valueToLerp);                 stopped = true;                 valueToLerp = 0;             }              if (move == false)             {                 playerAnimator.SetFloat("Forward", 0);             }              if (playerAnimator.GetFloat("Forward") == 0 && stopped)             {                 move = false;                 rot = true;                 currPos = player.position;                 stopped = false;                 Debug.Log("Player current position when valueToLerp value is 0 : " + currPos);             }         }                  if(rot)         {             playerAnimator.SetBool("Walk Back", true);              rot = false;         }          if (playerAnimator.GetCurrentAnimatorStateInfo(0).IsName("Walk Back"))         {             playerAnimator.SetBool("Walk Back", false);         }     }      private bool IsFacing(Transform target)     {         Vector3 forward = player.TransformDirection(Vector3.forward);         Vector3 toTarget = target.position - player.position;         return Vector3.Dot(forward, toTarget) > 0;     }      public IEnumerator ScaleOverSeconds(Vector3 scaleTo, Vector3 rotateTo, Vector3 moveTo, float seconds)     {         float elapsedTime = 0;         Vector3 startingScale = player.localScale;         Vector3 startingRotation = player.localEulerAngles;         Vector3 startingPosition = player.localPosition;          while (elapsedTime < seconds)         {             player.localScale = Vector3.Lerp(startingScale, scaleTo, (elapsedTime / seconds));             player.localEulerAngles = Vector3.Lerp(startingRotation, rotateTo, (elapsedTime / seconds));             player.localPosition = Vector3.Lerp(startingPosition, moveTo, (elapsedTime / seconds));              elapsedTime += Time.deltaTime;              yield return null;         }         player.localScale = scaleTo;         player.localEulerAngles = rotateTo;         player.localPosition = moveTo;     } } 

At this part, I’m setting the bool to true once and it’s playing the "Walk Back".

if(rot)             {                 playerAnimator.SetBool("Walk Back", true);                      rot = false;             }                  if (playerAnimator.GetCurrentAnimatorStateInfo(0).IsName("Walk Back"))             {                 playerAnimator.SetBool("Walk Back", false);             } 

After that, I’m setting the trigger to false when it’s finishing playing it because if I will not set it to false it will keep playing the "Walk Back" nonstop.

I want that it will play it once but also when rot is true again then play it again once. but because I’m setting it false it’s not playing it again even if rot is true again.

animator controller

I created a transition from the Grounded blend tree to the Walk Back with the bool condition in the screenshot.

The Walk Back is working fine smooth as I wanted the problem is now how to make it play once only each time rot is true?

The way or the place in the script I’m setting the bool back to false make it that it will not play the Move Back ever again even if rot is true.

Unity Animation Controller and walking diagonally keeps switching between the two diagonals?

Using Unity 2019.4.1f1 Personal.

Unity Animation Controller nodes

I have the above Animation Controller. When only walking in one direction at a time and when you stop you stay facing that direction, this works as planned.

Successfully walking pokemon trainer

All of the transitions do not have an exit time and have a fixed duration of 0. I don’t know what info to give as this is the first time I’ve used animation. Let me know and I’ll add relevant details.

Here’s my update function that updates the variables in the animator:

void Update() {   float h = Input.GetAxisRaw("Horizontal");   float v = Input.GetAxisRaw("Vertical");    animator.SetFloat("Horizontal", h);   animator.SetFloat("Vertical", v);    if (Mathf.Abs(v) < Mathf.Abs(h)) {     // flips the renderer to reuse other handed image     spriteRenderer.flipX = h > 0;   }    // no actual movement until I figure this out } 

What I’m experiencing that I want to fix is when running diagonally:

Broken diagonal movement

It seems that when walking diagonally then the animator variables for Horizontal and Vertical are both either 1 or -1 and this causes the alternating of the sprites. What I’d like to happen is that the state machine would deterministically pick either N/S sprites or E/W sprites when moving diagonally. Turning on "Can Transition To Self" seems to properly pick a direction and stick to it but it fails to animate when only moving in a single direction. It looks like it’s constantly transitioning to itself which starts over the cycle so I’ve left that unchecked on all transitions.

I’m not sure what the best way to fix this is. Most attempts I’ve tried so far complicate the state machine to the point it’s unmaintainable. I also couldn’t find anything about introducing priorities in a way that fixed the jitter.

What’s the idiomatic Unity way to achieve clean diagonal movement in 2D?

How does the address bus , Memory Controller , RAM and CPU interact?

So I was taking a course and didnt quite get what was happening so I tried to research the material and found 2 different representations for the topic.

2 Photos

CPU getting data/instructions from RAM

So the first one shows that the Memory controller is part of the CPU and the process of sending data is:

CPU -> Memory controller -> Address bus -> RAM -> Data Bus -> CPU

The second shows :

CPU -> Address Bus -> Memory Controller -> Ram -> Memory Controller -> Data bus -> CPU

I did search for the topic but couldn’t find an answer as most people don’t show memory controller and address bus interaction

So does anybody know which is the right one ?

Thanks in advance

How to do collision detection in Unity between Character Controller, Rigidbody Collider and a NavmeshAgent?

I would like to know more about how collision detection works in Unity especially when character controllers and NavmeshAgents get involved.

For example, how can I detect collision between NavmeshAgent and a CharacterController? I have added a sphere collider to my NavmeshAgent, but it doesn’t seem to detect a collision unless I set it to “IsTrigger”. I have tried OnCollisionEnter(), OnControllerColliderHit() and OnTriggerEnter(). OnTriggerEnter() seems to be the only one that works and only when I enable “IsTrigger”.

How come the other two don’t work? Shouldn’t they? What if I didn’t want to make the collider a trigger?

Finite state machine for a modified traffic controller

Consider a traffic light controller for an intersection of a main street and a smaller street. Both streets are one-way streets. The intersection has two traffic lights, one for each street. In normal operations the main street light is always green unless cars arrive on the small street. In this case, the small street is given green until all cars have passed or 2 minutes have elapsed. The transition of green from one street to another street includes a 10-second period when the first street becomes orange and the second street remains red. For the emergency situation that an ambulance is approaching the intersection, the controller immediately turns both lights into red to indicate the emergency. After the emergency, the smaller street is turned green if it has cars. Otherwise, the main street is turned green

How to build a Create a Finite State Machine describing the central controller of this intersection. Also how to Draw a UML state chart diagram

Can we use the State Design Pattern to implement the finite state machine? Pointers towards some pseudo code would help.