How to Move an Object Towards a Direction Without Stopping

Set orientation of moving object towards it movement direction without using rigidbody

Neither transform.position (a point in space) nor transform.forward (the direction an object is already facing) will necessarily give you the direction an object is moving in.

For an object with a RigidBody, you'd access this through the velocity variable - but since it sounds like you're using a proprietary spline library and have no access to the object's actual movement direction, you may need to keep track of it yourself.

The simplest solution (if the library really doesn't provide movement information), is to store the object's present position and previous position each frame, and manually calculating the delta position (aka. The distance travelled between the frames) to find the velocity vector. For example:

public class MovingObject : MonoBehaviour {
private Vector3 prevPosition;

void Start() {
// Starting off last position as current position
prevPosition = transform.position;
}

void Update() {
// Calculate position change between frames
Vector3 deltaPosition = transform.position - prevPosition;

if (deltaPosition != Vector3.zero) {
// Same effect as rotating with quaternions, but simpler to read
transform.forward = deltaPosition;
}
// Recording current position as previous position for next frame
prevPosition = transform.position;
}
}

Hope this helps! Let me know if you have any questions.

Move object in the direction of it's rotation

You can flip sin/cos for x/y and use -velocity on y.

Here is a useful refactoring:

local function getLinearVelocity(rotation, velocity)
local angle = math.rad(rotation)
return {
xVelocity = math.sin(angle) * velocity,
yVelocity = math.cos(angle) * -velocity
}
end

...and you can replace:

local angle = math.rad( bullet.rotation )
local xDir = math.cos( angle )
local yDir = math.sin( angle )

physics.addBody( bullet, "dynamic" )
bullet:setLinearVelocity( xDir * 100, yDir * 100)

with:

physics.addBody( bullet, "dynamic" )
local bulletLinearVelocity = getLinearVelocity(bullet.rotation, 100)
bullet:setLinearVelocity(bulletLinearVelocity.xVelocity, bulletLinearVelocity.yVelocity)

How to make a game object move to the opposite direction of a constantly moving object? Unity 2D

So this:

playerRb.velocity = -rotatingPropellant.transform.position * impulseForce * Time.deltaTime;

relies on rotatingPropellant position relative to World's zero position (Vector3.zero), which might work only if the propellant revolves around this zero point.
What you should probably do instead is get the difference:

Vector3 dir = (rotatingPropellant.transform.position - transform.position).normalized;
playerRb.velocity = dir * impulseForce * Time.deltaTime;

Also, instead of changing velocity, you can add force instead:

Vector3 dir = (rotatingPropellant.transform.position - transform.position).normalized;
playerRb.AddForce(dir * impulseForce, ForceMode2D.Impulse);

Accelerate object towards a certain direction

Perform these steps in a method that is called in the Update or FixedUpdate method. FixedUpdate is recommended if you are using rigid bodies.

First, you need to find the direction from your position to the point, and define a velocity instance variable in your script if not using Rigid Bodies. If you are using a Rigidbody, use rigidbody.velocity instead. target is the Vector3 position that you want to accelerate towards.

// Use rigidbody.velocity instead of velocity if using a Rigidbody
private Vector3 velocity; // Only if you are NOT using a RigidBody

Vector3 direction = (target - transform.position).normalized;

Then you need to check if we have already passed the target or not. This check makes sure that the velocity remains the same

// If our velocity and the direction point in different directions 
// we have already passed the target, return
if(Vector3.Dot(velocity, direction) < 0)
return;

Once we have done this we need to accelerate our Transform or Rigidbody.

// If you do NOT use rigidbodies
// Perform Euler integration
velocity += (accelMagnitude * direction) * Time.deltaTime;
transform.position += velocity * Time.deltaTime;

// If you DO use rigidbodies
// Simply add a force to the rigidbody
// We scale the acceleration by the mass to cancel it out
rigidbody.AddForce(rigidbody.mass * (accelMagnitude * direction));

I recommend that you use a Rigidbody since it makes much more sense when doing something like this.

How to move a game object in the forwrard direction of an other game object Unity3D

Is the camera controller a component of the parent? If yes, good. Give it a reference to your child object with the camera and when scrolling change the position of the child not the parent.

When you now scroll the wheel, use camera.forward, with camera being the child.
For drag and everything else you still change the position of the parent object.



Related Topics



Leave a reply



Submit