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 GreenTee · Mar 02, 2015 at 10:59 AM · c#movementmovemove an object

Move character up and down while animation runs

Hello,

i got a question. I created a flappy bird clone with a tutorial but i want to add the death feature. When the player is death(collides), he has to move up a bit and then fall through the ground(like in mario).

I tried to use the code below but nothing happend. I think it is important to say that the death is an animation, because the bird turns from normal color into gray.

 var pos = transform.position;
 pos.y += 1;

Is there any possibility?

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
1

Answer by maccabbe · Mar 02, 2015 at 11:06 AM

You never tell the object what its new position is, add the following line

 transform.position=pos;
Comment
Add comment · 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 GreenTee · Mar 02, 2015 at 07:45 PM 0
Share

i added the code and it works but the "player" does not move upwards, it is more like a teleportation + 1 in y-axis

avatar image maccabbe · Mar 02, 2015 at 08:36 PM 1
Share

Two ways to make the object move gradually are

1) $$anonymous$$ake the object move every frame using the UpdateFunction

 void Update (){
     transform.position+=Time.deltaTime*Vector3.up;
 }

2) Add a rigidbody2D to the gameObject and give that object a momentum using AddForce

 void Start(){
     rigidbody2D.AddForce(new Vector2.up, Force$$anonymous$$ode2D.Impulse); 
 }
avatar image GreenTee · Mar 02, 2015 at 09:32 PM 0
Share

the problem is that the dead = true is in the OnCollisionEnter2D() function

 void OnCollisionEnter2D(Collision2D collision) {
 
         if (god$$anonymous$$ode)
         return;
 
         animator.SetTrigger("Death");
         dead = true;
 
 
 
         }
     }

and in my Update() function the if(dead == true) does not work(does nothing)

     void Update() {
 
         if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space) || Input.Get$$anonymous$$ouseButtonDown (0)) {
             didFlap = true;
 
 
             //make dead bird jump and fall through ground like $$anonymous$$ARIO
             if (dead == true) {
         /*        var pos = transform.position;
             pos.y += upwardSpeed;
             transform.position=pos;       
                 
             Rigidbody2D.AddForce(new Vector2(0f, 1f),Force$$anonymous$$ode2D.Impulse); */
                 
                 transform.position+=Time.deltaTime*Vector3.up;
 
             }
 
         }

ADDED::

I copied this code into the if statement that returns the "dead"

         if (dead) {
 
             transform.position+= upwardSpeed *  Time.deltaTime*Vector3.up;
             return;
             }

Now it is bouncing.

avatar image
1

Answer by Zhorky · Mar 02, 2015 at 01:43 PM

I would try to do this with

  Rigidbody2D.AddForce(new Vector2(0f, 1f),ForceMode2D.Impulse);

Comment
Add comment · Show 2 · 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 GreenTee · Mar 02, 2015 at 07:46 PM 0
Share

tried to add but error appears:

An object reference is required to access non-static member `UnityEngine.Rigidbody2D.AddForce(UnityEngine.Vector2, UnityEngine.Force$$anonymous$$ode2D)'

What do i have to do?

avatar image Zhorky · Mar 04, 2015 at 10:49 AM 0
Share

rb2d = GetComponent(); rb2d.AddForce(new Vector2(0f, 1f),Force$$anonymous$$ode2D.Impulse);

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Can't jump in C# 1 Answer

Help with elevator Script 2 Answers

Move relative to objects rotation (C#) 1 Answer

c# script not doing anything. 2 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