- Home /
Adding an offset to an instantiated objects rotation
I'm instantiating an object at another objects transform.position and transform.rotation and shooting it forward (it's a bullet) however I want to add an offset so that I can have the trajectory be offset +/- from the transform.rotation direction. Using Javascript in the previously written code. Please let me know if I can be more clear.
Here are the pertinent lines:
var roundClone : Rigidbody = Instantiate(round, transform.position, transform.rotation);
roundClone.velocity = transform.TransformDirection(Vector3.up * roundSpeed);
Thank you!
Depending on your goal, there are a number of ways. A simple way which may or may not be what you are looking for is to:
roundClone.transform.Rotate(offsetX, 0.0, offsetZ);
I used x, and z here since you are adding velocity in the up direction.
'roundClone' is a Rigidbody, so roundClone.Rotate isn't being recognized as a member.
I should have caught that. I edited the comment. So is this what you are looking for, or do you need something else to solve your problem?
That doesn't seem to change anything. I was able to get the effect by rotating the object the transforms are pulled from so that the axis was ai$$anonymous$$g 45 degrees from center but I need to do it in code as I'll have a round shooting straight, and then one on either side at a 30-45 degree angle from it, basically a tri-shot effect as illustrated below. Let me know if that makes no sense. :)
The line of code I handed needs to be called before you assign the velocity. Your problem is made more difficult because it appears from your code that you are firing it in the object Transform.up direction. There is a lot of code samples that assume that you fire/move objects in the forward direction.
$$anonymous$$aybe I'm mistaken about what you want to rotate. You can use transform.Rotate() to rotate whatever object is spawning the 'round'. Since the instantiated round, get the transform rotation of the object spawning (because you are passing it as a parameter), then rotation the spawn object will rotate the 'round'
Here is an alternate way to delta rotate:
transform.rotation = Quaternion.Euler(rotx, roty, rotz) * transform.rotation;
But if the earlier code did not work, this is likely to have the same issues whatever it is.