- Home /
Question by
MarushiaDark · May 31, 2014 at 04:35 AM ·
instantiateraycastquaternionprojectorsurface normal
Instantiate Projector at Normal Direction
Am trying to create a script that will instantiate a projector when and where the player clicks to create a sort of splatter effect on whatever surface is clicked. For this, I am using Raycasting because I want it to affect enemies, terrain, and Game Objects. I can get it to spawn in the right position, but I can only get it to go at a single hard-coded angle (e.g. facing straight down). What I want is to instantiate the projector so it appears the affect is lying flat on the surface, whether it be the ground, a wall, a slope, etc. I know I need to invoke the hit point's normal direction somehow, but I just don't know how to do it.
Any suggestions?
(I'm using C#, btw. Please excuse my comments, I use lots of comments when I work.)
using UnityEngine;
using System.Collections;
public class Painter : MonoBehaviour {
public float range = 10.0f;
public GameObject debrisPrefab;
public float cooldown = 0.2f; // Amount of time in seconds between shots.
private float cooldownRemaining; // Amount of time left before you can fire.
private float splatterRotation;
private float splatterScale;
// Update is called once per frame
void Update () {
cooldownRemaining -= Time.deltaTime;
if(Input.GetMouseButtonDown(0))
{
cooldownRemaining = cooldown; // Reset the cooldown time.
Ray ray = new Ray (Camera.main.transform.position, Camera.main.transform.forward);
RaycastHit hitInfo;
Physics.Raycast(ray, out hitInfo, range);
Vector3 hitPoint = hitInfo.point;
GameObject other = hitInfo.collider.gameObject;
// Paint onto surface.
if(debrisPrefab != null)
{
// Change the properties of the Splatter Effect game object.
splatterRotation = Random.Range(0f,360f);
splatterScale = Random.Range(.5f, 10f);
debrisPrefab.transform.localScale = new Vector3(splatterScale, 0f, splatterScale);
Instantiate(debrisPrefab, hitPoint + new Vector3(0f, 0.1f, 0f), Quaternion.Euler(90f, splatterRotation, 0f));
}
}
}
}
Comment