- Home /
Character does not rotate to his original position
Hi guys, I have the following issue in my game;
When I shoot at my opponent I get his rotantion in angle Y with the raycast hit, therefore I rotate my character in 90 degrees so I can shoot at him in a straight line as you can see in the code:
if (Physics.Raycast(ray, out hit))
{
if (hit.transform.tag == "Espantalho" || hit.transform.tag == "Xama" || hit.transform.tag == "Sheriff")
{
//instantiates the bullet
GameObject tiro = Instantiate(projetil, SpawBala.transform.position, SpawBala.transform.rotation);
Rigidbody BalaRigid = tiro.GetComponent<Rigidbody>();
tiro.transform.LookAt(hit.transform.position);
float AngleY = hit.collider.gameObject.transform.rotation.eulerAngles.y;
if (AngleY == 180)
{
player.transform.Rotate(0f, 90f, 0f);
Girou = true;
}
BalaRigid.velocity = SpawBala.transform.forward * ProjetilVeloc * Time.deltaTime;
gameManager.PontoAcaop1--;
if (Girou)
{
player.transform.Rotate(0f, 0f, 0f);
}
Personagens[i].GetComponent<Jogador>().PodeAtacar = true;
interfaceManager.ActionPointDown();
}
}
However this condition isn't working, because my character doesn't rotate back to his original position. What have I done wrong guys?
if (Girou)
{
player.transform.Rotate(0f, 0f, 0f);
}
Answer by Anis1808 · Dec 05, 2019 at 12:44 AM
Are you sure your variable Girou is true first?
The first thing wrong I see with this line is that you are not using transform.Rotate() the right way. The documentation might help you. In brief, your error is that you are not rotating the object with this line, you are telling it to rotate by 0 degrees so assuming the rest of your code is correct, this is your main error. The values inside the Rotate function must be the angle BY which you want to rotate not the angle TO which you want to rotate. You can calculate that angle by subtracting the rotation by the initial position.
You were totally right man
Now I solved it by doing this coroutine:
if (Girou)
{
yield return new WaitForSeconds(3f);
Girou = false;
player.transform.Rotate(0f, -90f, 0f);
}
However, how I could know if the opponent is on my left or right? Because, if he's on my left, I have to rotate to -90 degrees.
You could get his position and compare its z or x component with the one of the player by subtracting one from the other and check if the value is positive, it is on one side, if it is negative, it is on the other side. It depends on how your game is set up in the editor.
Yes, I've done it and it's working:
if (!Girou && hit.collider.gameObject.transform.position.x > player.transform.position.x)// if the oppenent's on the right
{
player.transform.Rotate(0f, 90f, 0f);
Girou = true;
}
if (hit.collider.gameObject.transform.position.x < player.transform.position.x && !Girou)// if the oppenet's on the left
{
player.transform.Rotate(0f, -90f, 0f);
Girou = true;
}
Now, I have to check if the opponent's in the front or behind me. I think I'm gonna use the vector3.distance for this one.
Thank you man, and Thank you for your help!
Your answer
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Particle System Instantiate's With Original Rotation - C# 1 Answer