- Home /
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);
}
}
}
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.
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
Hey, sorry i don't know why but when i post the code it bunches together making it unreadable...
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
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);
}
}
}
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
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.
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.
Your answer
Follow this Question
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