Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ScottUnityNoob · Jan 28, 2019 at 07:18 PM · movementtransform.positionworldlocalspaceglobalspace

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;
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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);

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MasterChameleonGames · Jan 28, 2019 at 08:38 PM 0
Share

Note: Translate adds to transform.position, it does not overwrite it.

avatar image ScottUnityNoob MasterChameleonGames · Jan 28, 2019 at 10:27 PM 0
Share

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);

avatar image MasterChameleonGames ScottUnityNoob · Jan 29, 2019 at 12:49 AM 0
Share

Ah, forgot to tell you that, my bad. I'm glad that you figured it out though!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

220 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Moving walls in multiple places 1 Answer

Objects follow player and mimics player movement (C#) 0 Answers

Move 3D object on transform.localposition 1 Answer

Rigidbody movement: Changing the Y of transform.position makes my character stutter 0 Answers

How to make movement smooth? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges