- Home /
Question by
chrisall76 · Jan 12, 2013 at 05:46 AM ·
c#raycastfps
Ray.direction editing not working in C#?
Hello,
So today I decided to transfer my FPS code from UnityScript to C#, and I only have one more problem. this piece of code:
void ShootReg (){
Transform trf = transform; // a little optimization
RaycastHit hit;
Quaternion hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
Transform cam = Camera.main.transform;
Ray ray = new Ray(cam.position, cam.forward);
ray.direction.x += Random.Range(-CurrentSpreadF, CurrentSpreadF);
ray.direction.y += Random.Range(-CurrentSpreadF, CurrentSpreadF);
ray.direction.z += Random.Range(-CurrentSpreadF, CurrentSpreadF);
if(Physics.Raycast (ray, out hit, Distance)){
causes this error: "Cannot modify a value type return of 'UnityEngine.Ray.Direction'. Consider storing the value in a temporary variable."
Any help?
Comment
Best Answer
Answer by robertbu · Jan 12, 2013 at 06:01 AM
Something like this has always solved the issue for me:
Vector3 v3T = ray.direction;
v3T.y += Random.Range(-CurrentSpreadF, CurrentSpreadF);
v3T.y += Random.Range(-CurrentSpreadF, CurrentSpreadF);
v3T.z += Random.Range(-CurrentSpreadF, CurrentSpreadF);
ray.direction = v3T;
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Modifing an Enum value by a String? 2 Answers
Where can i find an easy raycast shooting tutorial for C#? 2 Answers
shooting multiple enemies using raycast 2 Answers
Raycasting to avoid falling (FPS game) 2 Answers