Axe Spinning

I want to rotate an Axe from the script. I Added a transform rotate line for this and values in the z-axis updated as per code but no rotation visible on the screen. It moving in the right direction as per code but not rotating despite values are updating successfully in the z-axis.

[SerializeField] float translationSpeed = 1f;
float RotationSpeed = 180f;

// Update is called once per frame
void Update()
{
    transform.Translate(Vector2.right*translationSpeed* Time.deltaTime);
    transform.Rotate(Vector3.back * RotationSpeed * Time.deltaTime);

}

Hi Daniyal,

Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

Hey,

Yes ma’am I checked step by step through the break point debugging but not found any major issue. Axe rotating and take rounds in the loop and not translating.

What’s the value of translationSpeed? Check that with a Debug.Log during runtime.

You wrote that you had not found any major issue. Does that mean you found issues? If so, which ones?

Now I add Space.World as 4th parameter in transform.Translate and increase translationSpeed as 3f so it starts translating now but still rotating in loops . I want it to spin at fixed position in z axis but it take loops instead of just spinning

I’m not sure what you mean by loops but if you mean what I mean, the code might be rotating a parent game object, and the axe, as a child, rotates relative to the parent.

There is no parent of the Axe game object Nina it is a single object. And I want it to spin like someone throws hammer in real life than it will come at you while spinning like Rick did it through animation but instead it rotates in a circle like picture below .It is not spinning at fixed position it is take loops in circle on z axis

Am I clear this time?

There must be a pivot point around which the axe rotates. Where is it? To which game object does it belong? And is the axe also animated?

Could you share a screenshot of your Hierarchy so I can see where your axe is located?

Nina, there isn’t any parent object of Axe object. There is no animation on Axe I try to make it rotate and translate through the script.

Now I have made a parent object for the Axe and remove Time.deltaTime so it spins perfectly now as I wanted to .But now it translating in a weird way.

This is my current hierarchy

This is my current Code

float rotationSpeed = 60f;
float translationSpeed = 5f;

// Update is called once per frame
void Update()
{
    transform.Translate(Vector2.right *translationSpeed * Time.deltaTime);
    transform.Rotate(0,0,rotationSpeed);
}

Now there are 2 confusions

  1. As far as I know Time.deltaTime make it frame rate independent so not understand why Axe start rotating perfectly by removing Time.deltaTime and it will also not good for a game to remove it.

  2. It was translating correctly but after I handle rotation issue so now it is not translating properly.

The problem I figured out about translating is that after adding transform.rotation line my (Y position) also start updating and (X position) update in a limited way which I don’t understand why.I add Rotation why it is impact translation.

Your screenshot looks fine. Make sure that the “Body” does not have an offset to “Axe”. Otherwise, the “Body” with the sprite will rotate around the pivot point of the “Axe”.

Good question. Unfortunately, the API does not provide any information in this respect but Time.deltaTime is not used in the code examples. Generally, “not good for a game” depends on the result. A bad result is not good for a game.

That’s interesting. I’m wondering if the axe or its child has got a Collider2D and a Rigidbody2D component attached. If that’s the case, it might be that the y-position changes due to the physics simulation.

It would be great if you could share screenshots of both your Axe and the Body so I know exactly what you did and have.

I have only “Script” attached in my “Axe” object and “body” object only have “Sprite Renderer”.

Axe inspector

Body Inspector

Could you please upload your project to GitHub without the Temp and Library folder and share a link to the public repository here? I’d like to take a look into this.

Here is a tutorial video by Brackeys:

Sure Nina, Thanks for giving a tutorial.

Here is the link:

Thank you. I downloaded and tested your project in Unity 2020.1.5f1. The issue is also there but I figured out something interesting: Aparently, the axe moves along its forward axis.

image

After some testing, I figured out that Space.World fixes the issue. Space.Self is the default value in the Translate method. Unfortunately, I don’t know what exactly Space.Self means in the context of the axe. When setting the position to (0, 0, 0), the circle becomes smaller, so I assume that the origin has something to do with Space.Self. Anyway, Space.World works.

Here is the code that’s working for me. I set the translationSpeed value to 1000f.

using UnityEngine;

public class Axe : MonoBehaviour
{
    float rotationSpeed = 1000f;
    float translationSpeed = 5f;


    // Update is called once per frame
    void Update()
    {
        Vector3 speed = Vector3.right * translationSpeed * Time.deltaTime;
        transform.Translate(speed, Space.Self);

        transform.Rotate(Vector3.back * rotationSpeed * Time.deltaTime);
    }
}

Did it fix it for you, too?

Hi Nina,

Thank you very much for download and debug you are truly helpful :slight_smile:

There is some difference in what you said and what is in the code you posted.

  1. you said that you set translationSpeed =1000f but in the code there is rotationSpeed which is setted to 1000f.

  2. you said “Space.World” worked for you but you use “Space.Self” in the code

Anyways I applied Space.World and it worked finally :partying_face: I also tried Space.World several times before but it didn’t work at that time So I didn’t check another time after last update in which I remove Time.deltaTime. So If we break it down in a simple way than the real culprit was Time.deltaTime which is very very interesting.

Thank you Nina to bear with me this far :slight_smile:

This is the final workable code :slight_smile:

float rotationSpeed = 180f;
float translationSpeed = 5f;

void Update()
{
    transform.Translate(Vector2.right *translationSpeed *Time.deltaTime,Space.World);
    transform.Rotate(Vector3.forward*rotationSpeed);
}

That was a typo. The code I posted was working for me. Since I pasted it here, I probably meant to say that the translationSpeed was set to 1000f. :confused:

you said “Space.World” worked for you but you use “Space.Self” in the code

I’m realising that I shouldn’t do things in Visual Studio while simultaneously typing an answer in a forum. I tested something in that moment to figure out if there was a relationship between Space.Self and the origin (0, 0, 0).

I’m glad that you managed to fix the problem even though my last reply was slightly(?) confusing. :slight_smile:

You are like role model seriously.When I will feel good enough I will also give my services to community like you :slight_smile: . Now after applying RigidBody 2D “Time.deltaTime” makes no issue and works Fine. Interesting.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms