- Home /
How can I make an object in my scene jump continuously?
Hello Unity people! I am currently working on a game that will have the player collecting object in the level. I wont get into detail as its not important. I will have objects in the scene the player is collecting, in this case I want it to be a sheep. For testing purposes, the "sheep" is currently just a cube, and I have it moving around in a circle. However, I want it to continuously jump as well as it moves around. using UnityEngine; using System.Collections; public class Movement : MonoBehaviour { %|1007631654_1|% } void Update () { transform.Translate (new Vector3 (1, 0, 0) * Time.deltaTime); transform.Rotate (new Vector3 (0, 45, 0) * Time.deltaTime); %|-1896819847_6|% }
That is what I have for moving the "sheep" (cube) in a circle. I tried simply adding another line to the update: "transform.Translate (new Vector3 (0, 3, 0) * Time.deltaTime);". However, it would jump a couple times like this then would glitch like crazy landing on it side and flying into the walls. Anybody know how I could make the cube continuously jump? Thanks in advance!
Sorry about the % and numbers... ignore them, some sort of formatting issue must have occurred. They do not exist in my script.
Answer by satyagames · Aug 31, 2015 at 11:13 AM
using UnityEngine;
using System.Collections;
public class rotateScript : MonoBehaviour {
public float minRotationSpeed = 80.0f;
public GameObject pivot;
bool grounded;
void Start() {
}
void Update() {
transform.RotateAround(pivot.transform.position, Vector3.up, minRotationSpeed * Time.deltaTime);
if (grounded)
{
transform.Translate(Vector3.up * 2 * Time.deltaTime, Space.World);
}
}
void OnCollisionEnter(Collision collision) {
if (collision.gameObject.tag == "ground") {
grounded = true;
} else {
grounded = false;
}
}
}