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 scole127 · Aug 22, 2012 at 01:50 PM · jumpbeginner

How do i make my object jump so when it reaches hte start position it stops falling, at the moment it just keeps falling down the screen and doesn't stop.

This is what i have so far...

 using UnityEngine;
 using System.Collections;
 
 public class DinoController : MonoBehaviour
 {
     float moveSpeed = 5;
     
     // Use this for initialization
     void Start ()
     {
         animation["Run"].wrapMode = WrapMode.Loop;
         animation["Idle"].wrapMode = WrapMode.Clamp;
     }
     
     // Update is called once per frame
     void Update ()
     {
         if(Input.GetKey(KeyCode.RightArrow))
         {
             transform.LookAt(new Vector3(-200,transform.position.y,transform.position.z));
             animation.CrossFade("Run");
         }
         else if(Input.GetKey(KeyCode.LeftArrow))
         {
             transform.LookAt(new Vector3(200,transform.position.y,transform.position.z));
             animation.CrossFade("Run");
         }
         else
         {
         animation.CrossFade("Idle");
         }

         if(Input.GetKey(KeyCode.UpArrow))
         {
         transform.Translate(Vector3.up * Time.deltaTime*moveSpeed);
         }
         else if(Input.GetKey(KeyCode.UpArrow))
         {
                 transform.Translate(Vector3.down * Time.deltaTime*moveSpeed);
         }
     }
 }

Comment
Add comment · Show 2
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 Khada · Aug 22, 2012 at 02:17 PM 0
Share

Please format that code. Can you read it?

avatar image Piflik · Aug 25, 2012 at 10:41 PM 0
Share

Edited that code for you. Please make sure to format your code on future posts (using the '101010' button), to make it easier for people to read.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Daryn · Aug 23, 2012 at 05:27 AM

I could not really understand the code you gave as it is really chaotic but if you want something to happen when you reach a certain height just use

if(transform.localPosition.y = "theHeightYouWant"){

whateveryou want it to do down here

} i probaly can give you a better answer i just need you to be more descriptive and neat

Comment
Add comment · Show 4 · 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 scole127 · Aug 23, 2012 at 06:12 AM 0
Share

Hey, sorry i don't know why but when i post the code it bunches together making it unreadable...

avatar image Daryn · Aug 23, 2012 at 03:34 PM 0
Share

It does that to everyone. i put an extra space inbetween my lines to get them to seperate.

and can you please rephrase the question

avatar image scole127 · Aug 24, 2012 at 02:08 AM 0
Share

O$$anonymous$$ so i'm making this game for a trainee-ship i have with an iPhone game company and we had a task to make this(just walking) for an introduction to the programs we use(Blender and Unity). and at the end of the tutorial they say for an extension try making a jump.

this isn't using physics and what they said to us was to make it so when you press up it jumps but like a jet pack where if you hold it down you will keep going up and let go it will fall down to the position it started at. I was wondering how i can make it check to see if i am holding the key down, if not to start falling and then stop at the same place that it started at.

i should also add there isn't a floor to make a collision with because the main object doesn't move its just the background scrolls through a cycle when holding the L-R arrow keys, the same keys trigger the animation of ruining to make it look like its moving, when its not. I think its called parallax scrolling?

This is the updated code i have at the moment, which goes up but not down...

using UnityEngine;

using System.Collections;

public class DinoController : $$anonymous$$onoBehaviour

{

float moveSpeed = 5;

// Use this for initialization

void Start ()

{

animation["Run"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Loop;

animation["Idle"].wrap$$anonymous$$ode = Wrap$$anonymous$$ode.Clamp;

}

// Update is called once per frame

void Update ()

{

if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow))

{

transform.LookAt(new Vector3(-200,transform.position.y,transform.position.z));

animation.CrossFade("Run");

}

else if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow))

{

transform.LookAt(new Vector3(200,transform.position.y,transform.position.z));

animation.CrossFade("Run");

}

else

{

animation.CrossFade("Idle");

}

{

if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow))

{

transform.Translate(Vector3.up * Time.deltaTime*moveSpeed);

}

else if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.None))

{

transform.Translate(Vector3.down * Time.deltaTime*moveSpeed);

}

}

}

avatar image Daryn · Aug 25, 2012 at 09:37 PM 0
Share

Well to make it stop you could do something like this

int startHeight,xPosition;

bool isButtonDown

Void Update () { xPosition = transform.localPosition.x;

if(transform.localPosition.y==startHeight && isButtonPressed==false){

transform.localPosition= new Vector2(xPosition,startHeight);

}

Have isButtonDown false when you dont have the button down.

Sorry just realize im pretty sure this only will work if the object it self moves

avatar image
0

Answer by lpye · Aug 23, 2012 at 12:45 PM

Reformatted the code listed:

 using UnityEngine; using System.Collections;
     
     public class DinoController : MonoBehaviour { 
         float moveSpeed = 5; // Use this for initialization 
     
         void Start () { 
             animation["Run"].wrapMode = WrapMode.Loop; 
             animation["Idle"].wrapMode = WrapMode.Clamp; 
         } 
         
         // Update is called once per frame 
         void Update () { 
             if(Input.GetKey(KeyCode.RightArrow)) { 
                 transform.LookAt(new Vector3(-200,transform.position.y,transform.position.z)); 
                 animation.CrossFade("Run"); 
             } else if(Input.GetKey(KeyCode.LeftArrow)) { 
                 transform.LookAt(new Vector3(200,transform.position.y,transform.position.z)); 
                 animation.CrossFade("Run"); 
             } else { 
                 animation.CrossFade("Idle"); 
             } 
             
             { 
                 if(Input.GetKey(KeyCode.UpArrow)) { 
                     transform.Translate(Vector3.up * Time.deltaTime*moveSpeed); 
                 } else if(Input.GetKey(KeyCode.UpArrow)) { 
                     transform.Translate(Vector3.down * Time.deltaTime*moveSpeed); 
                 } 
             } 
         } 
     }
 

I'm seeing in the Update() function two references to KeyCode.UpArrow. I'm assuming that one of those should be KeyCode.DownArrow.

Anyway, without knowing more about the rest of your project, I suspect the problem is to do with your use of the Translate() function. Translate ignores collisions at is part of the Transform, not part of the Rigidbody. The Rigidbody handles physics interactions. Assuming your objects have a Rigidbody, you would be better suited applying an appropriate force through rigidbody.AddForce() rather than doing a translate. This also assumes that there is a suitable "floor" object with a Rigidbody attached to stop the object from going too far down.

If, on the other hand, you are not using physics, then Translate() is what you will need to use but you when performing any translations, you will need to check your boundary conditions. i.e. check to see if the object has moved too high or too low as a result of the most recent translation and then correct it.

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 Piflik · Aug 25, 2012 at 10:52 PM

If you want to make a Jump behavior with transform.Translate, you need to check somehow, if the player reached the bottom. For example you could check for his Y-Position when you make him fall, and when it reaches a certain point, stop the downwards movement (or the background's upwards movement).

This really only works with a constant floor. If you have different heights, you could parent some colliders to the background plane and then trigger the stopping of the background that way.

I personally would make the movement in two parts (one horizontal, one vertical) and then add these two together, so you can easily set one of them zero without affecting the other.

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

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

10 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

Related Questions

Multiple Cars not working 1 Answer

Moving the camera with mouse not working 1 Answer

I need help with C# 0 Answers

Item/inventory system-equipable items 1 Answer

How do you create a program to link scenes together? 1 Answer


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