Jump to content

Unity Rotate changes Y transform??

iLostMyXbox21
Go to solution Solved by straight_stewie,
5 hours ago, iLostMyXbox21 said:

So i have a unity project and the script changes the movement on the Y axis, it should stay left and right, but now it goes diagonal..

Your current code has movement in local space. You need movement in global space.

 

transform.Translate(Vector3.right * speed * Time.deltaTime, Space.World); // this moves to the right
transform.Translate(Vector3.left * speed * Time.deltaTime, Space.World); // this moves to the left

 

What you are dealing with is the difference between local position and world position.

So i have a unity project and the script changes the movement on the Y axis, it should stay left and right, but now it goes diagonal..

Script: 

Spoiler

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

public class Move : MonoBehaviour
{
	public float speed = 3f;
	public float rspeed = 20f;
    void Update()
    {	
        if (Input.GetKey("d")) {
            transform.Translate(Vector3.right * speed * Time.deltaTime);
        }
		if (Input.GetKey("a")) {
			transform.Translate(-Vector3.right * speed * Time.deltaTime);
		}
		if (Input.GetKeyDown("q")) {
			transform.Rotate(0f, 0f, 25f);
		}
		if (Input.GetKeyDown("e")) {
			transform.Rotate(0f, 0f, -25f);
		}
    }
}

 

Video explaining my problem:

 

✧・゚: *✧・゚:*  Quote for a reply  *:・゚✧*:・゚✧

 

✧・゚: *✧・゚:*   Ask for discord   *:・゚✧*:・゚✧

Link to comment
Share on other sites

Link to post
Share on other sites

https://docs.unity3d.com/ScriptReference/Transform.Rotate.html

When dealing with anything Euler order is important as this goes back to Linear algebra (which I highly suggest you learn if you haven't already) as matrix multiplication is not Commutative. e.g. A * B != B * A.

 

You should look at using the physics engine for any movement as it will make your life easier as everything is handled by quaternion which are easier to use and less taxing on the CPU. 

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to comment
Share on other sites

Link to post
Share on other sites

35 minutes ago, trag1c said:

https://docs.unity3d.com/ScriptReference/Transform.Rotate.html

 

You're rotating around the Z axis which is aligned parallel to the view point frustum so that's why it rotates counter clock wise according to the view perspective.

 

Also when dealing with anything Euler order is important as this goes back to Linear algebra (which I highly suggest you learn if you haven't already) as matrix multiplication is not Commutative. e.g. A * B != B * A.

 

You should look at using the physics engine for any movement as it will make your life easier as everything is handled by quaternion which are easier to use and less taxing on the CPU. 

so i just removed that feature entirely, now i have an animation moving uo and down, but now my move script does not move left adn right, what did i mess up?

✧・゚: *✧・゚:*  Quote for a reply  *:・゚✧*:・゚✧

 

✧・゚: *✧・゚:*   Ask for discord   *:・゚✧*:・゚✧

Link to comment
Share on other sites

Link to post
Share on other sites

I would look at the Rigidbody functions. Specifically addTorque() and addForce() (If I remember the function calls correctly but a quick google should bring them up.) That should allow you to add a force to move the object in a linear direction (along the X and Y axis) with a rotational direction (torque is rotational force.) 

 

See my script I quickly came up with.

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



public class Testmovement : MonoBehaviour
{
    // Rigidbody2D for physics and collision behaviour.
    // Add a Rigidbody2D component to your game object for this script to work.
    // Also I set linear and angular drag to 1. I also turned off project gravity as it's not needed for this script as the rigidbody is  affected by gravity so without
    // an input force along the Y axis the object would simply fall off the camera into oblivion.
    private Rigidbody2D rb;
    // Public force and torque values for adjusting the speed of translation and rotation.
    public float torque = 15.0f;
    public float thrust = 15.0f;

    // Use this for initialization
    void Start()
    {
        rb = gameObject.GetComponent<Rigidbody2D>();
    }
    // Fixed update is called at a specific frequency so that the physics simulation doesn't go crazy since frame rates can send physics engine's into jank mode simulations.
    void FixedUpdate()
    {
        // Get each axis and calculate the force and torque to add to the object at each physics update
        // When an axis is assigned as a key press it ranges from -1 to 1 so it does not change the product of the calculation.
        // The torque and thrust values are scaled against time for even acceleration. Otherwise it would be an instant change and it would create an object that could
        // accelerate at an extreme and uncontrollable rate.

        // Axis can be assigned under Edit->Project Settings->Input
        // They can also be renamed and remapped to suite each project.
        float hMove = Input.GetAxis("Horizontal") * thrust * Time.deltaTime; // Left arrow right arrow or a and d
        float rot = Input.GetAxis("Vertical") * torque * Time.deltaTime; // Vertical has had the key inputs changed to q and e.
        rb.AddForce(new Vector2(hMove, 0.0f)); // Vector2 for the force Vector2(X Axis linear movement, Y Axis linear movement).

        rb.AddTorque(rot); // Add torque
    }
}

 

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to comment
Share on other sites

Link to post
Share on other sites

CPU: Intel i7 - 5820k @ 4.5GHz, Cooler: Corsair H80i, Motherboard: MSI X99S Gaming 7, RAM: Corsair Vengeance LPX 32GB DDR4 2666MHz CL16,

GPU: ASUS GTX 980 Strix, Case: Corsair 900D, PSU: Corsair AX860i 860W, Keyboard: Logitech G19, Mouse: Corsair M95, Storage: Intel 730 Series 480GB SSD, WD 1.5TB Black

Display: BenQ XL2730Z 2560x1440 144Hz

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, iLostMyXbox21 said:

So i have a unity project and the script changes the movement on the Y axis, it should stay left and right, but now it goes diagonal..

Your current code has movement in local space. You need movement in global space.

 

transform.Translate(Vector3.right * speed * Time.deltaTime, Space.World); // this moves to the right
transform.Translate(Vector3.left * speed * Time.deltaTime, Space.World); // this moves to the left

 

What you are dealing with is the difference between local position and world position.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×