- Home /
Is it possible to constrain an object's rotation in worldspace?
I'm a beginner.
My player is a cube that moves by rotating 90 degrees on an axis (so if I constrained y rotation in inspector, it would disable part of my movement). If I change directions too soon before the rotation is finished, it has a tendency to rotate very slightly on the y axis, so that now when I move forward or back, I'm no longer parallel with the tiles I'm moving across.
Thanks in advance for any help you can offer.
We'll need to see code that's doing what you describe to be of much help.
Answer by peaceofmind1188 · Sep 06, 2018 at 04:48 AM
You can write the code so that every time the forward or back button is pressed down the rotation is set to exactly 90 degrees.
Keep at it! I just solo developed my first app and it's now on Itunes and Android! It's called Microbz!
https://itunes.apple.com/us/app/microbz/id1418937151?ls=1&mt=8 https://play.google.com/store/apps/details?id=com.peaceofmindgames.microbz
I hope my suggestion works and I hope you get a chance to try my game =)
If you want more insight on complete game development, I am doing a step by step process on Instagram at https://www.instagram.com/peaceof$$anonymous$$d_games/
Also, holding a live session 09/11/2018 at 7:00P$$anonymous$$ PST at https://ga$$anonymous$$gama.amafeed.com/how-to-create-a-game-with-zero-experience-ask-me-841927
Answer by Koyemsi · Sep 06, 2018 at 04:13 AM
Maybe the player input should be disabled before the rotation is complete ?
I found this script online and copy/pasted it. I've read through and have a general understanding of how it works.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Player$$anonymous$$ovement : $$anonymous$$onoBehaviour { public float speed; // The point the cube will rotate around // They represent the middle point of each 4 bottom edges of the cube Vector3 forwardRotationPoint; Vector3 backRotationPoint; Vector3 leftRotationPoint; Vector3 rightRotationPoint; Bounds bounds; bool rolling;
void Start()
{
bounds = GetComponent<$$anonymous$$eshRenderer>().bounds;
// Compute the rotation points
forwardRotationPoint = new Vector3(0, -bounds.extents.y, bounds.extents.z);
backRotationPoint = new Vector3(0, -bounds.extents.y, -bounds.extents.z);
leftRotationPoint = new Vector3(-bounds.extents.x, -bounds.extents.y, 0);
rightRotationPoint = new Vector3(bounds.extents.x, -bounds.extents.y, 0);
}
void Update()
{
// $$anonymous$$ake sure you are not already rolling / moving
if (rolling)
return;
// Rotate around forward point when pressing the up button
if (Input.Get$$anonymous$$ey("w"))
StartCoroutine(Roll(forwardRotationPoint));
// Rotate around back point when pressing the down button
else if (Input.Get$$anonymous$$ey("s"))
StartCoroutine(Roll(backRotationPoint));
// Rotate around left point when pressing the left button
else if (Input.Get$$anonymous$$ey("a"))
StartCoroutine(Roll(leftRotationPoint));
// Rotate around right point when pressing the right button
else if (Input.Get$$anonymous$$ey("d"))
StartCoroutine(Roll(rightRotationPoint));
}
// $$anonymous$$ake the cube roll around given rotation point
private IEnumerator Roll(Vector3 rotationPoint)
{
// Compute the real rotation point according to current position
Vector3 point = transform.position + rotationPoint;
// Compute an axis to rotate in the correct direction
Vector3 axis = Vector3.Cross(Vector3.up, rotationPoint).normalized;
float angle = 90;
float a = 0;
// Prevent the user from rolling since we already are
rolling = true;
while (angle > 0)
{
// Compute the angle and rotate the cube around the point
a = Time.deltaTime * speed;
transform.RotateAround(point, axis, a);
// $$anonymous$$eep track of the remaining angle
angle -= a;
yield return null;
}
// Adjust the rotation to make sure the cube rotates **exactly** 90°
transform.RotateAround(point, axis, angle);
// Allow the user to roll in a new direction
rolling = false;
}
}
I think the rolling bool is to disable movement while it's going, but I'm not sure what I might change to extend the window of time that player controls are disabled.
Your answer
Follow this Question
Related Questions
Player model Rigidbody keeps falling upsidedown 0 Answers
How to make a RigidBody not go into ground when tilted foward? 2 Answers
Stop A player Turning at Specific Point. 1 Answer
Rigidbody constraints not working with parent/child relationship 2 Answers
How does one constrain rotations of a CharacterController without a Rigidbody? 0 Answers