I have a simple 3D L-System made from cylinders, and I currently just go through each symbol in the final string and do appropriate actions like draw, turn, rotate, push and pop. I am aware that push and pop pretty much resembles a new branch, but I tried changing the radius whenever push is called, but it leads to strange results.
Does anyone have the correct way of making newer branches becoming thinner than the previous branches?
Thanks.
else if((*it) == Push)
{
Radius -= 0.1f;
PushMatrix();
}
else if((*it) == Pop)
{
Radius += 0.1f;
PopMatrix();
}
You also need to reset the position of the turtle back to the point of the branch.
Related
I have been programming a deeper version of Pong using skills.
One of the skills is that the player will make the other player's paddle stop, and instead the enemy will start moving its goal.
I start moving the goal with this method. The method is called in the FixedUpdate.
private void moveGoal(string vertical, Vector3 nextPosition)
{
nextPosition += speed * Time.fixedDeltaTime * Input.GetAxis(vertical) * Vector3.up;
nextPosition.y = Mathf.Clamp(nextPosition.y, goalMinY, goalMaxY);
rb.transform.position = nextPosition;
}
We have tried doing it in the Update and using the Time.deltaTime, but the result is the same.
We recently changed to "rb.transform.position" from "rb.MovePosition(nextPosition)" because the problem was way worse.
The position is reset with a method inside the skill's script where we have saved the base position of the goal, and once the skill gets deactivated it automatically reset the goal's position to its base position.
The problem is that if the goal starts in the Y position 1.4, after it has been reset the y position changes slightly, for example going from 1.4 to 1.25.
We do not understand why it is moving even though the position we set it to is always the same.
I am sorry if the post sounds confusing, but the problem itself is very confusing and very difficult to explain.
Sorry if I am misunderstanding your issue. Could it be that you reset the position of the goal but within the same frame it executes moveGoal() once, setting it slightly off your original position? Goodluck.
So I am kinda stuck on what to do now because as I said I am trying to use Rigidbody.AddTorque to rotate a rigid body towards a certain point, which I was going to use to align a player with a gravitational pull so they can be upright. I have got the input part of the code, I just don't have a way to rotate the player to align with it, without violating the laws of physics with Quaternion.FromToRotation and messing with my character controller too, which I am trying to make entirely physics based rotation wise too to avoid any other problems.
I have experimented with a couple of methods, first I tried adapting my character controller code which used Rigidbody.AddForce to move the player and also dampening unwanted movements, as Rigidbody.AddTorque is basically Rigidbody.AddForce but for rotations, however, it was too weak and just flopping around when I tried it, here's the code for the character controller for calculating the force needed for one axes:
if(projected_speed.x*speed == relative_v.x)
{
applied_speed.x = 0f;
}
else if(Mathf.Sign(projected_speed.x)== -1)
{
applied_speed.x = relative_v.x - Mathf.Abs(projected_speed.x*speed);
}
else if (Mathf.Sign(projected_speed.x) == 1)
{
applied_speed.x = projected_speed.x*speed - relative_v.x;
}
Where projected_speed is the speed the controller wants to be at,relative_v is the relative velocity, and applied_speed is the speed that will be actually applied in Rigidbody.AddForce.
Anyways so maybe I didn't use enough force, as the player is under a gravitational pull, but that would have probably made it flip out or something, anyways so the second thing I tried was a PID controller, and I managed to find a page which explains it pretty well, sadly don't have the link anymore, but when I tried this because you have to tune it, I was stuck doing it, and it just wasn't able to do anything and was just rolling around the floor and sometimes spinning, probably as it couldn't cope with gravity, so that didn't work, so does anyone know how I could finally do this and make my character able to right themselves according to gravity?
Sorry for not indicating that my issue is solved, click here for the answer, credit to Ruzihm.
This builds upon a question I recently asked here:
Unity3D Leap Motion - Put hand in static pose (SetTransform causes Hand to spin crazy)
However, the code suggested in the only response given does not work. I have been using the 'SetTransform' method and while it does allow me to move the hand to my desired position, the rotation is crazy. The hand continuously spins and despite spending the best part of four days on it, I can't find the solution.
In short all I am trying to do it set the hand to a fixed pose (a fist for example) but have it move and rotate with the live hand data. I created a method (detailed in previous question) that would manually recalculate the positions of the joints to put the hand in the pose as the SetTransform was causing this crazy rotation. However, I still ended up with crazy rotation from having to transform the hand to rotate it, so I have switched back to the SetTransform method for ease.
posedHand = PoseManager.LoadHandPose(mhd.LeftHand, int.Parse("2"), transform);
Hand h = new Hand();
if(posedHand != null)
{
h.CopyFrom(posedHand);
h.SetTransform(LiveData.LeftHand.PalmPosition.ToVector3(), LiveData.LeftHand.Rotation.ToQuaternion());
}
All I really want is a method that I can pass two hand objects into (one being the current 'live' hand, the other being the desired pose) and get a hand object returned which I can then render.
Update
As requested here are images of what I am currently getting and what I want to achieve.
Current:
Target:
The target image would display the fixed pose, which in this example is a fist at the current position and rotation of the live hand. Thus meaning I could have my hand 'open' but onscreen I would see a fist moving around. As you can see from the 'current' the SetTransform is giving me the correct pose and position but the rotation is freaking out.
This question has been answere elsewhere. Unity3D Leap Motion - Put hand in static pose (Pose done just can't rotate)
Essentially, i had to create a function that took in the pose hand as a parameter which then used Hands.GetHand(Chiralty) to get the position and rotation from the live data which was then used to 'SetTransform' the posed hand to the new position and rotation
public static Hand TransformHandToLivePosition(Chirality handType, Hand poseHand)
{
Hand sourceHand = Hands.Get(handType);
Hand mimicHand = null;
if (poseHand == null && sourceHand != null && sourceHand.Fingers.Count > 0)
{
//poseHand = PoseManager.LoadHandPose(sourceHand, 2, this.transform, true);
}
if (poseHand != null && sourceHand != null)
{
// Copy data from the tracked hand into the mimic hand.
if (mimicHand == null) { mimicHand = new Hand(); }
mimicHand.CopyFrom(poseHand); //copy the stored pose in the mimic hand
mimicHand.Arm.CopyFrom(poseHand.Arm); // copy the stored pose's arm into the mimic hand
// Use the rotation from the live data
var handRotation = sourceHand.Rotation.ToQuaternion();
// Transform the copied hand so that it's centered on the current hands position and matches it's rotation.
mimicHand.SetTransform(sourceHand.PalmPosition.ToVector3(), handRotation);
}
return mimicHand;
}
I want to be able to direct my player when he is in the air. So I maded a method called CheckTilt() which is called in FixedUpdate() which is basically checking to see if the tablet has been moved in the x direction. The condition inside of CheckTilt() is based on a bool variable called isGrounded which basically is false when the player is airborne and true otherwise that part works fine. However, I cannot get the player to move based on the accelerometer. Here is my method:
public void checkTilt()
{
if (!isGrounded)
{
float tiltSpeed = 10.0f;
float x = Input.acceleration.x;
Vector2 tilted = new Vector2(x, 0);
myRigidBody.AddForce(tilted * tiltSpeed * Time.deltaTime);
}
}
Other Info: I'm building this for android device, and have tested it with the unity remote.
Fortunately the answer is just that nothing like that works with the Unity remote - you must build to try it.
Re your other question: it's almost impossible to guess the "parity". you have to try it, and if that is not right, just multiply by -1f. (What I do is include a UI button on screen, which, swaps the parity - but really just build twice until you get it!)
"I'm still not getting any movement when moved..."
it's an incredible pain in the ass getting the axes right. YES, on fact, it is 100% a fact that you will need to read all 3 axis, even if a 2D game.
Don't forget that
Apple STUPIDLY USE Z-BACKWARDS orientation ...
because they are utter idiots who have never worked in the game industry.
DOCO
But you will almost certainly have to do this:
You are familiar with using the Canvas / UI system, right? if not tell me.
1) make a canvas and add three Text fields
2) do this
public Text showX;
public Text showY;
public Text showZ;
and then
showX.text = yourAccelerometer.x.ToString("f2");
.. same for the others ..
It seems like a pain but I assure, it's the ONLY WAY to figure out what the hell is going on.
I'm working to let a character push objects around. The problem is that as soon as he touches an object he starts moving it, even when the touch was accidental and the character isn't facing the direction of the object.
What I want to do is get the direction the object when a collision happens, and if the camara is really facing that direction allow the player to move it.
Right now I only managed to get the direction of the object, but I don't know how to compare that with the direction of the camera.
This is what I'm trying now:
void OnCollisionEnter(Collision col) {
float maxOffset = 1f;
if (col.gameObject.name == "Sol") {
// Calculate object direction
Vector3 direction = (col.transform.position - transform.position).normalized;
// Check the offset with the camera rotation (this doesn't work)
Vector3 offset = direccion - Camera.main.transform.rotation.eulerAngles.normalized;
if(offset.x + offset.y + offset.z < maxOffset) {
// Move the object
}
}
You can try to achieve this in several different ways. And it is a bit dependent on how precise you mean facing the box.
You can get events when an object is visible within a certain camera, and when it enters or leaves using the following functions. With these commands from the moment the box gets rendered with that camera (so even if just a edge is visible) your collision will trigger.
OnWillRenderObject, Renderer.isVisible Renderer.OnBecameVisible, OnBecameInvisible
Or you could attempt to calculate whether and objects bounding box falls within the camera's view frustum, there for you could use the following geometry commands
GeometryUtility.CalculateFrustumPlanes, GeometryUtility.TestPlanesAABB
Or if you rather have a very precise facing you could also go for a Physics.Raycast So then you will only trigger the event when the ray is hitting the object.
Hope this helps ya out.
Take a compass obj and align it to ur device sorry object than when you move it you can always know where it points to.
theoretically it should work but maybe your object just moves around because of a bug in your simulator motion engine.