- Home /
Problem with the look direction after Instanciate/Rotate
Hi at all helper,
i have a big problem in rotate a GameObject.
The orginal gameobject is a Spider and it's position is 0,0,0 (world center). All the directions are correct:
Blue is the forward direction.
Now i instantiate the Spider and place it anywhere on a large Sphere with
...
if(Random.Range(1,3) == 1){
this.spawned_mob_list.Add((GameObject)Instantiate( Orginal_Spider));
}else{
this.spawned_mob_list.Add((GameObject)Instantiate( Original_Mobo));
}
this.GetNewMobPosition(this.spawned_mob_list[this.spawned_mob_list.Count-1].transform);
...
private Vector3 GetNewMobPosition(Transform t){
float angle = Random.Range(0,360);
//float dist = Random.Range(10,30);
float dist = Random.Range(2,3);
t.position = player.position;
//t.rotation = player.rotation;
t.RotateAround(t.position, t.up, angle);
t.position = t.position + t.forward.normalized * dist;
RaycastHit hit = new RaycastHit();
Vector3 tmp = (new Vector3(0,0,0) - t.position).normalized;
Vector3 dtmp = (new Vector3(0,0,0) + t.position);
if(Physics.Raycast(transform.position + dtmp, tmp, out hit)){
t.position = hit.point;
t.rotation = Quaternion.FromToRotation(t.up, hit.normal);
}
return t.position;
}
And i have a function for gravity on a Update() Function:
protected void DoGravity(){
if (this.gravitytype == GravityType.NORMAL){
Vector3 gVector = (new Vector3(0.001f,0.001f,0.001f) - transform.position).normalized;
transform.rotation = Quaternion.LookRotation(transform.forward, -gVector);
rigidbody.AddForce(gravity * gVector);
}
else{
int iDefLAyer = gameObject.layer;
gameObject.layer = 2;
RaycastHit hit = new RaycastHit();
Vector3 tmp = (new Vector3(0,0,0) - transform.position).normalized;
Vector3 dtmp = (new Vector3(0,0,0) + transform.position).normalized;
if(Physics.Raycast(transform.position + dtmp * 0.5f, tmp, out hit)){
transform.position = hit.point;
transform.rotation = Quaternion.LookRotation(transform.forward, hit.normal);
}else if(Physics.Raycast(transform.position + dtmp * 100, tmp, out hit)){
transform.position = hit.point;
transform.rotation = Quaternion.LookRotation(transform.forward, hit.normal);
}
gameObject.layer = iDefLAyer;
}
}
The rotation is already like the original GameObject and has no own rotation
How can i rotate the Spider correct to the hit.normal of the Raycast with it's own rotation?
Hmmm, not really sure what all is going on here, but I am wondering if your global and local directions got mixed up?
I compared the rotation of the real gameobject with the clone gameobject and the clone has always the direction of the real one.
How can i rotate the clone GameObject on the Sphere with its own direction... Why keep the Clone the Original direction?
Whats wrong?
if i rotate my Clone with
transform.parent.transform.Rotate(0f, doTurn, 0f);
It will rotate the GameObject with the mesh.. The rotation int the Inspector of the clone is chanced to. But the rotation with the rotate button in the scene doesnt chance... It work if i rotate it manually in scene...
Your answer
Follow this Question
Related Questions
Updating new cube face positions after rotation 0 Answers
why does Infinite Runner Terrain Collider break my game? 0 Answers
Instantiate object at position, moving in a direction? 1 Answer
How can I move an object to click point in 2D? 0 Answers
Instantiated object not rotating around the center 0 Answers