Move object twoards its rotation - c#

Look into picures its z-axis not ahead so instead on using forward i am using this line of code to translate my object on keys
robotObj.transform.position += Vector3.right * Time.deltaTime * Input.GetAxis("Vertical") * -1;
An this line of code to rotate it.
robotObj.transform.Rotate(0f, Input.GetAxis("Horizontal") * speed, 0f);
but the problem is object is not moving towards it direction

Put an empty gameobject as parent of your robotObj and use the script you made to move the empty gameobject.
But make sure the z-axis is facing the x-axis of the robotObj!

If you want to move your object according to its looking direction (for you it is the red axis), then I would suggest you rotate your object first and then use transform.right as movement direction, like
transform.position += transform.right* Time.DeltaTime * Input.GetAxis("Vertical")
Note: transform.right take the object's local blue axis and projects it into world space (ref: https://docs.unity3d.com/ScriptReference/Transform-right.html)
Note 2: Edited to use transform.right although I would suggest putting your GameObject into another, empty GameObject to achieve the blue axis pointing forward, so you can use transform.forward, as it is more intuitive.

Related

Have an object follow a line I draw

I want to make my character jump by drawing lines.
But while my code is going towards the x-axis direction in 2D, when I switch to 3D, it always turns upwards regardless of the direction of the line. There is no change in the X-axis. My code is as follows:
if (other.gameObject.CompareTag("Line"))
{
rb.AddForce(Vector3.up * speed * Time.deltaTime, ForceMode.Impulse);
/ / Destroy(other.gameObject);
}
Could someone suggest how to correct my code?
Currently you're passing in Vector3.up as the force's direction, meaning that the character will always jump straight up.
If you want the character to jump in the line's direction, maybe try using the line's transform to send the player the right way, like this:
if (other.gameObject.CompareTag("Line"))
{
Vector3 jumpDir = other.gameObject.transform.up;
rb.AddForce(jumpDir * speed * Time.deltaTime, ForceMode.Impulse);
}
Now you'd need to point the "Line" GameObject in the direction you want the player to jump when they collide. (In the scene view, the Line transform's green arrow should be the character's jump direction)
You could switch it to the red or blue arrows (x or z axes) with
jumpDir = other.gameObject.transform.right;
or
jumpDir = other.gameObject.transform.forward; respectively.
You need to get the direction depending on the line you drew.
Depending on how you implemented your lines, you can get the directions from the transform via
Transform.up
Transform.forward
Transform.right
If you need the other directions, you can simply invert the Vectors.
Also you don't need to use Time.deltaTime while using rb.AddForce(), because the physic-system is taking care of it.

Make a Unity 3D body move in a circle from it's launch point?

I'm playing with some Unity code to expand my horizons.
I have the following code, which launches a bullet from the character that flies in a direction set by 'this.angle' which is the angle the gun is pointing to the target.
What I'm trying to accomplish is to make the bullet leave the gun and travel 360 degrees right around and shoot the player (actually the gun really) from behind.
I'm not fully understanding quarternions, but I don't think I need to, to solve this.
Could someone please give me a pointer?
base.transform.rotation = Quaternion.Euler(0f, Mathf.MoveTowardsAngle(base.transform.rotation.eulerAngles.y, this.angle, 9.3f * Time.deltaTime), 0f);
Vector3 cposition = base.transform.position;
base.transform.position = Vector3.MoveTowards(base.transform.position, cposition, Time.deltaTime * 2f);
this.pos.set(base.transform.position);
this.rot.set(base.transform.rotation);
Okay so if you want to stick with your original idea, you could make it like this:
Code for the bullet
void Update(){
transform.Rotate(degTurnRate, 0, 0);
transform.position += transform.forward * Time.deltaTime * bulletSpeed
}
You can make the code much simple if you just use the Transform.RotateAround
this enables you to rotate the object around the specified targt for the x amount on any of the axes.

How to smooth transform position?

I'm looking for solution to smooth transform position of my object.
To moving to new position I'm using that code
transform.position += Vector3.left * Time.deltaTime * 100f;
The effect of moving is to fast, so I want to make it more smooth. There is any option to change this code for better effect? Like the small bricks in this video when ball destroy big brick
https://youtu.be/mqj7eYna3Ds
You can also use this:
transform.Translate(Vector3.left * Time.deltaTime * 100f);
This should make it a bit smoother. Just remember if you would ad a velocity to the object, Transform.Translate wil not work nice!
if you want more float like movement you can give a Addforce to the attached rigidbody.
rigidbody.AddForce(transform.left * 10, Forcemode.Impulse);
Note: If you use Translate there wil not be any acceleration!
Rigidbody.AddExplosionForce may solve your problem but you cannot put the collision point as the origin. you may have to move your origin of an explosion a little bit below than the collision point. Let me know if it solves the issue.
https://docs.unity3d.com/ScriptReference/Rigidbody.AddExplosionForce.html
Using AddForce or manipulating the velocity variable on the Rigidbody is better if you want smooth movement.
However, if your object doesn't have a rigidbody, then you can just use the functions provided with the Transform class (e.g: Rotate(), Translate(), SetPositionAndRotation())
You can use this block of code to smooth out the movement of the player
public Transform player;
public Vector3 targetPosition;
public float smoothFactor = 2;
void Update()
{
player.transform.position = Vector3.Lerp(player.transform.position, targetPosition, Time.deltaTime * smoothFactor);
}

GameObject moves in the wrong direction after flipping transform.right

I'm working on a 2D game in Unity. I have some GameObjects which should move into the direction of the player.
On start I flip the transform.forward vector into the direction of the player.
Vector3 target = new Vector3(player.transform.position.x, transform.position.y, transform.position.z);
transform.right = (target - transform.position).normalized;
In the editor I can see that the transform.right vector (red arrow) is pointing in the right direction. But when I move the gameobject it moves in the wrong direction if the gameobject is right from the player.
rBody.AddForce(Vector2.right * movementSpeed);
And I have no idea why? Has anyone any suggestions? Thank you.
You're using AddForce() which always uses global space in reference to your character. You want to use local space to append your force, so use AddRelativeForce().
Try:
rBody.AddRelativeForce(Vector2.right * movementSpeed);
https://docs.unity3d.com/ScriptReference/Rigidbody.AddRelativeForce.html
Changing Vector2.right into transform.right solved my problem
rBody.AddForce(transform.right * movementSpeed);

Clamp player movement correctly

I have the following code to clamp player movement. It works but i have a problem. For example if the player is at position -3.05 and if I hold the button to move left the player still moves over the -3.05 limit to about -3.56. Once i let go of the button it bounces back to -3.05. Same goes for the right side. I do not want it to go over the limits no matter what.
Vector3 tmpPos = transform.position;
tmpPos.x = Mathf.Clamp(tmpPos.x, -3.05f, 3.05f);
transform.position = tmpPos;
The following is the way i add movement to the player:
rigidbody.AddForce (movement * speed * Time.deltaTime);
You should not mix up transform operation with rigidbody unless it's marked isKinematic. So instead of transform.position, try clamping rigidbody.position inside of FixedUpdate.
void FixedUpdate(){
Vector3 pos = rigidbody.position;
pos.x = Mathf.Clamp(pos.x, minX, maxX);
rigidbody.position = pos;
}
However, since you're using AddForce to move your object, a much simpler way is to make empty game objects with box collider on the left and right of the object, which then will limit your object movement like invisible walls.
Try using rigidbody.MovePosition(tmpPos); instead of setting transform.position.
I solved my problem. Instead of using the AddForce to move the object
rigidbody.AddForce (movement * speed * Time.deltaTime);
I use rigidbody.position to move the object. I use Mathf.Clamp to limit the movement before applying it to rigidbody.position.

Categories

Resources