How does velocity works? - unity3d

hey guys i've something here to ask about how does really the velocity on unity works ??? i've been working on a project recently, i want to create the bouncing ball games, so whenever the ball hit the collider, it will be bounced depends on the position that have been hit.
i'm using the getComponent().velocity, but somehow the ball doesn't bounce really well, and whenever the ball hit the middle of collider, it should be bounced back without changing the direction .. please help !!! any help would be grateful ... here's my code :
float getBallPos(Vector2 ballPos, Vector2 boxPos, float boxWide ){
return (ballPos.x - boxPos.x)/boxWide ;
} ---> to get the bounce direction
void OnCollisionEnter2D (Collision2D other){
isHit = true;
if (other.gameObject.tag == "up") {
float x = getBallPos (transform.position, other.transform.position, other.collider.bounds.size.x);
Vector2 dir = new Vector2 (x, 1).normalized;
Debug.Log ("normalized : " + dir);
GetComponent<Rigidbody2D> ().velocity = dir * 5f;
}else if (other.gameObject.tag == "down") {
float x = getBallPos (transform.position, other.transform.position, other.collider.bounds.size.x);
Vector2 dir = new Vector2 (x, -1).normalized;
Debug.Log ("normalized : " + dir);
GetComponent<Rigidbody2D> ().velocity = dir * 5f;
}
}

You should create a Physics Material 2D with a high bounciness and low friction and apply it to your collider2D (you should see the values until you feel comfortable with it.
You create them in Assets->Create->Physics2D Material.
What you are trying yo do is simulate physics and it requires a somewhat complex mathematical model.

Related

Top Down Shooter Animations Unity, how do you get the right directional animations to play when moving?

i'm not really sure how to phrase the question sorry about that. I have encountered this hurdle while setting up animations on a character for learning a top down character controller (No rigid body) I've seen this question asked on Reddit and unity forums like a decade ago, but I don't really understand their answers.
My problem right now is that I can't find out a way to tell mecanim if the player is moving towards the direction they're facing. For example, if the player is moving left and aiming to the left, the moveForward animation should be played. If the player is moving left but aiming to the right, the moveBackwards animation should be played.
This is the first time i'm posting a question. I am sorry if my formatting is wrong.
Here is the code I have, I would greatly appreciate any help.
public class CharacterMovement : MonoBehaviour
{
private Vector3 velocity;
private Vector3 PlayerMoveInput;
Animator animationS;
[SerializeField] CharacterController characterrController;
[SerializeField] private float MoveSpeed;
[SerializeField] private float JumpHeight;
[SerializeField] private float Gravity = -9.81f;
// Start is called before the first frame update
void Start()
{
animationS = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
PlayerMoveInput = new Vector3(Input.GetAxis("Horizontal"), 0f, Input.GetAxis("Vertical")); //collects the input for the player
MovePlayer();
PlayerRotation();
}
private void MovePlayer()
{
if (characterrController.isGrounded)
{
velocity.y = -1f;
}
if (Input.GetKeyDown(KeyCode.Space) && characterrController.isGrounded)
{
velocity.y = JumpHeight;
}
///
else
{
velocity.y -= Gravity * -2f * Time.deltaTime;
}
Vector3 MoveVector = transform.TransformDirection(PlayerMoveInput);
characterrController.Move(MoveSpeed * Time.deltaTime * MoveVector);
characterrController.Move(velocity * Time.deltaTime);
float velocityX = Vector3.Dot(PlayerMoveInput, transform.forward);
float velocityZ = Vector3.Dot(PlayerMoveInput, transform.right);
animationS.SetFloat("velocityX", velocityZ, 0.1f, Time.deltaTime);
animationS.SetFloat("velocityZ", velocityX, 0.1f, Time.deltaTime);
}
void PlayerRotation()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Debug.DrawRay(ray.origin, ray.direction, Color.yellow);
if (Physics.Raycast(ray, out hit))
{
Vector3 targetPosition = new Vector3(hit.point.x, transform.position.y, hit.point.z);
Quaternion rotation = Quaternion.LookRotation(targetPosition - transform.position);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * 10.0f);
}
}
}
This isn't the exact implementation that would be used with your code but should give you a good start.
// get the angle between where the player is pointing, and where he is moving
float angle = Vector3.Angle(MoveVector, targetPosition - transform.position);
if (angle < 90f){
//move forward animation
} else {
//move back animation
}
I haven't tested this code but the idea is that if the angle between the direction the player is facing and the direction the player is moving is less than 90 degrees, then he is moving forward. Also the variables used here: MoveVector and targetPosition are private so you will need to fix that issue before this method can be implemented.
With mechanim, its very common to have 4 separate float value params in your animator. Two are used for movement info, two are for look direction info. This lets you use blend trees based on those functions that handle alot of the pain, all you need to do is update the values in the animator with the correct ones on each update/lateupdate depending on what you are doing.
This is how my animator params are layed out as well as the locomotion blend tree, I have another on a separate layer just for the head that uses Head left right, and head updown params to, you know, control the head.
And here is the code (or at least some of it) that sets the values on the animator component
EDIT:
Sorry, forgot to mention that the function i call "LerpFloat" on the anim variable is an extention method i defined myself for the animator component, all it does is gets the float value, lerps it, then sets the float value back. It just uses Mathf.Lerp.
Thank you both for your answers, the problem was the settings of the animation clips.....
Where you tick
Root transform Rotation
Root transform position (Y)
Root transform position (XZ)
I had ticked all of them, where just the root transform rotation was needed.
I will need to be more careful with the animator and animation clips in the future to avoid headaches.

How to get virtual joystick direction

i have a problem. I am making a game that can shoot some kind of spider man rope , in standalone version i'm using mouse to point the direction, something like this:
// Get Direction
public void setTarget(Vector2 targetPos)
{
Vector2 dir = targetPos - origin.position;
dir = dir.normalized;
velocity = dir * speed;
transform.position = origin.position + dir;
pull = false;
updateLine = true;
}
// Set Direction On Pc Version
if (Input.GetMouseButtonDown(1))
{
Vector2 worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
rope.setTarget(worldPos); // another script will get this value
}
And Heres the problem :
// Set Direction On Mobile (The Problem)
if (Input.GetMouseButtonDown(0))
{
Vector2 direction = new Vector2(joystick.Horizontal, joystick.Vertical);
rope.setTarget(direction);
}
and it's working fine, but now i want to make the mobile version using virtual joystick that i downloaded from the assets store, i want to get the virtual joystick angle so that the rope can shoot to that angle direciton. Please help me , sorry for my bad english, thanks
I suppose it's one of two things:
You are using your "direction" as target and it looks like rope.transform.position + direction is your target position.
rope.setTarget(rope.transform.position + direction);
You are not considering camera angle. You should rotate your distance by Camera rotation y
rope.setTarget(rope.transform.position + Quaternion.AngleAxis( Camera.main.transform.rotation.eulerAngles.y, Vector3.up ) * direction);

How to make a player only move in 1 direction (Unity)

I am trying to create a game in Unity where the player can only move in the direction it is facing, but the following code allows the player to move in all 4 directions.
(This is for a 3D project)
Any help would be appreciated! Thanks!
public class PlayerController : MonoBehaviour {
public float speed;
private Rigidbody rb;
void Start() {
rb = GetComponent<Rigidbody>();
}
void FixedUpdate() {
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
}
So I didn't get what you wanted: first you said you wanted it to move only forward, then the only thing you have to do is get when he pressed a key and move forward while is not unpressed.
If you wanted to say that it can move all directions BUT only one at a time, the you will have to put the same code but with some changes:
First of all to make it move forward you have to get the forward of the transform, otherwise it will move in the same direction if you rotate it (you don't want that, no?).
Vector3 moveDirection = (transform.forward * Input.GetAxis("Vertical") + transform.right * Input.GetAxis("Horizontal")).normalized;
moveDirection.y = 0;
rb.velocity = moveDirection;
Then, to make that it ONLY moves to one direction at a time, you have to put the priority of the greatest axis number and if it's equal then you should think if you want to move forward or right (with it's axis value).
From the code you posted, I'm not sure where you are storing the player's facing-direction. However, I presume that it is stored as a Quaternion. If you have a player rotation quaternion called playerRotation, then you could do this (warning - untested):
Vector3 input = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
Vector3 normal = playerRotation * Vector3.forward;
Vector3 movement = Vector3.Dot(normal, input) * input;
If the game is first-person, then you can take a shortcut and just use Camera.current.transform.forward instead of the normal vector.
This will project the input direction onto the normal with the player's facing direction so that your movement force can only be in that direction.

Unity3D RayCasting in 4 directions only

I am trying to create a maze game where the player can move only when it can see a sweet.
Currently the raycast is searching in every direction (360 degrees) But I only want the raycast to look directly Up,Left,Down,Right (instead of 360 degrees)... This way the player can only move when a sweet is placed in direct line of sight.
public function setTargetSweet(target:GameObject)
{
too = target.transform.position;
targetSweet = target;
var fwd = too - transform.position;
Debug.Log("Setting target sweet: " + target.name);
Debug.DrawRay(transform.position, fwd, Color.red, 5f);
Physics.Raycast (transform.position, fwd, hit);
if ( hit.collider.tag == "MoveToSweet") {
print ("Can See Sweet");
gotoSweet = true;
}
else
{
Debug.Log(hit.collider.name);
Debug.Log(hit.collider.tag);
gotoSweet = false;
}
}
You're doing ray casts in the direction of any sweet every time. You want four calls like Physics.Raycast(transform.position, Vector3.forward, hit) and check the hit between calls.

Avoid Pushing between rigidbody

I have a issue with my 2D game in unity (pokemon style), I'm using transform.position to move the gameobjects.
I have a player and enemies that follow him, all is ok. But when the enemies make a collision, they begin to push each other
I need that nobody to be pushed when the enemies and player get a collision.
I tried to use kinematic in enemies, but the player can push them.
I tried to add a big amount of mass to the player, but he can push the enemies.
I tried to detect the collision in code with OnCollision, but when I cancel the enemy movement, they don't return to move.
----UPDATE----
I need the collision but without pushing between them, here is a video to illustrate the problem
https://www.youtube.com/watch?v=VkgnV1NOxlw
Just for the record, i'm using A* pathfinding script (http://arongranberg.com/astar/) here my enemies move script.
void FixedUpdate () {
if(path == null)
return;
if(currentWayPoint >= path.vectorPath.Count)
return;
Vector3 wayPoint = path.vectorPath [currentWayPoint];
wayPoint.z = transform.position.z;
transform.position = Vector3.MoveTowards (transform.position, wayPoint, Time.deltaTime * speed);
float distance = Vector3.Distance (transform.position, wayPoint);
if(distance == 0){
currentWayPoint++;
}
}
----UPDATE----
Finally I'll get the expected result,changing the rigidbody2D.isKinematic property to true when the target was close and stop it
Here is a video https://www.youtube.com/watch?v=0Zm0idUU75s
And the enemy movement code
void FixedUpdate () {
if(path == null)
return;
if(currentWayPoint >= path.vectorPath.Count)
return;
float distanceTarget = Vector3.Distance (transform.position, target.position);
if (distanceTarget <= 1.5f) {
rigidbody2D.isKinematic = true;
return;
}else{
rigidbody2D.isKinematic = false;
}
Vector3 wayPoint = path.vectorPath [currentWayPoint];
wayPoint.z = transform.position.z;
transform.position = Vector3.MoveTowards (transform.position, wayPoint, Time.deltaTime * speed);
float distance = Vector3.Distance (transform.position, wayPoint);
if(distance == 0){
currentWayPoint++;
}
}
You can do this in several ways,
You can use Physics2D.IgnoreCollision
Physics2D.IgnoreCollision(someGameObject.collider2D, collider2D);
Make sure that you do the IgnoreCollision call before the collision occurs, maybe when objects instantiate.
or alternatively you can use, Layer Collision Matrix
Unity Manual provides information on using this. This simply does the collision avoidance by assigning different GameObjects to different layers. Try:
Edit->Project Settings->Physics
Or if you want it to just stop moving, You can easily do it like,
bool isCollided = false;
// when when OnCollisionEnter() is called stop moving.
//maybe write your move script like
void Move() {
if(!isCollided) {
// move logic
}
}