Cart physics movement Unity - unity3d

I want to make a cart following rails so i made a cyclindric path and an invisible sphere that move along so i can follow the position of the sphere and apply to my cart .
There is no problem with position tracking however the rotation lookat have some issues every thing work fine when i climb forward
but if is a backward decent there is a flip on the object rotation then the camera which follow the cart rotation
I only want a solution that keep my cart looking straigth to the rail and the invisible sphere to look that he snap on rail and follow there angle.
Here is a Unitypackage of my project so that you can understand more what is the probem :
https://mega.nz/#!IY8WCagb!UUZYmWVHCVcoSZ11_gn-L5IMnJFVVtvkrO0mZxikw9o
Thanks in adance for your answer and your help

Try having two spheres, one for each pair of wheels. The one sphere will obviously travel the rails a short distance in front of the second sphere. Position of your cart is in the center of the two spheres, direction of view is from rear to front sphere.
pos = Vector3.Lerp(front_sphere_pos, rear_sphere_pos, .5f);
orientation = Quaternion.LookRotation(front_sphere_pos - rear_sphere_pos);
Depending on the size of the rails and the carts it may be that the spheres touch. In that case you have to disable collision between the spheres and only have them collide with the rails.

Related

How to roll a cube on it's edges on Unity 3D

I want to move a cube by making it roll on it's edges. The cube will be standing on a x-z grid and each movement it takes will make it stand on a different square of the grid.
The player will controll the movement and will only be able to make the cube roll on one direction at a time (left, right, forward or back), but the cube must always stand exactly on top of one of the grid's squares.
I don't think applying a force to the cube will help cause it could move it a little bit too much or too little. I want to achieve something like this: https://www.youtube.com/watch?v=yaAIUYuNi84 but only on the x-z plane. Notice how on each corner the cube can stop and change direction with ease, because it never moves too much or too little.
Any ideas on how to approach this?
If you are new to the Unity it will be useless to trowing you a bunch of codes so i am telling you the way of doing it so you can implement your own codes.
You can create 4 empty game object which will always follow the cube on the floor and when you want to roll the cube you will rotate the cube around the empty objects.
You can find the codes for following the cube and rotating the cube on youtube, and for the starters searching is allways good.
So i hope you can manage it out, if you cant please write me again where did you stuck and i will be happy to answer you :)

How can I rotate an object based on an angle?

I am working in Unity3D and I was wondering how I would rotate a cube based on the angle between the cube and the mouse position. With that I don't mean the mouse position in world space but in pixels.
Here are some pages that'll lead you to your answer:
Input.mousePosition This also includes an example of how to turn screen coordinates into a ray in world coordinates. If you know how far away from the camera you want your point, check out ScreenToWorldPoint for a point instead of a ray.
transform.Rotate To perform a rotation.
The rest of your question is kinda vague--rotating "based on" the angle between cube and mouse position could mean a lot of things. You shouldn't need much more information than this though!

How to make Unity3d move objects on a table?

I am working on a tabletop game that user controls using accelerometer in the phone. It is pretty similar to Labyrinth on iOS.
The scene contains a tabletop created using planes and cubes, and a sphere as a ball. As it works in Labyrinth game, on tilting phone ball should drop to the tilted side, while the camera stays centered to table. I am trying to do similar thing where user tilts the phone, and objects on table move to tilted side.
Currently, I add the x and z component to Physics.gravity on tilt. Sadly, this change in gravity does not affect the ball which stays put on the table. Use gravity is selected for the ball, and it drop down from height to the tabletop initially and then comes to halt. After the initial drop, ball does not react to any gravity change.
I have also tried rotating the whole table, but using transform.rotate does not work either. The table rotates perfectly, alongwith the camera, but the ball stays put hanging in the air. So, currently I am out of my depth about the issue.
Is there any other way to allow tilt action registered, so that ball moves to the tilted direction? I cannot use addforce function, as there are multiple objects which need to react to tilt, and it will be difficult to keep track of that many addforce calls. How else can I get it working?
See this post for some related information.
As for why your sphere is sticking to the table, can I assume your sphere has a rigidbody? If so, you need need to wake it up:
if (rigidbody.IsSleeping())rigidbody.WakeUp();
Ideally you would make that call only after detecting a change in orientation/gravity, not every frame.

mouse joint is not working to restrict the ball in the half part of the screen

Hi guys I Am developing the application in cocoas2d using the box 2d frame work but unfortunately
i'm not able to restrict the gray ball in the half screen area of the image shown here
i want that ball not to go opposite part of the screen
I Have Used The b2Mousejoint For to move the ball around the screen
b2PrismaticJointDef restrict on any particular axis
But
i want to restrict on the particular rect area of the screen
You could create your custom distance joint which will restrict global axes of the ball. But it will be hard if you never write your own physics engine.
There are 2 easier ways to implement what you want.
Create 4 static "border" boxes around the area where the ball must stay. Then place the ball and the boxes into one collision group.
However, the response of the "border" boxes will not be instant. Therefore, the ball at high speed will sometimes "sink" into the boxes, then be popped out.
You can correct the position and reset the speed of the ball manually in code when it crosses the bounds of the desired area. But it may lead to the unstable physics simulation.

Cocos2d Accelerometer Movement

I was wondering how you would move a CCSprite right and left by tilting the device right and left. Like in the falling balls app.
Try this:
http://www.cocos2d-iphone.org/wiki/doku.php/tips:using_accelerometer_for_sprite_movement
it is an example code to move the sprite on a plane surface along the x and y accelerometer axis as per the tilt.
The first solution provided is dependent on Chipmonk which is a 2d physics engine. The accelerometer can be used to move a CCSprite without the need of a physics engine. There are several ways to achieve what you want.
This is a great blog with awesome tutorials to get you moving in the right direction.
http://www.raywenderlich.com