how can I move a Gameobject along its global axis
Hello, I'm attempting to create a snowboard game. I'm having issues getting the gameobject "center of mass" to move along it's global Z axis as it spins around on it's Y axis (with the snowboard).
as of now the "Center of Mass" is basically just a cube that i want to move towards the front (nose) of the board when pushing up on joystick, and move towards the back (tail) of the board when pushing down on joystick. when joystick input = zero the "center of mass" returns to the center of the board.
the method below is working, but it only moves the "com" back and forward on it's local axis. How can I get the "com" to move along it's global axis?
Any help is greatly appreciated!
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ExampleScript : MonoBehaviour {
public float rotationSpeed;
public float distanceToBinding;
public GameObject snowBoard;
public GameObject com; //"Center of Mass"
void FixedUpdate ()
{
Spin();
LeanForwardAndBack();
}
void Spin()
{
float horRot = Input.GetAxis("Horizontal") * rotationSpeed * Time.deltaTime ;
if(Input.GetAxis("Horizontal") != 0)
{
snowBoard.transform.Rotate(0, horRot, 0);
}
}
void LeanForwardAndBack()
{
//rotate with the board
com.transform.rotation = snowBoard.transform.rotation;
Vector3 pos = com.transform.position;
pos.x = snowBoard.transform.position.x;
pos.y = snowBoard.transform.position.y + 1;//move slightly upward
//SLIDE CoM FROM NOSE TO TAIL (RECENTERS WHEN NOT PUSHING JOYSTICK)
pos.z = snowBoard.transform.position.z + (-Input.GetAxis("Vertical") * distanceToBinding * Time.deltaTime);
//THIS MOVES THE "CENTER OF MASS" ALONG LOCAL AXIS. I NEED IT TO MOVE ALONG THE GLOBAL AXIS!
com.transform.position = pos;
}
}
Answer by MasterChameleonGames · Jan 28, 2019 at 08:37 PM
I'm not sure if you want to know how to move or rotate in global space, so I'll tell you both.
You would need to use transform.Rotate or transform.Translate which let you add another parameter after the Vector specifying which space it is relative to. To use the global axis, add Space.World.
So,
transform.Rotate(random vector, Space.World);
transform.Translate(random vector, Space.World);
Note: Translate adds to transform.position, it does not overwrite it.
Thank you for your reply! I tried transform.Translate and didn't get the effect I was going for. However, I did figure out a solution. I changed the line:
com.transform.position = pos; TO
com.transform.position = com.transform.TransformDirection(pos);
Ah, forgot to tell you that, my bad. I'm glad that you figured it out though!
Your answer
Follow this Question
Related Questions
Object stops lerping when target is far away 0 Answers
Confusion trying to set position at an angle 1 Answer
How can I move an object between two positions, while it is on a rotating platform? 0 Answers
Reversing movement directions 1 Answer
How could I make an object move from left to right and it will come back from right? 0 Answers