Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Evil_Weevil · Jan 30, 2014 at 02:31 PM · positionlocalgamepad

Getting back to the set position

I have a simple script that let`s my object (sphere) copy the moves of the stick of my Xbox 360 gamepad. It just emulates the stick: if I tilt it a little, my sphere moves a bit; if I release it, it goes back to origin. That`s perfect, that`s exactly what I need.

 using UnityEngine;
 using System.Collections;
 
 public class Move : MonoBehaviour {
     public Transform target1;
 
 
     // Use this for initialization
     void Start () {
     
     
     }
     
     // Update is called once per frame
     void FixedUpdate () {
                 
 
                 transform.LookAt (target1);
 
 
                 transform.position = new Vector3 (Input.GetAxis ("Horizontar"), transform.position.y, transform.position.z);
                 transform.position = new Vector3 (transform.position.x, transform.position.y, - Input.GetAxis ("Verticar"));
 
         }
 
         }

Now, what I want my sphere is to make the same moves, but LOCALLY, not necessarily returning to origin. I don`t know how to do that. With the script I showed you the sphere is always set at origin, when I start the game — no matter where I placed it in the editor.

Any help is appreciated. Thanks.

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
0

Answer by KellyThomas · Jan 30, 2014 at 02:32 PM

Firstly understand that the code lines 21-22 in you code above can also be represented as:

 Vector 3 input = new Vector3 (Input.GetAxis ("Horizontar"), transform.position.y, - Input.GetAxis ("Verticar"));
 transform.position = input;
 

This results in a position that varies from (-1,0,-1) to (1,0,1) depending on input.

If you were instead to use this:

 float scale = 0.1f;
 Vector 3 input = new Vector3 (Input.GetAxis ("Horizontar"), transform.position.y, - Input.GetAxis ("Verticar"));
 transform.Translate(input * scale);

It's movement would be cumulative over each frame.

Play with the scale value to control the speed.

Comment
Add comment · Show 6 · 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 Evil_Weevil · Jan 30, 2014 at 02:42 PM 0
Share

First of all, thanks for answering. Secondly, none of this worked for me. Actually when I implemented the code, my poor sphere just reached sky on y (5.000 and grossing).

Also, I don`t understand the very principle. With my parameters the results vary from (-1,0,-1) to (1,0,1), true. With your parameters I think they are to vary from (-0.1, 0, 0.1) to (0.1, 0, 0.1).

avatar image KellyThomas · Jan 30, 2014 at 03:01 PM 0
Share

Hmm.. they were moving on the y axis? Oh I see. Sorry a copy-paste error on my part:

 float scale = 0.1f;
 Vector 3 input = new Vector3 (Input.GetAxis ("Horizontar"), 0.0f, - Input.GetAxis ("Verticar"));
 transform.Translate(input * scale);

This idea is to apply an change in position relative to the current position. Translate helps us achieve this. The scale is just so that we can control how fast they should move, I suggest starting with small values as it will be applied in a cumulative manner on each frame.

avatar image Evil_Weevil · Jan 30, 2014 at 03:33 PM 0
Share

Oh, I see! Sorry, but that`s NOT what I`m looking for. As I said, I`m looking for emulating the Xbox 360 stick. If I use simple transfom.Translate I get the normal, the ordinary moving object. So, when I tilt the stick it moves (ok), when I release the stick it stops (not okay). What I want is to when I release the stick the sphere should not just stop but also get back to the position from which its movement has started. It works perfectly when the movement starts from 0,0,0. It just doesn`t work otherwise — that`s the problem.

avatar image KellyThomas · Jan 30, 2014 at 06:21 PM 0
Share

O$$anonymous$$ I think we are on the same page now, I was thinking by "LOCALLY, not necessarily returning to origin" you wanted it to travel, now I understand you want behaviour similar to your current behaviour but centred around an arbitrary point.

This is what's required:

 Vector3 origin = new Vector3(1,2,3); //for whatever values you want
 Vector 3 input = new Vector3 (Input.GetAxis ("Horizontar"), 0.0f, - Input.GetAxis ("Verticar"));
 transform.position = origin + input;

This would take the position at the start of game and treat that as the origin:

 using UnityEngine;
 using System.Collections;
  
 public class $$anonymous$$ove : $$anonymous$$onoBehaviour {
     public Transform target1;
     private Vector3 origin;
 
     void Start () {
         origin = transform.position;
     }
  
     void FixedUpdate () {
         transform.LookAt (target1);
         Vector 3 input = new Vector3 (Input.GetAxis ("Horizontar"), 0.0f, - Input.GetAxis ("Verticar"));
         transform.position = origin + input;
    }
 }


Is your camera moving? Is this intended to show the player how to execute combos or something? If it needs to remain stationary relative to a moving camera then we would need to refine thing more.

avatar image AlucardJay · Jan 31, 2014 at 03:31 AM 0
Share

This question is very closely related to your others. If you don't consider this a duplicate question, then you should provide reference links to those other questions :

  • http://answers.unity3d.com/questions/625713/emulating-xbox-360-controller-stick.html

  • http://answers.unity3d.com/questions/624435/the-release-of-an-xbox-360-controller-stick.html

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

20 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

Related Questions

Problem with transform.position 1 Answer

Why does the object alignment only work when the object isn't rotated? 2 Answers

GameObject's local position without parent 1 Answer

Change position relative to object 3 Answers

Camera shake 11 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