I am using Unity3d with mono develop(c#).
My object is Arrow.It gets Random Angel at starting.When i click on arrow ,it should be move like gun shooting some Point.How to achieve this?can any one help?
The forward Vector3 on the Transform belonging to your GameObject describes which direction is forward for the object.
An example given on the linked page is rigidbody.velocity = transform.forward * 10;. So if you were to put that line of code in your FixedUpdate function then your GameObject would move forward at a velocity of 10 * framerate.
Related
I have bunch of Cylinders connected with Hinge joints, as per a youtube tutorial. Now the hinge joints move when an ExplosionForce is applied to them, but I want them to 'swing' back and forth when they are moved via the animation.
So I have the top Cylinder's RigiedBody set to Kinematic, and I animate that along the X axis to the right. Then it stops abruptly, and i want the bottom or the rest of the 'rope' to swing a bit further to the right then back again like a rope would. Ultimately I want to attach a spotlight to this rope and light up my scene. But I can't get the rope to swing, it just stays static.
I've exported the scene as a package, you can download it here:
https://www.pastefile.com/Up59SO
Here is an animation of the rope not swinging:
Please help. This is really annoying and I cannt see why it shouldnt be working!
Firstly make sure all rigidbodies are in the root of the scene; not parented to anything. Secondly, you have to use physics forces to get the elements to swing. However, you can't apply forces to kinematic objects. So what I would advise is instead of setting the root of the rope as kinematic to freezing its position on the Y axis.
public class Movement : MonoBehaviour
{
[SerializeField] Rigidbody rigidbody;
[SerializeField] float power = 25;
[SerializeField] float speed = 5;
void FixedUpdate()
{
rigidbody.AddForce(Vector3.right * Mathf.Cos(Time.time * speed) * power);
}
}
Maybe something like this could help, https://answers.unity.com/questions/1244519/how-to-randomly-set-the-direction-of-an-object-and.html?_ga=2.69603664.1732232616.1585344454-527098876.1571345094
Try thinking of it like wind.
I am trying to figure out how to modify HelloARController.cs from the example ARCore scene to place objects directly in front of the camera. My thinking is that we are raycasting from the camera to a Vector3 on an anchor or tracked plane, so can't we get the Vector3 of the start of that ray and place an object at or near that point?
I have tried lots, and although I am somewhat a beginner, I have come up with this
From my understanding, ScreenToWorldPoint should output a vector3 of the screen position corresponding to the world, but it is not working correctly. I have tried other options instead of ScreenToWorldPoint, but nothing has presented the desired effect. Does anyone have any tips?
To place the object right at the middle of the camera's view, you would have to change the target gameObject's transform.position (as AlmightyR has said).
The ready code would look something like this:
GameObject camera;
GameObject object;
float distance = 1;
object.transform.position = camera.transform.position + camera.transform.forward * distance;
Since camera's forward component (Z axis) is always poiting at the direction where Camera is looking to, you take that vector's direction and multiply it by a distance you want your object to be placed on. If you want your object to always stay at that position no matter how camera moves, you can make it a child of camera's transform.
object.transform.SetParent(camera.transform);
object.transform.localPosition = Vector3.forward * distance;
Arman's suggestion works. Also giving credit to AlmightyR since they got me started in the right direction. Here's what I have now:
// Set a position in front of the camera
float distance = 1;
Vector3 cameraPoint = m_firstPersonCamera.transform.position + m_firstPersonCamera.transform.forward * distance;
// Intanstiate an Andy Android object as a child of the anchor; it's transform will now benefit
// from the anchor's tracking.
var andyObject = Instantiate(m_andyAndroidPrefab, cameraPoint, Quaternion.identity,anchor.transform);
The only problem with this is that because of the existing HelloAR example code, an object is only placed if you click on a point in the point cloud in my case (or a point on a plane by default). I would like it to behave so that you click anywhere on screen, and it places an object anchored to a nearby point in the point cloud, not necessarily one that you clicked. Any thoughts for how to do that?
Side tip for those who don't know: If you want to place something anchored to a point in the cloud, instead of on a plane, change
TrackableHitFlag raycastFilter = TrackableHitFlag.PlaneWithinBounds | TrackableHitFlag.PlaneWithinPolygon;
to
TrackableHitFlag raycastFilter = TrackableHitFlag.PointCloud;
I am a beginner and trying to make penalty shooter Game in unity.. I have just setup the scene and just trying to shoot the ball towards the goal post.
When i shoot the ball it goes toward the goal but does not come down, because i am shooting it through the script and its gravity is off and kinematic is on.
Currently i have the following script:
void Start () {
startTime = Time.time;
rb.GetComponent<Rigidbody>();
}
void Update () {
transform.position -= fakevel * Time.deltaTime;
transform.position += fakeGravity * Time.deltaTime;
fakevel *= 0.999f ;
}
void OnTriggerEnter( Collider other ) {
fakevel = new Vector3(0.01f, 0, 0) * 2000f;
fakeGravity = new Vector3 (0 ,0.01f, 0)* 200f;
y = -45.68312f;
}
}
I have tried enabling the gravity and disabling the kinematic at some specific position but i does so the gravity just pull it down at that position and it does not look realistic. Some screen shoots are attached.
Please help me in setting trajectory of the ball and stopping it when it collides with the goalpost,
I would suggest you use a non-kinematic rigidbody on your ball and add a force to it when you shoot it: GetComponent<Rigidbody>().AddForce(forceVector3, ForceMode.Impulse) (this is just an example, you should avoid calling GetComponent on a frequent basis, you are better off calling it for the rigidbody component once on Start and store it in a private variable)
This way Unity's physics engine can handle gravity and the balls' velocity over time. To modify the trajectory you can play around with the properties of the balls' rigidbody component.
If you want to have more control over the velocity, you can also set the rigidbody's velocity directly in the FixedUpdate() (which is called right before each physics update). When doing so, you have to consider that velocity is measured in m/s and the physics engine handles sub-steps for you (I am assuming that FixedUpdate() is called more than once per second), meaning you do not have to multiply your velocity with some delta time (but I am not 100% sure about this right now).
Seeing how it does work, just doesn't give the effect you want, you can either spend several hours fine tuning it, or like xyLe_ stated, use the built in physics. Using the built-in physics isn't a bad thing, you just gotta be smart with it. You can add a bouncy physics material also, which will help give the ball a well, ball feel.
I have 2 AI game objects which are both capsules. Considering the first capsule AI is named X and the second capsule AI is named Y, I try to make Y move away from X (escape from him) while X is chasing Y (following him). I have no idea how to do that, I would appreciate a direction.
I tried to do what's written here, but they both move through walls even though they have capsule collider, I tried to do this:
http://forum.unity3d.com/threads/getting-objects-to-move-away-from-my-users-gameobject.142468/
but they only move in one direction and through walls.
Vector3 position = transform.position;
Vector3 targetPosition = target.transform.position;
Vector3 direction = position - targetPosition;
transform.position += direction * 2.0f * Time.deltaTime;`
You are moving them with transform.Translate. Moving the transform means "Put the object at the given position regardless of environment". If you want them to interact, you need to either use the CharacterController component and its methods or a rigidbody component and move it with force.
This is a top-down view but in 3D coordinates, I would like to instantiate and fire a bullet from the player's gun. This script is on a spawner object at the end of the barrel. Also tried putting the script on the player itself but also didn't work.
GameObject projectile = Instantiate (bullet, transform.position, transform.rotation) as GameObject;
projectile.GetComponent().AddForce(transform.forward * speed);
The problem is the bullets doesn't behave as intended, instead they don't appear relative to the player rotation they just go in a very different direction. Shouldn't "Transform.Forward" mean forward in the Z position regarding the object's transform ?