- Home /
RaycastHit.normal
Hello, i have a little problem to my C# script... i have this lines :
addBlock.transform.position = hit.transform.position + hit.normal; addBlock.transform.rotation = hit.transform.rotation;
I would like add "+hit.normal" after hit.transform.rotation (do : hit.transform.rotation + hit.normal) but i have this error : error CS0019: Operator +' cannot be applied to operands of type
UnityEngine.Quaternion' and `UnityEngine.Vector3' and i don't understand how to do this...
The complete code :
using UnityEngine;
using System.Collections;
public class Wood_Block_Build : MonoBehaviour {
RaycastHit hit;
int MaxBuildDist = 10;
Transform addBlock;
Transform deleteBlock;
public int iDBlock = 1;
//Transform blockRotat;
void Start () {
addBlock = GameObject.Find ("addBlock").transform;
deleteBlock = GameObject.Find ("deleteBlock").transform;
}
void Update () {
if (Physics.Raycast (Camera.main.ScreenPointToRay (new Vector3 ((Screen.width / 2), (Screen.height / 2), 0)), out hit, MaxBuildDist))
{
addBlock.GetComponent<Renderer>().enabled = true;
if (hit.transform.tag == "Block")
{
addBlock.transform.position = hit.transform.position + hit.normal;
addBlock.transform.rotation = hit.transform.rotation;
deleteBlock.transform.rotation = hit.transform.rotation;
deleteBlock.transform.position = hit.transform.position;
deleteBlock.GetComponent<Renderer>().enabled = true;
}
if (hit.transform.tag != "Block")
{
deleteBlock.GetComponent<Renderer>().enabled = false;
addBlock.transform.position = new Vector3 (hit.point.x, hit.point.y + 0.5f, hit.point.z);
}
//Rotation
if (Input.GetAxis("Mouse ScrollWheel") <0f && Input.GetKey(KeyCode.N)) // forward
{
deleteBlock.transform.rotation *= Quaternion.Euler(0f, 0f, -2f) ;
addBlock.transform.rotation *= Quaternion.Euler(0f, 0f,-2f);
}
if (Input.GetAxis("Mouse ScrollWheel") >0f && Input.GetKey(KeyCode.N)) // forward
{
deleteBlock.transform.rotation *= Quaternion.Euler(0f, 0f, 2f) ;
addBlock.transform.rotation *= Quaternion.Euler (0f, 0f, 2f);
}
if (Input.GetAxis("Mouse ScrollWheel") <0f && Input.GetKey(KeyCode.B)) // forward
{
deleteBlock.transform.rotation *= Quaternion.Euler(0f, -2f, 0f);
addBlock.transform.rotation *= Quaternion.Euler (0f, -2f, 0f);
}
if (Input.GetAxis("Mouse ScrollWheel") >0f && Input.GetKey(KeyCode.B)) // forward
{
deleteBlock.transform.rotation *= Quaternion.Euler(0f, 2f, 0f);
addBlock.transform.rotation *= Quaternion.Euler (0f, 2f, 0f);
}
//Input
if (Input.GetMouseButtonDown (0))
{
iDBlock++;
GameObject block = (GameObject)Instantiate (Resources.Load ("Wood_Block"), addBlock.transform.position, addBlock.transform.rotation);
block.name = "0" + iDBlock ;
}
else if (Input.GetMouseButtonDown (1) && hit.transform.tag == "Block" && hit.transform.tag != "Player")
{
Destroy (hit.transform.gameObject);
}
}
else
{
addBlock.GetComponent<Renderer>().enabled = false;
deleteBlock.GetComponent<Renderer>().enabled = false;
}
// if is exist, save it
if (GameObject.Find ("0" + iDBlock) != null)
Debug.Log (iDBlock + " is exist");
}
}
Answer by Namey5 · Apr 08, 2017 at 07:44 AM
This is because RaycastHit.Normal returns a Vector3, whilst Transform.rotation is a Quaternion. They may not appear to be different types (they do similar things) but in actual fact have no relation to each other. In order to convert the Vector3 into a Quaternion, you can use Quaternion.LookRotation();
addBlock.transform.rotation = hit.transform.rotation * Quaternion.LookRotation (hit.normal); //You have to multiply Quaternions to add them, its a bit confusing
thanks you !
and i have a last question.. I can instantiate many prefab of "Wood_block" and each "Wood_Block" have a different Id (Id is 1,2,3,4,.....) and i would like get all Id if is instantiate and save it with playerPrefs. I think i need to do a table but i don't know how to do this...
Your answer
Follow this Question
Related Questions
Change Transparency 1 Answer
Need help with OnUse() script! 1 Answer
Want to move object slowly to where the mouse clicks? 1 Answer
Break Block Like Minectaft 2 Answers
Layer and LayerMask Variables 0 Answers