Velocity in Unity 2D How to get velocity of object without rigidbody - c#

By using method transform.translate(Vector3.left * 5f * Time.Deltatime; does this change the velocity of a gameobject with a rigidbody ,because in my case it doesnt work.Is there a way i can move an object so the velocity changes .If not is there any way to measure velocity of an object without having a Rigidbody attached to it. Thanks.

You can measure the velocity vector like this:
Vector3 pos, velocity;
void Awake()
{
pos = transform.position;
}
void Update()
{
velocity = (transform.position - pos) / Time.deltaTime;
pos = transform.position;
}

You can use Transform.hasChanged
if (!this.transform.hasChanged)
{
print("Player is not moving");
}
transform.hasChanged = false;

Related

Multiple touch unity mobile

I am creating a 2d mobile game where one of the scripts uses a joystick to move and the other script lets the player shoot an object when tapping anywhere on the screen. The issue is when using the joystick it also shoots at the same time in that direction. Is there a way to separate the touches so when you use the joystick it does not immediately shoot to that direction but the player can still move and shoot anywhere at the same time?
Move Code
private void Update()
{
Vector2 moveInput = new Vector2(joystick.Horizontal, joystick.Vertical);
moveAmount = moveInput.normalized * speed;
}
Shoot code
private void Update()
{
Vector2 direction = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward);
transform.rotation = rotation;
if(Input.GetMouseButton(0))
{
if (Time.time >= shotTime)
{
Instantiate(projectile, shotPoint.position, transform.rotation);
shotTime = Time.time + timeBetweenShots;
}
}
}
Instead of using Input.mousePosition you'll have to use Input.GetTouch. You can loop through it using Input.touchCount to find the first touch that is not interacting with a ui element, than use that touch instead of Input.mousePosition to find the direction to shoot (or not shoot if there is no touch). To find out if a specific touch is over ui you need a reference to the scene's EventSystem (or use EventSystem.current), and use EventSystem.IsPointerOverGameObject with Touch.fingerId.
If the joystick is not a ui element you'll need a different way to detect if the touch is over the joystick. For example you could check the pixel position, or see if the joystick itself has an "interacting fingerId". But with the assumption that the joystick is an ui element, here's one way to do what I wrote above: (untested)
private void Update()
{
var eventSystem = EventSystem.current;
for (var i = 0; i<Input.touchCount; i++)
{
var touch = Input.GetTouch(i);
if (eventSystem.IsPointerOverGameObject(touch.fingerId))
{
continue;
}
ShootToScreenPos(Vector2 screenPos);
break;
}
}
private void ShootToScreenPos(Vector2 screenPos)
{
Vector2 direction = Camera.main.ScreenToWorldPoint(screenPos) - transform.position;
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(angle - 90, Vector3.forward);
transform.rotation = rotation;
if (Time.time >= shotTime)
{
Instantiate(projectile, shotPoint.position, transform.rotation);
shotTime = Time.time + timeBetweenShots;
}
}

How to face character in its movement direction and at the same time keep it aligned to any surface?

I want to move my player character(human) on a curved surface. But at the same time character shall stay perpendicular to the surface normals and it should face in the movement direction and can handle collisions(if there is a wall ahead, shall not be able to go through it).
I tried to make a parent stay over normals and change the child local rotation towards direction of motion of its parent. But it has several limitations as of now.
Here is the code what i was using:
[SerializeField] float raycastLength = 1f;
bool canPlayerMove = true;
public float speed = 2f;
public Vector3 offset; //object's position offset to ground / surface
public Quaternion childDirection;
private void Update()
{
float moveHorizontal = SimpleInput.GetAxis("Horizontal");
float moveVertical = SimpleInput.GetAxis("Vertical");
Ray ray = new Ray(transform.position, -transform.up);
RaycastHit hitInfo;
if (Physics.Raycast(ray, out hitInfo, raycastLength))
{
transform.rotation = Quaternion.LookRotation(Vector3.up, hitInfo.normal);
transform.position = hitInfo.point + offset;
Debug.DrawLine(ray.origin, hitInfo.point, Color.red);
}
if (canPlayerMove)
{
Vector3 movement = new Vector3(moveHorizontal, 0, moveVertical);
if (movement != Vector3.zero)
{
childDirection = Quaternion.Slerp(transform.GetChild(0).localRotation, Quaternion.LookRotation(movement), 0.15F);
transform.GetChild(0).localRotation = childDirection;
}
transform.Translate(movement * speed * Time.deltaTime, Space.Self);
}
}
first to not make your player go thru walls you want to add a collider to your walls and not set it as trigger, you will also need a rigidbody on your player and this will help in the next steps.
Secondly you will need to acces the rigidBody in code using this: (if you Check Use Gravity it will also stay on your terrain that you made)
private Rigidbody rb;
private float speed = 7.5f;
private void Start()
{
//this gets the rigidbody on the gameObject the script is currently on.
rb = this.GetComponent<Rigidbody>();
}
private void Update()
{
float hor = Input.GetAxis("Horizontal");
float vert = Input.GetAxis("Vertical");
//this will move your player frame independent.
rb.MovePosition(this.transform.position + new Vector3(hor, 0, vert) * speed *
Time.deltaTime);
}
Also make sure that you have a rigidBody on your player, else it will throw an error.

How to know if the object is moving upward or downward?

I have a gameobject and I would like to find out if the object is moving upward (positive) or downward (negative). How do I get to do this?
Assuming that the object has a rigidbody, you can use this in the update method (or anywhere for that matter) of a MonoBehavior attached to your GameObject.
Rigidbody rb = GetComponent<Rigidbody>();
float verticalVelocity = rb.velocity.y;
If you want the velocity along any axis, you can use the dot product:
Rigidbody rb = GetComponent<Rigidbody>();
Vector3 someAxisInWorldSpace = transform.forward;
float velocityAlongAxis = Vector3.Dot(rb.velocity, someAxisInWorldSpace);
The above code would give you the velocity along the GameObject's forward axis (the forward velocity).
If the object doesn't have a rigidbody, you can save its old position in a variable and then compare it to the current position in the update loop.
Vector3 oldPosition;
private void Start() {
oldPosition = transform.position;
}
private void Update() {
Vector3 velocity = transform.position - oldPosition;
float verticalVelocity = velocity.y / Time.deltaTime; // Divide by dt to get m/s
oldPosition = transform.position;
}

Unity - Move object until point

I'm using the code below to move a Ball to determined point. But, the ball are "teleporting" to there, how can i roll the ball until the point ?
void Update(){
if (Input.GetMouseButtonDown(0) && EventSystem.current.currentSelectedGameObject != ButtonDiminuir && EventSystem.current.currentSelectedGameObject != ButtonAumentar &&
EventSystem.current.currentSelectedGameObject != BarraForca) {
transform.position = Vector3.Lerp(transform.position, new Vector3(transform.position.x, transform.position.y, -9.0424f), 2 * Time.deltaTime);
Anim.Play("Kick_Up");
}
}
You can do it like this using Vector3.Lerp :
Vector3 startPosition;
Vector3 endPosition;
var speed = 10.0;
transform.position = Vector3.Lerp(startPosition, endPosition, speed * Time.deltaTime);
or using Vector3.MoveTowards
// The step size is equal to speed times frame time.
float step = speed * Time.deltaTime;
// Move our position a step closer to the target.
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
If you want to roll the ball, use AddForce() instead transform.position.
First, add Rigidbody and Sphere Collider to your ball game object.
Then try this code:
public Vector3 targetPoint;
public float forceAmount;
...
void Update()
{
Vector3 force = ((targetPoint - transform.position).normalized * forceAmount * Time.smoothDeltaTime);
GetComponent<Rigidbody>().AddForce(force);
}
Also, if you want to ball stops immediately when arrive to target point, you can set GetComponent<Rigidbody>().velocity to 0 when targetPoint - transform.position = 0
I hope it helps you.

Unity Rigidbody Click to move

I'm wanting to have a script that when I click in my scene, my player will rotate and have a force added to it and will travel until it has reached the clicked point in my scene.
Right now I have it working using Vectors and having my player lerp from one point to another. But I want to amend it so I can use physics and get a better sense of my player moving. Like have him speed up to start moving and slowing down as he reaches my target loction
My script now looks like this
public GameObject isActive;
public float speed;
public Ray ray;
public Rigidbody rigidBody;
public Vector3 targetPoint;
// Use this for initialization
void Start ()
{
targetPoint = transform.position;
}
// Update is called once per frame
void Update ()
{
}
void FixedUpdate()
{
if (Input.GetMouseButton(0))
{
targetPoint = Camera.main.ScreenToWorldPoint (Input.mousePosition);
ChangeRotationTarget ();
}
Quaternion targetRotation = Quaternion.LookRotation (targetPoint - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, speed * Time.deltaTime);
rigidbody.position = Vector3.Lerp(transform.position, targetPoint, speed * Time.fixedDeltaTime);
}
void ChangeRotationTarget ()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Plane playerPlane = new Plane (Vector3.up, transform.position);
float hitdist = 0.0f;
if (playerPlane.Raycast (ray, out hitdist))
{
targetPoint = ray.GetPoint (hitdist);
}
}
However, when I run this, he just slides from one point to another. Regardless of the drag or mass I put in my rigidbody.
Can someone help me make my changes? Or point me in the right direction
I didn't had time to test this, but it should almost be what you are looking for. Just apply your Quaternion and Rotation codes to it.
void FixedUpdate() {
if (Input.GetMouseButton(0))
{
targetPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (rigidBody.position != targetPoint)
{
reachedTargetPoint = false;
if (reachedTargetPoint == false)
{
GetComponent<Rigidbody>().AddForce(speed);
}
}
//zoneBeforeReachingTP should be how much units before reaching targetPoint you want to start to decrease your rigidbody velocity
if (rigidBody.position == (targetPoint - zoneBeforeReachingTP))
{
//speedReductionRate should be how much of speed you want to take off your rigidBody at the FixedUpdate time rate
rigidBody.velocity = speedReductionRate;
}
if(rigidBody.position == targetPoint)
{
rigidBody.velocity = new Vector3(0, 0, 0);
reachedTargetPoint = true;
}
ChangeRotationTarget();
}
}
That's because you modify the rigidbody.position, and your logic overrides the physics. What you have to do instead is to ApplyForce every physics frame, probably with ForceMode.Force or ForceMode.Acceleration.

Categories