#I can't rotate an object correctly on the X axis.

1 messages · Page 1 of 1 (latest)

gentle stump
#

I know some of you already are prepared to give me the typical response of "just google it" I'm stuck on this phase for about an hour now, I did rotate things in the past.

But I literally can't understand why when I rotate the barrel with code, it will rotate on the y and z axis as well, I want to lock those 2 axis to 0 and -90.

Here is a video of the problem I have:

https://youtu.be/3-PE9OSvxTo

Here is the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ExplosiveBarrel : MonoBehaviour
{
    public float barrelSpeed = 2f;
    public float rotationSpeed = 10f;
    void Update()
    {
        // Making the Barrel move on the road
        Vector3 newPos = new Vector3(0, 0, -1f * barrelSpeed * Time.deltaTime);

        transform.position += newPos;

        // Spinning the Barrel on the X axis

        transform.Rotate(1f * rotationSpeed * Time.deltaTime, 0f, 0f);
        
    }
}

Please explain what did I do wrong, I plan to do small projects to expand my knowledge but I'm stuck here for some reason.

Thank you in advance!

#

I want the barrel to go down the road, I've succesfully did that, but when I try to rotate, the entire barrel just goes in random directions.

weary wind
#

My hunch is that the physics engine is interfering with the rotation. You could try expanding the Constraints section on the rigidbody and freezing all axes of rotation. That way only your code can rotate it

pearl juniper
#

Does the object have a rigidbody?

weary wind
#

The video shows one ye

gentle stump
gentle stump
#

Let me see if I can fix it!

pearl juniper
#

@gentle stump do not use a rigidbody and directly modify the transform

#

one or the other

gentle stump
gentle stump
#

I can't understand at all what's wrong...It doesn't even make sense for me, I'm moving the object in the X axis but now it's moving on the y?

This is extremely weird and confusing.

#

Kind of made an fix @pearl juniper .

I just rotate the object using animations, at least temporary until I figure out what's happening with the code.

pearl juniper
#

Remove animation component also and just have the barrel move with code

#

Wait don't you want your barrel to be affected by physics?

gentle stump
pearl juniper
#

So keep the rigidbody, and use a physics based method to rotate the barrel

gentle stump
#

Once again, sorry that I keep asking stuff, I'm just extremely curious and at the beginning 🙂

pearl juniper
#

Don't be sorry this is literally the exact place to ask questions

#

Most used physics methods are addforce and addtorque you can check those out

gentle stump
# pearl juniper Don't be sorry this is literally the exact place to ask questions

From what I remember 2-3 years ago, the best physics movement was something with addforce, but was using something called "Vector" I can't remember the full phrase though.

But I will start researching how to move things using physics.
Because, when I move my car, it instantly reaches full speed, once I stop pressing the specific key, the speed is going straigth to 0 in a milisecond.