- Home /
Instantiated projectile always floats up
I have a Light_Spell script attached to a magic wand which is parented to a Razer Hydra hand object. The Light_Spell takes a prefab of a Light which is projected out of it when a button is pressed. However the light is just moving up, no matter what way I rotate the hand object, it always goes up. I had it working but I changed some code around and can't remember how I got it working in the first place.
Here is the code I have so far:
if (isSelectedSpell && SixenseInput.Controllers[0].GetButtonDown (SixenseButtons.BUMPER) && triggerIsPressed == false) {
Rigidbody instantiateProjectile = Instantiate(wandLightProjectilePrefab, transform.position, transform.parent.transform.rotation) as Rigidbody;
}
I have tried setting Vector3 to up and forward and a whole set of different things. Any ideas on what I should do to make it match the rotation of where the hand is pointing and stuff? Thanks
If this is true (Wand with Light_Spell Script is child of Razer Hydra hand), then my assumption is that the wand's rotation is either 180 degrees on the x or z axis. that would put the Vector3.down pointing towards the world.up. Try using transform.parent.transform.rotation and see what happens.
So no matter what direction you choose it always goes up where? In World coordinates or relative to the back of the hand? Have you tried using .localPosition?
It could be a rotation thing as Ed mentioned. But if you tried every direction then it may be something else. It's definitely strange behaviour.
@Edunity Yeah I tried that just there and nothing happened :S I got rid of the line:
instantiateProjectile.position += Vector3.down * 20.0F;
and the light still just moves up when I used this as the instantiate:
Rigidbody instantiateProjectile = Instantiate(wandLightProjectilePrefab, transform.position, transform.parent.transform.rotation) as Rigidbody;
What exactly is this wandLightProjectilePrefab that you are spawning? Is it some sort of particle system or just a Point Light?
@Leuthil I have tried forward and down and get the same thing. When I add in that line I keep getting nullreferenceexceptions on the line after I make the rigidbody. I have dragged the prefab onto the script in the inspector so not sure why it is saying it isn't set to the instance of an object
Answer by trololo · Mar 26, 2014 at 09:46 PM
RaycastHit hit;
if(Input.GetMouseButtonDown(0) && Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
{
GameObject projectile = Instantiate(projectilePrefab);
StartCoroutine(MoveTo(projectile, hit.point);
}
// ...
IEnumerator MoveTo(GameObject proj, Vector3 hit)
{
while(Vector3.Distance(proj.transform.position, hit) > 1)
{
proj.transform.position = Vector3.Lerp(proj.transform.position, hit, Time.deltaTime);
yield return null;
}
}
@trololo this didn't work. the light started far away from me and like moved towards me and then curved up and went out of the scene. closer than where I was before however
Well just instantiate your projectile at your player location!
GameObject projectile = Instantiate(projectilePrefab, transform.position, transform.rotation);
@trololo It didn't work but I managed to fix the problem