- Home /
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.
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.
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).
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.
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.
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.
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 :
Your answer

Follow this Question
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