Vector 3 moving to certain place?
So I have this code :
if (Input.GetKey(KeyCode.I))
{
transform.position = new Vector3(0, 10, 0);
Debug.Log("Executed");
//this isn't what I want, this is only to :
// help other people and do what I originally want, something very similar.
}
And I would like it to instead of going to 0,10,0
I want it to go up 10 than it's original position.
Atleast know what to do in order
EDIT : Please note that this is gonna be a moving object
Answer by Runalotski · Nov 27, 2016 at 03:38 PM
To set the position relative to its current position use
transform.Translate(new Vector3(0,10,0));
Note: This will move along the local up vector of the object and not along the world up vector. This might be relevant when the object is rotated.
Anyway to make it return to a spot ?
Because what I'm doing is trying to get it into a kicking way. So when it gets on collision or something, It returns back under the leg.
to make it return you will run code with the following built in function
Void OnCollisionEnter(Collision col)
{
if(col.transform.tag == "Enemy")
{
Debug.Log(transform.name + " hit an Enemy");
transform.position = resetPosition;
}
}
Now I don't know what the original position is that you need to set and you also need a rigid body on that object to have this function call
Everything works after trying to debug most of it (I'm using Visual Studios)
So I got :
private void OnCollisionEnter(Collision collision)
{
if (collision.transform..tag = "Enemy")
{
Debug.Log(transform.name + " hit an Enemy");
transform.position = resetPosition;
}
}
And I don't know what resetPosition is?
@Runalotski but It says 'The name resetPosition does not exist in the current context'
That is correct you have to replace it with something that makes sense to your project.
That pretty much works to my standards,
I used : private void OnCollisionEnter(Collision collision) { if (collision.transform.tag == "Enemy") { Debug.Log(transform.name + " hit an Enemy"); Vector3 resetPosition = default(Vector3); transform.position = resetPosition; } }
Thanks for answering @Runalotski.
Answer by OneEyeMaker · Nov 27, 2016 at 03:34 PM
Maybe, it helps you:
transform.position += new Vector3(0.0F, 10.0F, 0.0F);
Also, if your GameObject has Rigidbody component, you can do this:
Rigidbody rigidbodyComponent = GetComponent<Rigidbody>();
rigidbodyComponent.AddForce(new Vector3(0.0F, 10.0F, 0.0F));
Read more about Rigidbody in documentation.
Okay, This works, but Is there anyway to have it so it's like a fighting{ish} style,
So after it connects with something (maybe a oncollision enter), it resets?
Sorry I am only like one month(ish) new to Unity
I can't say, what you should do, because I don't know, what you want to do. In one case you need specific Physical material with bounciness, in another - you need apply gravity on Rigidbody component. Or, maybe, you need special OnCollision...() method. It depends on what you want to do. In any case - RTF$$anonymous$$ :)
I just want it so : It goes up while rotating like a normal kick,
Then when it connects with something with the tag of "Enemy" or something, it resets its position.