Have an object follow a line I draw - c#

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.

Related

How to use transform.forward that only acknowledges one axis of rotation?

I am making a rolling ball game, and the way I want it to work is based on constant forward movement and rotation based turning.
I'm using force, which then has the ball roll forward.
My Issue here is that I would like the ball to move forward based on where it's "facing". Using Vector3.forward only moves it according to world space and transform.forward will start going backwards because the ball rolls over, upside down.
How can I make it so transform.forward ignores my forward turning, only being effected by the sideways turning?
"forward" is hard to imagine on a sphere.
You add speed when rotating around the X-axis (ball moves "forward")
For direction control, you rotate around global Y-axis.
The transform.forward will spin around the object. But the transform.right (local x-axis so to say) stays stable.
So we only need the Vector3.Cross Product of the transform.right and the global Vector3.Up.
Vector3 forward = Vector3.Cross(transform.right, Vector3.up);
You can use vector math for this. Take the cross product between the local right and the world up:
private static Vector3 GetRollingForward(Vector3 localRight)
{
return Vector3.Cross(localRight, Vector3.up);
}
// ...
void Update()
{
Debug.DrawLine(transform.position, transform.position
+ GetRollingForward(transform.right));
}

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.

Move object twoards its rotation

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.

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.

Unity Instantiated object only moves up

I have a Light_Spell script attached to a magic wand which is parented to a Razer Hydra hand object. The Light_Spell takes a prefab of a Light which is projected out of it when a button is pressed.
However the light is just moving up, no matter what way I rotate the hand object, it always goes up. I had it working but I changed some code around and can't remember how I got it working in the first place.
Here is the code I have so far:
//What happens when bumper is pressed
if (isSelectedSpell && SixenseInput.Controllers [0].GetButtonDown (SixenseButtons.BUMPER) && triggerIsPressed == false) {
Rigidbody instantiateProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
instantiateProjectile.position += Vector3.down * 20.0F;
}
I have tried setting Vector3 to up and forward and a whole set of different things. Any ideas on what I should do to make it match the rotation of where the hand is pointing and stuff?
Thanks
Consider using:
instantiateProjectile.position += -instantiateProjectile.transform.up * 20.0f;
Explanation:
Vector3.down is not relative to the transform's rotation, it is a 'down' in World space, that is to say it will always be going down according to the cardinal axes.
transform.up is relative to that transform's rotation. If the projectile is rotated, the axes used to obtain "up" are rotated too. We must use an inverted transform.up instead of transform.down because no transform.down exists.

Categories

Resources