I'm trying to make a game using unity and at this point I have created a cube object, a ground object a couple other things. I can make it move by giving it a forward force. I'm basically trying to make a game about a cube that you can navigate through obstacles.
What I want to know is how to get the input like touch input. For example you would use getkey("d") If you wanted to get input from "d" in the keyboard. How would I do the same except for touch input in android?
Use this
Input.GetMouseButtonDown(0)
Example
if(Input.GetMouseButtonDown(0)){
circle.velocity = Vector2.up * jumpForce;
}
https://docs.unity3d.com/ScriptReference/Input.GetTouch.html
You can use this to get a touch object which will give you the screen position, then you can use your cameras position and angle to cast a ray from that point to see if the user is touching the cube.
Related
Please is it possible to make a Player with rigidbody or character controller run on wall just like The Flash. Please guide me on how to implement this.Here's a link of what I'm talking about. Thank you in advance.
Rotate the player to be vertical to the building, adjust the gravity parameter on your rigid body (make it lower so the added force won't be canceled by it) and just add the force (or set the velocity, whatever makes you happy)
I'm going to assume you already know how to move the player around on the ground. All you need to do is create some form of detection for when they want to begin running on the wall (maybe the player presses a button, or walks up to the wall), at which point you will want to do 2 things: rotate the player to have their feet on the wall, and change gravity to be pulling the player into the wall.
If we assume the player is facing the wall when they go up on it, you can just rotate 90 degrees backwards. Changing gravity should be easy too. Once the player is fully rotated, do something like this
Vector3 newGravityVector = tranform.down.normalized
Physics.gravity = transform.down.normalized * 9.8 //9.8 is the default gravitaty value, feel free to use a different multiplier if you wish.
(this script should be attached to your player)
So I have an enemy AI that spawns from various points and follows a way point system. What I am trying to do is capture the movement direction into a variable and say basically if currPos.x or .y is +1 or -1 then apply it to a float in my blend tree.
https://pastebin.pl/view/6ca3d428
I tried originally without a blend tree, using bools but I still cant figure out how to capture into a variable which way something is going. I was able to capture a transform.position.x but that was just monitoring its actual position in xyz on the screen and not a velocity of foward or backwards.
I am trying to get a player pull and push objects in my 3D game. I am mainly focusing on pulling and pushing a box. Now when a player collides with a box and the E key is pressed the isKinematic of the box becomes false, and the player is able to move the box with its own mass.
I am trying to implement a basic pull and push interaction of the object. I have been thinking moving the box along with the movement of the player based on the WASD input but it doesn't sound stable, which road should I take here? Should I use a Fixed Joint connect them? I tried putting the fixed joint the box and making the connected body the player, but now the player is not able to move like there is a collision around it.
What is the best way to implement this? Should I use Fixed Joint or take a different way? Thanks.
if you really want to work with physics, i would say use a rigidbody. Use the rigidbody.AddForce or rigidbody.velocity to move your player. or use the transform by lerping to the position, but this is a bit messy and could not register all of the collisions if you are working on a fast object.
I am looking at developing a game within Unity by using C#
The game will take the gyroscope orientation and rotate accordingly to the direction of which the phone is rotated.
I am curious of how this would work, I understand how I would be able to read and update the gyroscope orientation however I am unsure on how to assign this to a world to rotate. There will be a player on world which will be the next challenge to prevent the player clipping through the world when it is rotated.
Hence the world should rotate around the players current location.
I currently have no code as I am in the process of designing this, however i am unable to get the logic of how to make this work within my head
Thankyou
If I get your objective right, I guess rotating the camera like in a Fist-Person game should be enough, and would be way simpler than rotating the whole world while keeping the player static.
This is how I would go about it, first I wouldn't rotate the world, as that is alot of objects to rotate, but if you really wanted to you could parent all of the world to a single game object, then rotate that object about an axis based off of the players position(See this: https://docs.unity3d.com/ScriptReference/Transform.RotateAround.html).
The simplest way is to attach your main camera to your player(Drag and drop it on your Player Object in your hierchy), then rotate your player.
for an example you can test on your machine without the gyroscrope:
using UnityEngine;
public class rotatorScript: MonoBehaviour {
void Update() {
// Will Rotate based off of left/right arrows
float rotator = -Input.GetAxisRaw("Horizontal");
// Move up or down based off of up/down Arrows
float verticalDirection = Input.GetAxisRaw("Vertical");
// Do the actual movement... using space.self so it is based on the player not the world.
transform.Translate(Vector3.up * verticalDirection * Time.deltaTime * 5f, Space.self);
transform.Rotate(0f,0f,90f * Time.deltaTime * rotator);
}
}
This script would be attached to the player.
I'm using a character controller, which moves the character along a set path using ray casting:
This works fine for moving by x and z axis, and I'm also able to land on objects if I hit them directly from top. But if I enter an object with a collider from the side, either pass through or get stuck:
I assume this happens because the player is set to position itself along the raycast, and to avoid the player to teleport up on the platforms, I need the raycast to ignore them. Now this causes the new collision problem. Does anyone have any idea on how to work around this, so my player properly is colliding with the platforms? I'm using all physics inside a FixedUpdate(), and the path is created using the Tween plug-in, but I assume this issue would appear with any script using raycast in this manner.
Add specific mask to those platforms, and in code where you calling ray cast, just add new variable for mask and there just don't select the platform mask.