Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ericgobbo · Oct 26, 2020 at 11:20 AM · movementmovement scriptflying

Make character move left or right only if it is in the air,Make character move left or right only in the air.

It's been some days i am working in my first game, where you controls a balloon that needs to drop boxes in the right places, similar to Balloon Fight but you need only to hold the button to fly, instead of pressing the button to fly a lot of times. As i never programmed something before, i only learned and know a little of the basics of c# before doing this project. So the question is, how i make my character move left or right only if it is in the air after pressing the thrust button? And also, how i make my character not give a little "jump" everytime i press the thrust button. Below are the codes of the character: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class Jogador : MonoBehaviour
 {
 
     // Velocidade de deslocamento
     public float velocidade = 5.0f;
 
     // Força do impulso do personagem
     public float impulso = 5.0f;
 
     // Intensidade da gravidade
     public float gravidade = 0.5f;
 
     // Limite que o personagem pode atingir
     // nas laterais da tela
     public float limiteX = 4.0f;
 
     // Estados
     private bool impulsionar;
 
     // Referência para os componentes
     private Rigidbody2D rb;
     private SpriteRenderer sr;
 
     // Instruções iniciais
     void Start()
     {
         // Referência para o componente rigid body 2D
         rb = GetComponent<Rigidbody2D>();
         // Referência para o componente sprite render
         sr = GetComponent<SpriteRenderer>();
         // Atribui nova gravidade
         rb.gravityScale = gravidade;
     }
 
     // Game Looping
     void Update()
     {
 
         /*
          * Lê os inputs das teclas A e D ou das 
          * setas esquerda e direita gerando um 
          * valor do tipo float entre 0 e 1 ou 0 e -1, 
          * sendo 0 quando não há input, 1 quando o input 
          * é para o lado direito e -1 para o lado esquerdo.
          * Esse valor é multiplicado pela velocidade, fornecendo 
          * uma velocidade 0 quando não há input, negativa quando 
          * multiplicada por por -1 e positiva quando multiplicada por 1.
          */
         float px = Input.GetAxis("Horizontal") * velocidade * Time.deltaTime;
 
         // Move o personagem para os lados
         transform.Translate(px, 0.0f, 0.0f);
 
         // Verifica se a barra de espaço foi pressionada 
         if (Input.GetButton("Jump"))
         {
             // Altera o estado para verdadeiro para
             // executar o impulso
             impulsionar = true;
         }
 
     }
     // Função para instruções de fisica
     private void FixedUpdate()
     {
         // Aplica impulso para cima
         if (impulsionar)
         {
             rb.velocity = new Vector2(0.0f, impulso);
             impulsionar = false;
         }
     }
   
 }

Some words are in portuguese because i am Brazillian.

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

0 Replies

· Add your reply
  • Sort: 

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

207 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 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

Charactercontroller won't go down, only goes up 1 Answer

Accelerometer 2 Answers

MouseLook character is acting wierd 1 Answer

Character Controller - unknown rotation, flying 1 Answer

HOW TO MAKE A NPC GO FORWAR 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