Merge a rotation(vector3) to a lookAt(quaternion) ?


I’m trying to resolve an issue in a game prototype but so far all my attempts are useless :confused: I’ll try to explain the problem with my poor english.

What I want to do :
I have an object moving in space. In parallel, a script instantiate 1 out of 8 “different” objects ; the only difference between each of them are rotations (x/y/z), because each object have a starting rotation which is predefined. So : when my object is moving backward in space, it “drops” randomly up to 8 objects, the same prefab but with different rotations informations. The idea is to create a “tunnel”.

What’s the problem :
Now I want to let my object (which is instantiating objects) move in every directions (up, down, right, left…). Problem is that the predefined rotation informations can’t follow my moving object anymore. So I’m trying to merge this vector3 with the command LookAt… but it’s a mess. The goal would be to use LookAt first when the object is instantiate, and then use the predefined vector3 to rotate it.


What I’ve tried :
Since I’m using Vector3 as predefined rotation, and that LookAt returns a quaternion, I’ve tried to merge these 2 things together … with weird results, for example :

  • (vector3.x + Quaternion.eulerAngle.x)/2
  • vector3.x*Quaternion.eulerAngle.x
  • and so on…

Hey bud, you might be able to replace those with

//make a Quaternion in your variable list
private Quaternion desiredRotation = new Quaternion();

//then where you need to rotate instead of using the LookAt function

// Calculate and set rotation
desiredRotation = Quaternion.LookRotation(this.yourTargetGameObject.position - this.transform.position, this.yourTargetGameObject.up);

//cache it back to your transform
this.transform.rotation = desiredRotation;

Im not sure though, it may be best to see your controlling code as this may not suffice and your problem might be down to optimization for execution at run-time n all that as LookAt() can be an expensive method to run, so i understand. (happy to be proved otherwise here)

Take care bud and hope it helps some way.