Search Unity

Problems with Create with Code Lesson 3.3 Don't Just Stand There

Discussion in 'Community Learning & Teaching' started by astolbon, Oct 28, 2020.

  1. astolbon

    astolbon

    Joined:
    Oct 12, 2020
    Posts:
    1
    Hi!

    I´m going through the "Create with Code Lesson 3.3 Don't Just Stand There" and i have a problem.

    When applying the jump animation, the player is not jumping on the spot, but is actually jumping forward, leading to the player leaving the frame after a couple of jumps.

    I have not been tinkering with the animation, other than what is part of the lesson.

    Thanks!

    /John

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerController : MonoBehaviour
    6. {
    7.  
    8.     private Rigidbody playerRb;
    9.     private Animator playerAnim;
    10.     private float jumpForce = 800;
    11.     public float gravityModifier;
    12.     public bool isOnGround = true;
    13.     public bool gameOver = false;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         playerRb = GetComponent<Rigidbody>();
    19.  
    20.         playerAnim = GetComponent<Animator>();
    21.  
    22.         //Physics.gravity = gravityModifier * Physics.gravity;
    23.         Physics.gravity *= gravityModifier;
    24.  
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update()
    29.     {
    30.         if (Input.GetKey(KeyCode.Space) && isOnGround && !gameOver)
    31.         {
    32.             playerRb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
    33.             playerAnim.SetTrigger("Jump_trig");
    34.             isOnGround = false;
    35.         }
    36.     }
    37.  
    38.     private void OnCollisionEnter(Collision collision)
    39.     {
    40.        
    41.         //Ser om objektet man kolliderar med har taggen "Ground" eller "Obstacle"
    42.         if (collision.gameObject.CompareTag("Ground"))
    43.         {
    44.             isOnGround = true;
    45.         } else if (collision.gameObject.CompareTag("Obstacle"))
    46.         {
    47.             playerAnim.SetBool("Death_b", true);
    48.             playerAnim.SetInteger("DeathType_int", 1);
    49.             gameOver = true;
    50.             Debug.Log("Game over");
    51.         }
    52.     }
    53. }
    54.  
     
    R0nananan likes this.
  2. cremefreche

    cremefreche

    Joined:
    Jan 2, 2021
    Posts:
    1
  3. Vebzaaah2

    Vebzaaah2

    Joined:
    Jan 19, 2024
    Posts:
    1
    Had the same problem, disabling Apply Root Motion helped!