Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by whatisitstudios_unity · Jan 24, 2021 at 04:32 PM · 2d-platformerattack

How to add diving to my script?

I am making a 2D battle platformer, and I would like to add diving, but it seems really hard, as I am pretty new to coding. I watched some videos, but none of them were what I was looking for. I decided to leave it up to the professionals. Here's my code what I want is the player to launch in the direction they're facing, and to launch up in the air at the same height as jumping.

Here's my code: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Movement : MonoBehaviour
 {
     //Variables
     [Header("Movement")]
     //Input Variables
     public float playerSpeed = 5f;
     public float jumpheight = 5f;
 
     private int extraJumps;
     public int extraJumpsValue = 1;
 
     [Header("Physics")]
     //Physics stuff
     public LayerMask ground;
     public Rigidbody2D rb;
     public Transform player;
 
     bool isGrounded;
     public Transform groundCheck;
 
     [Header("Animation")]
     //Animation
     private bool facingRight;
     public Animator anim;
 
     public ParticleSystem jumpParticles;
 
     [Header("Sounds")]
     public AudioSource audioSource;
     public float Volume;
 
 
     //Start Function
     void Start()
     {
         extraJumps = extraJumpsValue;
         audioSource.volume = Volume;
     }
 
     //Moving
     void FixedUpdate()
     {
         isGrounded = Physics2D.OverlapCircle(groundCheck.position, 0.5f, ground);
 
         float x = Input.GetAxis("Horizontal");
 
         rb.velocity = new Vector2(x * playerSpeed, rb.velocity.y);
 
         if (facingRight == true && x > 0)
         {
             Flip();
         }
         else if (facingRight == false && x < 0)
         {
             Flip();
         }
     }
 
     //Jumping
     void Update()
     {
         anim.SetFloat("Horizontal", rb.velocity.x);
         anim.SetFloat("Speed", rb.velocity.sqrMagnitude);
 
 
         if (isGrounded == true)
         {
             extraJumps = extraJumpsValue;
         }
 
         if (Input.GetButtonDown("Jump") && extraJumps > 0)
         {
             rb.velocity = Vector2.up * jumpheight;
             extraJumps--;
 
             //Particles
             jumpParticles.Play();
 
             //Sounds
             FindObjectOfType<AudioManager>().Play("Jump");
         }
         else if (Input.GetButtonDown("Jump") && isGrounded == true)
         {
             rb.velocity = Vector2.up * jumpheight;
         }
 
         DidIDie();
         Attack();
     }
 
     //Flips the player
     void Flip()
     {
         facingRight = !facingRight;
         Vector3 Scaler = transform.localScale;
         Scaler.x *= -1;
         transform.localScale = Scaler;
     }
 
     //Checks if player has died
     void DidIDie()
     {
         if(player.position.y <= -10f)
         {
             transform.position = new Vector3(0f, 0f, 0f);
         }
     }
 }
 

Thanks in advance Also It should be physics based, as the game is entirely built on physics.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Llama_w_2Ls · Jan 24, 2021 at 05:03 PM

Well that's quite simple. You can add a force in the transform.forward direction of the player, the same size as your jump force. E.g:

 void Dive()
 {
       rb.AddForce(transform.forward * jumpHeight, ForceMode.Impulse);
 }

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whatisitstudios_unity · Jan 24, 2021 at 07:42 PM 0
Share

Hi, @Llama_w_2Ls, thanks for you answer, but for some reason this isn't working. I'm making it a 2D game so of course the first thing I did was change Force$$anonymous$$ode.Impulse to Force$$anonymous$$ode2D.Impulse, but it still doesn't work.

avatar image Llama_w_2Ls whatisitstudios_unity · Jan 24, 2021 at 08:34 PM 0
Share

Oh I didn't know it was 2D. If you use the velocity of the rigidbody, you can get the direction that the player is moving in. Then add force in that direction. $$anonymous$$g., if the player was moving negatively fast on the x axis, they are moving backwards. Does the direction of movement match where the player is looking, (right, left, up, down), or does the look direction correspond to the mouse position?

avatar image whatisitstudios_unity Llama_w_2Ls · Jan 24, 2021 at 08:52 PM 0
Share

@Llama_w_2Ls : the look direction is where the player's direction is

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

122 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

[UNITY 2D] Can I create a spawner that spawns another enemy after its death using IF statements? 1 Answer

Enemy's initial attack doesn't deal damage to player 1 Answer

How can i check if my Player was in the attack animation of my Enemy(2D) 1 Answer

How do i change my attack depending on which item i have equipped? 1 Answer

White vertical lines that flicker in 2d platformer,Vertical white flickering in 2d game 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges