- Home /
Question by
sk00ter · Mar 05, 2015 at 10:46 AM ·
rotationinstantiatequaternion
Instantiated Object Rotating Strangely
I'm writing a code to instantiate a prefab after clicking a given face of a block (think Minecraft mechanics).
This is a script that is attached the the actual prefab block itself:
void Update() {
timer += Time.deltaTime;
if (Input.GetMouseButton(0) && timer >= timeBetweenBlock && Time.timeScale != 0) {
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit)) {
Debug.DrawLine(blockObj.position, hit.point, Color.red);
spawnBlock(prefab, hit, 0.5f);
}
}
}
void spawnBlock ( GameObject prefab, RaycastHit hitPos, float distanceFromCenter ){
timer = 0f;
clone = Instantiate(prefab, hitPos.transform.position, Quaternion.FromToRotation(Vector3.forward, hitPos.normal)) as GameObject;
clone.transform.Translate(Vector3.forward * (distanceFromCenter*2));
}
It raycasts out and gets the hit data of the collider it touches and would otherwise work fine except for the new instantiated object's (clone) rotation is all messed up.
For example:
If I click the Back Face of the block; No Rotation
If I click the Top Face of the block; Clone's X rot is 270
If I click the Right Face; Clone's Y rot is 270
Left Face; Y is 90
Front Face; Clone's Y rot is 180, Z rot is 180
I don't understand Quaternions and I think my problem lies in there somewhere. Any advice? Thanks in advance!
Comment