Move a GameObject from point A to point B - Unity3D - c#

for the game I 'm creating I was trying to create a mega start that take you 750 meters ahead, but when trying to do this with vector3.lerp this is done instantly , and after trying many things he could not get it to work.
Here is the script that i wrote
if (in750Run)
{
PlayerManager.Instanse.gameObject.transform.position = Vector3.Lerp(PlayerManager.Instanse.gameObject.transform.position,PowerUpFinalePlayer.position, Time.deltaTime * 5);
Camera.main.transform.position = Vector3.Lerp(Camera.main.transform.position, PowerUpFinaleCamera.position, Time.deltaTime * 5);
if (Vector3.Distance(PlayerManager.Instanse.gameObject.transform.position, PowerUpFinalePlayer.position) > 2)
{
in750Run = false;
}
}
If you have a better way of doing this, please replay to this post :)

1) If you need smooth rotate to the target use:
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
2) To set a speed:
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

Related

How to make player follow touch x position smoothly?

Sorry for the noob question, I am still new to unity.
Currently, I'm creating a 2d game in unity in which I have a game object that should move on the x-axis following the x touch position in a smooth way to create a nice swipe mechanics (taking into account that the object is constantly being influenced by the gravity of the Rigidbody2d).
I already tried doing it with Vector3.MoveTowards(), Rigidbody2D.MovePosition(), and changing the velocity of the Rigidbody2D, but none of those created a smooth movement.
Vector3.MoveTowards():
transform.position = Vector3.MoveTowards(transform.position, new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x, transform.position.y, transform.position.z), speed * Time.deltaTime);
Rigidbody2D.MovePosition():
rigidbody2D.MovePosition(new Vector3(Camera.main.ScreenToWorldPoint(Input.mousePosition).x,
transform.position.y, transform.position.z));
Rigidbody2D.velocity
Vector3 mousePositionWorld = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (mousePositionWorld.x < transform.position.x)
{
rigidbody2D.velocity = new Vector2(-speed - -(speed * takeOff),
rigidbody2D.velocity.y)
}
else if (mousePositionWorld.x > transform.position.y)
{
rigidbody2D.velocity = new Vector2(+speed - (speed * takeOff),
rigidbody2D.velocity.y);
}
Am I missing something or is there a better way to achieve this?
I don't know what you mean by smooth, but i think that you can use Lerp function. Here is an example:
https://www.youtube.com/watch?v=d6BPukJ5QkA
is a short video.

how to do step-by-step code in unity 'void Update{}'?

I'm going to make track follower.
and here's my track in unity
Track image
I want to operate codes 1 ~ 10 at step by step in unity C# script method void Update{},
but I can't find how to make it.
I make rotation code using with trigonometric systems, Sin and Cos.
Rigidbody rigidbody;
public GameObject ball;
Vector3 P1 = new Vector3(-5, 1.5f, 25);
public float theta;
// Start is called before the first frame update
void Start()
{
rigidbody = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
ball.transform.position = Vector3.MoveTowards(transform.position, P1, 0.05f); // 1st move
if(theta <210) // 2nd rotation with trigonometic equations
{
theta += 1;
ball.transform.position = Rot(theta);
}
}
Vector3 Rot(float theta) // rotation by trigonometric equations
{
Vector3 P2;
P2.x = (3 * Mathf.Cos(theta * Mathf.PI / 180) - 3 * Mathf.Sin(theta * Mathf.PI / 180)) -8;
P2.y = 1.5f;
P2.z = (3 * Mathf.Sin(theta * Mathf.PI / 180) + 3 * Mathf.Cos(theta * Mathf.PI / 180)) + 22 ;
return P2;
}
if I play that code, the ball just warp to position P1 and immediately start rotation
how can i use that?
and can't use keyboard or something else input handler, only use by codes
To debug in unity you will need to use visual studio debug tool. Click on the play button "Connect with Unity" and play on the unity side. You will of course need to put a debug point where you need it.
Note: Your game will be stuck during the Update debug, if you can and you need to see update in the game view just execute the code that you need to debug in a coroutine.
If you just need to know the value of a variable you should be able to debug it without any problem even in the update.

How can I realise the same physics?

Once I saw a Google Play game where there was such
good physics. I'm new in Rigidbody2D move and physics. How can I do
the same physics? Now my bullet just explodes and don't boost tank. When I added Rigidbody2D, bullet pushed my tank
on a little bit, but that's all, my tank stops.
I have only default Rigidbody2D move script.
void FixedUpdate()
{
if (moveup1) // bool button for touch controls
{
direction = Mathf.Sign(Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.right)));
rb.AddRelativeForce(-Vector2.left * upspeed * 300); // these vectors looks weird but all work perfectly
}
if (movedown1) // bool button
{
direction = Mathf.Sign(Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.right)));
rb.AddRelativeForce(Vector2.left * downspeed * 300);
}
if (rotateleft1) // bool button
{
steeringAmount = -1;
rb.rotation += steeringAmount * steeringPower;
rb.AddRelativeForce(-Vector2.right * rb.velocity.magnitude * steeringAmount / 2);
}
if (rotateright1) // bool button
{
steeringAmount = 1;
rb.rotation += steeringAmount * steeringPower;
rb.AddRelativeForce(-Vector2.right * rb.velocity.magnitude * steeringAmount / 2);
}
}
Also
numbers should I put in the Rigidbody2D parameters? Now I'm using these params:
I hope for your understanding and patience. Thanks!
Lower Mass and Linear Drag. Also add Force when you instantiate the bullet. Force direction should be negative shooting direction.
If you want to push the tank on impact, the direction would be Vector3 dir = tank.position - impact.position. However the vector is longer on bigger distances, but we want the opposite (the close the explosion, the more force). So we can use float force = 1f / dir.magnitude and then apply it like this
rb.addForce(dir.normalized * force);
In general there is Rigidbody.AddExplosionForce but it's not available for 2D.

Making Player Movement Less Floaty

So I am having issues with my runner game.
At the moment the movement feels way to icy because I have momentum, I have to stop going one way to go the other and it gives me this really icy effect I don't want.
I have tried upping friction but that results in my cube tumbling down the track
void FixedUpdate()
{
rb.AddForce(0, 0, forwardForce * Time.deltaTime);
if (Input.GetKey("d"))
{
rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
if (Input.GetKey("a"))
{
rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
}
I want the movement to be nice and smooth and snappy.
So, first of all: I would recommend not using AddForce for movement of the player, you can just rb.velocity = sth; (that would remove the icy).
But! If you really want it that way, I guess you could try:
rb.AddForce(-rb.velocity*(from 0 to 1 the less the less icy) + (the speed you want to change to));
I think this would work. Try it, but also, if it's a capsule collider, you could loon at the Character Controller that makes it all look better and simpler, or at the Rigidbody Controller (not a component, just a name to define what you are trying but with the rb.velocity method).
Hope it works!
maybe this:
Vector3 CalculatedForce = -rb.velocity;
CalculatedForce.z = forwardForce * Time.deltaTime;
if (Input.GetKey("d"))
CalculatedForce.x += sidewaysForce * Time.deltaTime;
else if (Input.GetKey("a"))
CalculatedForce.x += -sidewaysForce * Time.deltaTime;
rb.AddForce(CalculatedForce, ForceMode.VelocityChange);

moving a rigidbody left and right

I can't seem to get my rigidbody to move left and right. The code looks fine and very similar to what everyone else has posted!
The debug statement is getting called but my character is not moving left and right.
Thanks for the help.
public float speed = 4.0f;
void Update()
{
float moveDirection = Input.GetAxis("Horizontal");
if (Input.GetKeyDown("d"))
{
Debug.Log("pressed d");
rb.AddForce(new Vector2(Time.deltaTime * speed * moveDirection, 0), ForceMode2D.Force);
}
I just tested it using an 3D environment, but that shouldn't matter. So after all I'm pretty sure you've got way to less force applied to AddForce.
So try increasing speed to about 40000, then you should be able to notice the AddForce being applied.
If you want to keep the speed value low, you could of course just add a multiplier here:
rb.AddForce(new Vector2(Time.deltaTime * speed * moveDirection * 10000f, 0), ForceMode2D.Force);
A nice one line option would be to use transform translate.
void Update ()
{
transform.Translate(Vector3.right * speed * Input.GetAxis("Horizontal") * Time.deltaTime);
}
AddForce will not work on a Rigidbody which is kinematic. Verify and set isKinematic to false in your Rigidbody component.
If this is already false, trying increasing the force value as suggested by d4Rk.

Categories