- Home /
How can I change jump direction?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cubejump : MonoBehaviour
{
public GameObject maincube;
private bool animate;
private float scratch_speed = 0.5f, startTime;
void FixedUpdate()
{
if (animate && maincube.transform.localScale.y > 0.4f)
{
PressCube(-scratch_speed);
}
else if (!animate)
{
if (maincube.transform.localScale.y < 1f)
{
PressCube(scratch_speed * 3f);
}
else if (maincube.transform.localScale.y != 1.5f)
maincube.transform.localScale = new Vector3(1f, 1f, 1f);
}
}
void OnMouseDown()
{
if (maincube.GetComponent<Rigidbody>())
{
animate = true;
startTime = Time.time;
}
}
void OnMouseUp()
{
if (maincube.GetComponent<Rigidbody>())
{
animate = false;
float force, diff;
diff = Time.time - startTime;
if (diff < 3f)
force = 190 * diff;
else
force = 300f;
if (force < 60f)
force = 60f;
maincube.GetComponent<Rigidbody>().AddRelativeForce(maincube.transform.up * force);
maincube.GetComponent<Rigidbody>().AddRelativeForce(maincube.transform.(***what I must write to ghange jump direction to -45 degrees Y?***) * force);
}
}
void PressCube(float force){
maincube.transform.localPosition += new Vector3(0f, force * Time.deltaTime, 0f);
maincube.transform.localScale += new Vector3(0f, force * Time.deltaTime, 0f);
}
}
Answer by unity_ek98vnTRplGj8Q · Dec 30, 2019 at 04:03 PM
I believe AddRelativeForce
is additive, so you can probably just call
AddRelativeForce(maincube.transform.up * force);
AddRelativeForce(maincube.transform.forward * force); //Forward
or
AddRelativeForce(maincube.transform.up * force);
AddRelativeForce(maincube.transform.forward * -1 * force); //Backward
To add the components of the force in whatever direction you want (note, this will add double the force value overall since you are applying it twice). If you want it all at once you can calculate your direction vector before hand
Vector3 direction = (transform.up + transform.forward).normalized; //Forward
AddRelativeForce(direction * force);
or
Vector3 direction = (transform.up + transform.forward*-1).normalized; //Backward
AddRelativeForce(direction * force);
This will only apply the force once in the desired direction
But I need certain direction , how can I do it? private float direction = new Vector3(0f, -45f, 0f); and maincube.GetComponent().AddRelativeForce(maincube.transform.direction * force);?
Be careful with your Vector3, you need to give it a direction vector, meaning a vector that points in the direction you want to jump. Vector3(0,-45,0)
is a vector that points straight down and is 45 units in length.
If you want just a vector pointing at a 45 degree angle, you can achieve this by adding two vectors of equal length -> (transform.forward + transform.up).normalized
. This will give you the same result as calling new Vector3(0,.5f,.5f)
. This vector points at a 45 degree angle, because it has both forward and upward components. If you wanted it to point at a -45 degree angle, you simply add the up vector to the backwards vector: (transform.forward*-1 + transform.up).normalized
If you want an angle other than 45 degrees you could do some more complicated stuff. Let's say I want a vector that is only 20 degrees off the vertical axis. I could do something like this:
//Angle off of the vertical axis we want to jump
float angleFromVertical = 20;
//We will start with the up vector
Vector3 startDirection = transform.up;
//Now we will ROTATE the up vector by the angle we want
Vector3 direction = Quaternion.Euler(angleFromVertical, 0, 0) * startDirection;
//Now we have a direction vector that is 20 degrees off the vertical, pointing slightly in the forward direction
AddRelativeForce(direction*force);