• 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
Question by Naografix · Sep 16, 2014 at 05:30 PM · c#rotationmovementsprite

(sprite2D) Movement like this game...

Hey !

I have a small problem with my game, i want to create a game similar of t$$anonymous$$s game (the movement) : http://armorgames.com/play/10912/space-is-key

And i made t$$anonymous$$s : (I'm new in Unity :/)

 void jump()
 {
     if (Input.GetKey(KeyCode.Space) && Global.isGrounded == true)
     {
         rigidbody2D.AddForce(Vector3.up * jumpForce);
         isJumped = true;
     }
     else if(Global.isGrounded == false)
     {
         rigidbody2D.AddForce(-Vector3.up * jumpForce / slowJump);
     }


     if (Global.isGrounded == false)
     {
        targetAngles = transform.eulerAngles - 180f * Vector3.forward;
        transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, targetAngles, speedRotation * Time.deltaTime);
     }

 }

 //MOVEMENT
 void Movement()
 {
     transform.Translate(Vector2.right / slowSpeed, Space.World);
 }


I dont know how can i improve my movement and my jump... Can you help me ?

Comment

People who like this

0 Show 0
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
Best Answer

Answer by robertbu · Sep 16, 2014 at 05:54 PM

Rather than using AddForce(), I'd do the jump mathematically. Here is some example code to get you part of the way there:

 using UnityEngine;
 using System.Collections;
 
 public class MoveAndJumpAndFlip : MonoBehaviour {
 
     public float speed = 4.0f;
     public float jumpTime = 1.2f;
     public float jumpHeight = 1.4f;
 
     private float xPos;
     private float yPos;
     private float deltaY = 0.0f;
     private bool isJumping = false;
 
     void Start () {
         xPos = transform.position.x;
         yPos = transform.position.y;
     }
     
     void Update () {
         if (!isJumping && Input.GetKeyDown (KeyCode.Space)) {
             StartCoroutine(Jump());
         }
         Vector3 newPos = transform.position;
         xPos += speed * Time.deltaTime;
         newPos.x = xPos;
         newPos.y = yPos + deltaY;
         rigidbody2D.MovePosition (newPos);
     }
 
     IEnumerator Jump() {
         if (isJumping) yield break;
         isJumping = true;
         float timer = 0.0f;
         w$$anonymous$$le (timer <= 1.0f) {
             transform.rotation = Quaternion.AngleAxis (-180.0f * timer, Vector3.forward);
             deltaY = Mathf.Sin (timer * Mathf.PI) * jumpHeight;
             timer += Time.deltaTime / jumpTime;
             yield return null;
         }
         deltaY = 0.0f;
         transform.rotation = Quaternion.identity;
         isJumping = false;
     }
 
     void OnTriggerEnter2D(Collider2D col) {
         if (col.tag == "Obstical") {
             Debug.Log ("Hit an obstical");
         }
         if (col.tag == "Star") {
             Debug.Log ("Hit star");
         }
     }
 }


I don't know to what extent you are duplicating the game at the link, but 'yPos' will be the height of the player object. So as you move down, you will set yPos to a new value. T$$anonymous$$s code assumes that the obstacle blocks have box colliders set to trigger...as do the stars. The "surface" the player block runs along does not have collider since it is simulated.

Comment
The Pirate Duck

People who like this

1 Show 3 · 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 Naografix · Sep 16, 2014 at 06:10 PM 0
Share

Ohhh thx dude !

It work perfectly ! <3

I have an other question now, i play Garry's Mod (Steam game) and i can dev in this game. They have a function to find if my var is changed, and execute just one time, like :

if(changed(Var) && Var == true){ print("hello")}

I havent loop if my var == true.

How can i do that in Unity ?

I want to do that because i want to save my position when my cube hit the world for the fist time

avatar image robertbu · Sep 16, 2014 at 06:12 PM 0
Share

There's nothing like the 'changed' function in Unity/C#. You'll have to code the logic yourself...save a copy of the original value and compare.

avatar image Naografix · Sep 16, 2014 at 06:20 PM 0
Share

Yep, i made this :) thx Robertbu.

Well, for me its ok. Problem fixed.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Rotating a object towards a direction then move it 0 Answers

Having Issues Rotating 2d Sprites to face another 2d Object 3 Answers

how do i make an object always face the player? 5 Answers

Joystick 2D Sprite Rotation and Movement 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