- Home /
Question by
U92 · Aug 20, 2020 at 09:26 AM ·
gameobjecttransform.rotationtransform.rotate
Setting a blocks rotation to predetermined values.
angles 120, and 270 seem to break any solution i throw at it any ideas.
![using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UnityEngine;
public class meinStruggle : MonoBehaviour
{
int blocksOn = 1;
int blockSpawned = 0;
int currentPosition = 4;
GameObject activeBlock;
int blockMovementSpeed = 100;
private float decay;
public float blockRotationSpeed = 100f;
float smooth = 5.0f;
Vector3[] blockPosition = new[] {
new Vector3(10f, 10f, 0f),
new Vector3(8.6602f, 10f, -5f),
new Vector3(4.999999f, 10f, -8.660254f),
new Vector3(-9.536743f, 10f, -10f),
new Vector3(-5.000001f, 10f, -8.660254f),
new Vector3(-8.660256f, 10f, -5f),
new Vector3(-10f, 10f, 0f),
new Vector3(-8.660258f, 10f, 5.000001f),
new Vector3(-5.000005f, 10f, 8.660255f),
new Vector3(-4.768372f, 10f, 10f),
new Vector3(4.999996f, 10f, 8.660257f),
new Vector3(8.660252f, 10f, 5.000003f),
};
Vector3[] blockRotation = new[] {
new Vector3(90, 0, 0),
new Vector3(90, 30, 0),
new Vector3(90, 60, 0),
new Vector3(90, 90, 0),
new Vector3(90, 120, 0),
new Vector3(90, 150, 0),
new Vector3(90, 180, 0),
new Vector3(90, 210, 0),
new Vector3(90, 240, 0),
new Vector3(90, 270, 0),
new Vector3(90, 300, 0),
new Vector3(90, 330, 0)
};
public GameObject block;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Reset();
if (blocksOn == 1 && blockSpawned == 0)
{
activeBlock = (GameObject)Instantiate(block, new Vector3(blockPosition[currentPosition][0], 30, blockPosition[currentPosition][2]), Quaternion.Euler(blockRotation[currentPosition][0], blockRotation[currentPosition][1], blockRotation[currentPosition][2]));
print(" BP is : " + blockPosition[currentPosition][0]);
blockSpawned = 1;
}
// keyboard input
float horizontalValue = Input.GetAxis("Horizontal");
float verticalValue = Input.GetAxis("Vertical");
if (horizontalValue < 0 && decay == 0)
{
currentPosition = currentPosition - 1;
//if current position is negative out of range going left, loop back to 11
if (currentPosition == -1)
{
currentPosition = 11;
}
// if current position is positive out of range going right, loop back to 0
if (currentPosition > 11)
{
currentPosition = 0;
}
decay = 0.25f;
print("Left");
// StartCoroutine(RotateMe(Vector3.right * -30, decay));
// activeBlock.transform.Rotate(blockRotation[currentPosition],Space.World);
//activeBlock.transform.rotation = Quaternion.Euler(90, 0, -1 * (currentPosition*30));
activeBlock.transform.Rotate(0,0, 30 , Space.Self);
}
if (horizontalValue > 0 && decay == 0)
{
currentPosition = currentPosition + 1;
//if current position is negative out of range going left, loop back to 11
if (currentPosition == -1)
{
currentPosition = 11;
}
// if current position is positive out of range going right, loop back to 0
if (currentPosition > 11)
{
currentPosition = 0;
}
decay = 0.25f;
print("Right");
// StartCoroutine(RotateMe(Vector3.right * 30, decay));
// activeBlock.transform.Rotate(blockRotation[currentPosition],Space.World);
}
if (verticalValue < 0)
{
//DROP THE BLOCK
print("Down");
}
if (verticalValue > 0)
{
print("Up");
}
// calc move
Vector3 newPos = Vector3.MoveTowards(activeBlock.transform.position, new Vector3(blockPosition[currentPosition][0], blockPosition[currentPosition][1], blockPosition[currentPosition][2]), blockMovementSpeed * Time.deltaTime);
//move
activeBlock.transform.position = newPos;
//calc tween rotation
//activeBlock.transform.rotation = Quaternion.LookRotation(blockRotation[currentPosition]);
// activeBlock.transform.rotation = new Vector3(blockPosition[currentPosition][0], blockPosition[currentPosition][1], blockPosition[currentPosition][2]);
print("CP : " + currentPosition);
}
private void Reset()
{
if (decay > 0)
{
decay -= Time.deltaTime;
}
if (decay < 0)
{
decay = 0;
}
}
IEnumerator RotateMe(Vector3 byAngles, float inTime)
{
var fromAngle = activeBlock.transform.rotation;
var toAngle = Quaternion.Euler(activeBlock.transform.eulerAngles + byAngles);
for (var t = 0f; t < 1; t += Time.deltaTime / inTime)
{
activeBlock.transform.rotation = Quaternion.Lerp(fromAngle, toAngle, t);
yield return null;
}
}
}][1]
[1]: /storage/temp/165896-50b03b6b91a1c3bf2457b9aea4ba19a6.png
50b03b6b91a1c3bf2457b9aea4ba19a6.png
(109.4 kB)
Comment
Answer by U92 · Aug 21, 2020 at 03:17 AM
screw it, after 20 hours and no resources for help you get dirty as code like this.
if (currentPosition == 9)
{
activeBlock.transform.rotation = block9.transform.rotation;
activeBlock.transform.position = block9.transform.position;
}
else if ( currentPosition == 3 )
{
activeBlock.transform.rotation = block3.transform.rotation;
activeBlock.transform.position = block3.transform.position;
}
else
{
// calc move
Vector3 newPos = Vector3.MoveTowards(activeBlock.transform.position, new Vector3(blockPosition[currentPosition][0], blockPosition[currentPosition][1], blockPosition[currentPosition][2]), blockMovementSpeed * Time.deltaTime);
//move
activeBlock.transform.position = blockPosition[currentPosition];
activeBlock.transform.rotation = Quaternion.Euler(90, (currentPosition * 30),0);
}
now to try and animate this jank
never would have thought rotating an object 30 degrees would take 20+ hours to end up with a jank solution.
Your answer
Follow this Question
Related Questions
Stopping transform.Rotate on an exact rotation value 1 Answer
Rotating an Object in Update Function 1 Answer
weird framrate behaviour in day/night cycle 0 Answers
Rotate Issue 0 Answers
transform.rotate just one time. 1 Answer