- Home /
How to make a sphere reflect at a random angle using Vector3.Reflect?
Hi there,
I have encountered a problem using Vector3.Reflect
.After quite an extensive searching I still have no idea how to make it work correctly. Basically I want to make a sphere in the game to bounce of the wall at a angle,but have only managed to make it bounce back in the direction that the sphere came in at.
I have tried to use:
movementArray[0] = Vector3.Reflect(movementArray[0].normalized,reflectionArray[0]);
, but that just resulted in the sphere moving a lot faster in a given direction.
So my question is to basically how to make the Vector3.Reflect
work correctly since i've been at it for a few days and it's still very unclear :D
By the way here is the code snippets:
//First element of the movement array that makes the sphere move in a random direction
movementArray[0] = (new Vector3(randomX,randomY,randomZ) * movementSpeed * Time.deltaTime);
reflectionArray[0] = -Vector3.forward;
//I know that something has to be done here to make the sphere reflect at an angle,
//but have no idea of what that might be.
movementArray[0] = Vector3.Reflect(movementArray[0],reflectionArray[0]);
Any suggestions or tips are greatly welcome :) Peace!
I fuzzy here on what you are doing. For reflect, you need the normal to the surface you will be reflecting off of:
Typically you get the normal through raycasting or by a collision point. The second thing you need is the velocity vector of the inco$$anonymous$$g object. And I not sure how random you want the outgoing vector. If it is completely random, then you don't need Reflect() at all. I don't see either a normal or an inco$$anonymous$$g vector in your calculations above. I think you need to back up from your specific question to describe the effect you are trying to achieve.
Aha.Well basically I have a sphere that moves in a random direction at the start.When this sphere eventually comes to the boundry I want it to either reflect at the angle it came in or have a random angle at which it reflects at.
I have been thinking of using raycasting to possibly do something like that. And well I used this http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Reflect.html to make a particle reflect.Even though I'm still hazy on what the code really means and how it works.
You mentioned that if the reflection angle was completely random then i don't need the Reflect()
at all.Why is this exactly?Can I simply say that "if the sphere collides with the boundry then reverse it's motion at an angle"?
Also how would i go about getting the velocity vector?I know it might be something obvious,but i got no clue.
How are you moving the sphere: Transform, Rigidbody or Character Controller? How are you defining the boundry: $$anonymous$$athematically, Visual element, Collider, Trigger, Other?
Getting the velocity will depend on how you are moving the sphere. If you are using a Rigidbody, then you can simply use rigidbody.velocity. If you are moving your object through the transform, you will have to keep track of a previous position and time, and calculate the velocity.
As for my comment about not using Reflect(). If the bounce is completely random, you can simply generate a random direction off the wall. You will need the normal so that the calculation is away from the wall. And you will need the magnitude (speed) of the inco$$anonymous$$g vector so that the ball heads back out relative to the inco$$anonymous$$g speed. But I'm guessing you don't want something totally random. You probably want to introduce some randomness into the bounce.
Ah yes should of mentioned those it seems.The sphere is moved through transform.Translate
and the boundry is defined using colliders.
And i will be able to introduce the randomness into the bounce once i know how to make it bounce at an angle :D Thank you for the help so far by the way.Will get to trying these out as soon as possible.
Your answer
Follow this Question
Related Questions
Vector3.Reflect troubles 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Can someone help me fix my Javascript for Flickering Light? 6 Answers