How can we roll the box not cube also need to work with physics
Hi Guys, I need to roll the box (not the cube), the size of the box will be like this x=1, y=2, z=1 How can we achieve this? Also I need to fall down the box if not exits floor,
Please watch this video. https://youtu.be/C_6m1uVw49g
public class cubePlayer : MonoBehaviour
{
public bool isRolling;
public float rotateSpeed;
private Bounds bound;
private Vector3 left, right, up, down;
// Start is called before the first frame update
void Start()
{
bound = GetComponent<BoxCollider>().bounds;
left = new Vector3(-bound.size.x / 2, -bound.size.y / 4, 0);
right = new Vector3(bound.size.x / 2, -bound.size.y / 4, 0);
up = new Vector3(0, -bound.size.y / 4, bound.size.z / 2);
down = new Vector3(0, -bound.size.y / 4, -bound.size.z / 2);
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.A) && !isRolling)
{
StartCoroutine(Roll(left));
}
else if (Input.GetKey(KeyCode.D) && !isRolling)
{
StartCoroutine(Roll(right));
}
else if (Input.GetKey(KeyCode.W) && !isRolling)
{
StartCoroutine(Roll(up));
}
else if (Input.GetKey(KeyCode.S) && !isRolling)
{
StartCoroutine(Roll(down));
}
}
IEnumerator Roll(Vector3 positionToRotation)
{
isRolling = true;
Debug.Log(positionToRotation);
float angle = 0f;
Vector3 point = transform.position + positionToRotation;
Vector3 axis = Vector3.Cross(Vector3.up, positionToRotation).normalized;
while (angle < 90f)
{
float angleSpeed = Time.deltaTime + rotateSpeed;
transform.RotateAround(point, axis, angleSpeed);
angle += angleSpeed;
yield return null;
}
transform.RotateAround(point, axis, 90 - angle);
isRolling = false;
}
}
You're definitely abusing Coroutines. I have no idea what exactly you are trying to achieve, since I cant watch the video right now, but you invoke one or more coroutines every frame in which there is input registered.
You dont need coroutines here. Use the Update() loop as your "iterative function" when moving or rotating things.
Your answer
Follow this Question
Related Questions
Issue with physic engine 0 Answers
Cloth constraints don't constrain *o* 1 Answer
unity 5 physics.raycast ignores mesh collider 1 Answer
A fast question about player physics (Unity 5, C#) 0 Answers