Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
iLostMyXbox21

Unity Rotate changes Y transform??

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.

Recommended Posts

Posted · Original PosterOP

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:

 


Discord: iLostMyXbox21#4649

I took a walk around the world to Ease my troubled mind I left my body lying somewhere In the sands of time I watched the world float to the dark Side of the moon I feel there is nothing I can do, yeah I watched the world float to the Dark side of the moon After all I knew it had to be something To do with you I really don't mind what happens now and then As long as you'll be my friend at the end If I go crazy then will you still Call me Superman If I'm alive and well, will you be There holding my hand I'll keep you by my side with My superhuman might Kryptonite You called me strong, you called me weak But still your secrets I will keep You took for granted all the times Never let you down You stumbled in and bumped your head, if Not for me then you'd be dead I picked you up and put you back On solid ground If I go crazy then will you still Call me Superman If I'm alive and well, will you be There holding my hand I'll keep you by my side with My superhuman might Kryptonite Oh whoa whoa If I go crazy then will you still Call me Superman If I'm alive and well, will you be There holding my hand I'll keep you by my side with My superhuman might Kryptonite If I go crazy then will you still Call me Superman If I'm alive and well, will you be There holding my hand I'll keep you by my side with My superhuman might Kryptonite

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 post
Share on other sites
Posted · Original PosterOP
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?


Discord: iLostMyXbox21#4649

I took a walk around the world to Ease my troubled mind I left my body lying somewhere In the sands of time I watched the world float to the dark Side of the moon I feel there is nothing I can do, yeah I watched the world float to the Dark side of the moon After all I knew it had to be something To do with you I really don't mind what happens now and then As long as you'll be my friend at the end If I go crazy then will you still Call me Superman If I'm alive and well, will you be There holding my hand I'll keep you by my side with My superhuman might Kryptonite You called me strong, you called me weak But still your secrets I will keep You took for granted all the times Never let you down You stumbled in and bumped your head, if Not for me then you'd be dead I picked you up and put you back On solid ground If I go crazy then will you still Call me Superman If I'm alive and well, will you be There holding my hand I'll keep you by my side with My superhuman might Kryptonite Oh whoa whoa If I go crazy then will you still Call me Superman If I'm alive and well, will you be There holding my hand I'll keep you by my side with My superhuman might Kryptonite If I go crazy then will you still Call me Superman If I'm alive and well, will you be There holding my hand I'll keep you by my side with My superhuman might Kryptonite

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 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 post
Share on other sites
Posted · Best Answer
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.


Teacher: Does anyone have a thin ruler?

Students: No.

Teacher: Ok. Does anyone have a nail file.

Students: Why do you need a nail file?
Teacher: I have a staple I need to remove.

Student: Oh. I have a staple remover...

 

People don't want power drills, they want holes.

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


×