Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by vanessa101 · Jun 14, 2016 at 07:40 AM · movementscript.jump

Cant Move towards Right and Jump at same time?

Im new to unity and im trying to make a 2d game that requires my object to move towards the right and jump Unfortunately, im unable to make the object move and jump at the same time. Can someone help? Heres the script im using. It appears to be that if the object is moving towards the right, the object is unable to jump. But if the object is able to jump, it is unable to move towards the right. Is my script over replacing the jump script?

Script #C

using UnityEngine; using System.Collections;

public class Frog : MonoBehaviour {

 public Vector3 userDirection = Vector3.right;
 float maxJumpHeight = 4.0f;
 float groundHeight;
 Vector3 groundPos;
 float jumpSpeed = 15.0f;
 float fallSpeed = 10.0f;
 public bool inputJump = false;
 public bool grounded = true;
 public static int movespeed = 1;

 void Start()
 {
     
     groundPos = transform.position;
     groundHeight = transform.position.y;
     maxJumpHeight = transform.position.y + maxJumpHeight;
     
 }


 void Update()
 {
     transform.Translate(userDirection * movespeed * Time.deltaTime);

     if (Input.GetKeyDown(KeyCode.Space)) Physics.Raycast(transform.position, -transform.up, 1);
     {
         if (grounded)
         {
             groundPos = transform.position;
             inputJump = true;
             StartCoroutine("Jump");
         }
     }
     if (transform.position == groundPos)
         grounded = true;
     else
         grounded = false;
 }

 IEnumerator Jump()
 {
     while (true)
     {
         if (transform.position.y >= maxJumpHeight)
             inputJump = false;
         if (inputJump)
             transform.Translate(Vector3.up * jumpSpeed * Time.smoothDeltaTime);
         else if (!inputJump)
         {
             transform.position = Vector3.Lerp(transform.position, groundPos, fallSpeed * Time.smoothDeltaTime);
             if (transform.position == groundPos)
                 StopAllCoroutines();
         }

         yield return new WaitForEndOfFrame();
     }
 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by EpiFouloux · Jun 14, 2016 at 12:29 PM

Does this even compile?

You have on line 25 a Physics.Raycast() just after the if statement doing nothing ! And I believe your doing it in a very complicated manner, try something more like that : (I don't garanty that it's perfect, juste wroted it from scratch)

 public class player_movement : MonoBehaviour
 {
  private Vector3 movement;
  private float ground;
  private float speed = 15f;
  private float jump_power = 20f;
  public float gravity = 5f;
 
  void Start()
  {
    ground = transform.position.y;
  }
 
  void Update()
  {
   movement.x = speed; // moving to the right
   if (transform.position.y == ground)
       movement.y = (Input.GetButton("Jump") ? jump_power : 0f); // jump
   else
       movement.y -= gravity; // gravity
   transform.Translate(movement * Time.deltaTime); // applies movement
  }
 }

EDIT : changed the jmping part, does the movement work?

Comment
Add comment · 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
0

Answer by vanessa101 · Jun 14, 2016 at 05:31 PM

Hmm, it still didnt make the object move nor jump

Comment
Add comment · Show 5 · 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 vanessa101 · Jun 14, 2016 at 05:51 PM 0
Share

Where do i place the jump and the move towards right script in this section?

avatar image EpiFouloux · Jun 15, 2016 at 07:29 AM 0
Share

well the movement.x = speed is the moving part.. and the movement.y is the jump part, I just editted the answer to make it more understandable

avatar image vanessa101 · Jun 15, 2016 at 02:17 PM 0
Share

Alright thanks i got the script to work the movement part worked but when i hit spacebar to jump it just falls down off the screen and through my ground. Does my ground object need something applied to it? or doesthe object need something

avatar image vanessa101 · Jun 15, 2016 at 02:18 PM 0
Share

your a genius, i love yu!. i just need that problem fixed above tho

avatar image EpiFouloux vanessa101 · Jul 04, 2016 at 08:55 AM 0
Share

can you accept my answer?

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

69 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

Related Questions

Help with jumping script 2 Answers

Need help with jumping on third person movement script. 0 Answers

I wrote a jumping script, but it isn't working and i'm not even getting any error codes. pls help. 0 Answers

This is frustrating 2 Answers

Help: Use a Trigger object to slow Player while inside object 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