Hi iam trying to build a game in which if i swipe on the device then the player should turn 90 degrees after entering the near by collider. i want the turn to happen only at the centre of the box collider. right now the player is turning when the trigger is entered.. please help..
you can get the center of the collider by this:
Vector3 center = collider.center;
and there is a suggestion to implement what you want:
Detect Collider.OnTriggerStay(), once this event is received, begin to record players input.
Detect Collider.OnTriggerExit(), once this event is received, stop recording players input, and if the player swiped the screen, turn your actor.
Reposition your trigger.
And below is some psuedo code:
bool shouldTurn = false;
void OnTriggerStay()
{
if (true//player swiped)
{
shouldTurn = true;
}
}
void OnTriggerExit()
{
if (shouldTurn)
//turn your actor
shouldTurn = false;
}
hope this helps.
Related
what I want to achieve is showing a sprite when the screen is touched by the player and make it disappear when a character reached the sprite's position, I made a pic to picture my words if I was not clear :
Thanks for your help
To add previous answer:
you should raycast from you touch position:
void Update()
{
Ray raycast = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit raycastHit;
if (Physics.Raycast(raycast, out raycastHit))
{
if (raycastHit.transform.GetComponent<BoxCollider2D>())
{
sprite.SetActive(false);
}
}
}
1) To show sprite use
if (Input.touchCount > 0)
{ gameObjectToShow.SetActive(true); }
2) To disapper it: add Collider to gameObjectToShow (2D Box or Circle), make it trigger, increase radius of that Collider and use OnTriggerEnter with gameObjectToShow.SetActive(false);
I'm creating a jetpack (mobile) controller where left joystick is used to control forward and backward movements and right joystick is used to control rotation and upward movements. What I want is player to go upwards whenever user is touching the right joystick even if horizontal && vertical axes both return zero. So if there is a finger on the right joystick player goes up similar to GetButton or GetKey(some keycode).
Hope this helps somebody in the future:
I found out there is OnPointerUp and OnPointerDown methods that can be used to check if joystick is pressed or not. Easiest way for me to use those were to change a few things in Standard Assets > Utility > Joystick.cs. This is how those methods look like after my modifications:
public void OnPointerUp(PointerEventData data)
{
transform.position = m_StartPos;
UpdateVirtualAxes(m_StartPos);
if (data.rawPointerPress.name == "MobileJoystick_right") {
rightJoystickPressed = false;
}
}
public void OnPointerDown(PointerEventData data) {
if (data.pointerEnter.name == "MobileJoystick_right") {
rightJoystickPressed = true;
}
}
So basically I just added the If-statements. Now I can access the rightJoystickPressed boolean from any other script to check if joystick is being pressed even if it is not moved.
I have just started unity. I have 4 Images(sprites) aligned in a grid. Now i want to move an image to a target position as soon as I touch the image. How can i do that?
I wrote the following code for move:
void Update () {
float step=speed*Time.deltaTime;
transform.position=Vector3.MoveTowards(transform.position,target.position,step);
}
I just don't know to move that particular sprite on which I touch.
Thanks
From http://answers.unity3d.com/questions/420808/how-to-get-position-of-touch-on-touch-screen.html
fingerPos = Input.GetTouch(0).position;
Vector3 pos = fingerPos;
pos.z = transform.position.z;
// simplified check
if (transform.position == Camera.main.ScreenToWorldPoint(pos))
{
// move towards the target as you want
}
Notice that i kept it brief with that if, but, of course, you should check if the touch position is withing the boundaries of your object.
I've got a player and I would like to throw an item what is "attached" to the player.
The problem is that both has rigidbody2D and Collider components. The item what I would like to throw is with the player and it has to collide with the ground and stuff. (except the player)
Here is what I tried:
if (Input.GetButtonDown ("Fire1") && canThrowCandle) {
Candle.rigidbody2D.isKinematic = false;
if (faceingRight)
Candle.rigidbody2D.AddForce(new Vector2(400f, 400f));
else if (!faceingRight)
Candle.rigidbody2D.AddForce(new Vector2(-400f, 400f));
Candle.collider2D.enabled = true;
canThrowCandle = false;
}
And then if the player collides with the item (Candle), it is with the player again:
void OnCollisionEnter2D(Collision2D coll){
if (coll.gameObject.name == "Candle") {
canThrowCandle = true;
Candle.rigidbody2D.isKinematic = true;
Candle.collider2D.enabled = false;
}
And the the code (CandleController) what controls the position of the ithem that I would like to throw:
void Update () {
if (GameObject.Find ("Player").GetComponent<PlayerController> ().canThrowCandle)
transform.position = new Vector3 (player.transform.position.x, player.transform.position.y, -0.01f);
}
So the question is, that how can I make this work?
If I understand correctly that you want the candle to go inside the player but both of them need to collide with the ground.
This can be done using collision layers. Just put ground, player and candle to different layers. Then adjust that both player and candle are colliding with ground, but not with each other.
If you want both trigger collider and physics collider to be attached to same gameObject, I think you need to use child gameObject for that as mentioned here.
I have a number of sprites on top of each-other on the game board. When i use the mouse and select the sprite on top, all sprites under the mouse position is selected. My question is how to only select the sprite that I click on and not catch the ones below?
Here is the code i am using for my tests, attached to the sprites:
function Update () {
if(Input.GetMouseButtonDown(0)) {
var theActualMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
// print("ALL MousePosition: " + theActualMousePosition);
if(collider2D.OverlapPoint(theActualMousePosition)) {
// print("HIT theActualMousePosition: " + theActualMousePosition);
print(collider2D.gameObject.tag);
}
}
}
The results you are getting are completely expected as your code is placed on the GameObjects, what you should do is to push your script out of those objects or use another function than OverlapPoint (because overlap point does not check for collision it simply checks if you are in the bounds of an object, which means it is valid for all object)
Some ideas :
Using OnMouseDown should provide you with an event only for the first collider encountered
Using A raycast from the camera : Camera.ScreenPointToRay should be able to be used for a raycast and then to check only the first collider encountered
Using Layers depending on layer collision Order.
EDIT :
Or you could also cast the ray the other way around :
function Update () {
if(Input.GetMouseButtonDown(0)) {
var theActualMousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 direction = (transform.Position-theActualMousePosition).normalized;
Ray ray = Ray(transform.Position,direction)
if(Physics.Raycast(ray) == null) {
//Then there is nothing between the object and the camera then the object is the first one
print(collider2D.gameObject.tag);
}
}
}