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.
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.