Unity3D-rigidBody.velocity, unity3drigidbody

Source: Internet
Author: User

Unity3D-rigidBody.velocity, unity3drigidbody

I got off work in half an hour and wrote about the problems I encountered today, solutions, and some of my own understandings. This is not necessarily true. I hope you can correct it.

What I did today is hero's move.

Which of the following has been used before?Transform.Translate(Vector3.Forward*Time.DeltaTime*Speed);

But there are some bugs in this process, that is, moving is not sustained. Of course, you can also add a time delay function to simulate the animation effect a little bit, which is too troublesome (I Just Want To Do This ).

Later, the teacher guided me to work on rigidbody. AddForce (new Vector3 (, 0); I thought it was too simple, and the effect was that hero fell.

No way. I got a piece of code later.

Rigidbody. velocity = transform. forward * heroXSpeed * 1.1f;

Required distance-= heroXSpeed * Time. fixedDeltaTime * 1.1f;

The core is the first rigidbody. In the velocity group, it is used to calculate the velocity vector of a rigid body (good official). It is used to say that an object has a initial velocity. That's what it means.

However, this section is stored in the Update () function:

void Update (){animation.Play ("walk");rigidbody.velocity = transform.forward * heroXSpeed * 1.1f;}
Hero Mobile also has animations

However, if I write a trigger

void Update (){animation.Play ("walk");//rigidbody.velocity = transform.forward * heroXSpeed * 1.1f;if (Input.GetKey (KeyCode.D)) {rigidbody.velocity = transform.forward * heroXSpeed * 1.1f;//rigidbody.velocity =new Vector3(1,0,0);}}
In this way, when I press the D key, hero will not be able to execute the move operation.

I modified the code again.

void Update (){animation.Play ("walk");//rigidbody.velocity = transform.forward * heroXSpeed * 1.1f;if (Input.GetKey (KeyCode.D)) {rigidbody.velocity += transform.forward * heroXSpeed * 1.1f;//rigidbody.velocity =new Vector3(1,0,0);}}

You can add a plus sign (+. In this way, we can infer that Update () is a frame-by-frame rendering that can display the effect in real time. However, after judgment, we need to wait for a response and cannot achieve real-time results.

Another way of writing is:

// Update is called once per framevoid Update (){animation.Play ("walk");//rigidbody.velocity = transform.forward * heroXSpeed * 1.1f;if (Input.GetKey (KeyCode.D)) {//rigidbody.velocity += transform.forward * heroXSpeed * 1.1f;rigidbody.velocity =new Vector3(1,0,0);}}


This can also be moved, but the movement is faster, and the effect of transform. Translate () is almost the same.

Note: heroXSpeed is 0.1f.

It's time to get out of work. I hope you will give me more advice. After all, I am a newbie, haha.


Supplement:

The next day, I completed the code and adjusted my mind. After a while, hero will stop.

Using UnityEngine; using System. collections; public class Hero_Script: MonoBehaviour {// herox axis moving speed public float heroXSpeed = 0.1f; // whether hero is moving private bool m_bMoving = false; // hero's moving destination is private Vector3 m_targetPos; // Use this for initializationvoid Start () {}// Update is called once per framevoid Update () {Move (); if (m_bMoving) {animation. play ("walk"); rigidbody. velocity = transform. forward * heroXSpeed * 1.1f; // Debug. log (transform. position + "" + m_targetPos); if (Vector3.Distance (transform. position, m_targetPos) <0.1f) {// when the distance between the location of hero and the destination is only 0.1, stop the animation and move it to wait for rigidbody. velocity = Vector3.zero; m_bMoving = false; animation. play ("wait") ;}} void Move () {if (Input. getKey (KeyCode. d) {// record the target point m_targetPos = new Vector3 (transform. localPosition. x + 1, transform. localPosition. y, transform. localPosition. z); transform. lookAt (m_targetPos); // change the mobile Statem_bMoving = true ;}}}


I feel like I have learned things slowly ~~ Come on!






In Unity3D, I set rigidbody for the skeleton model to fall onto the ground but pass through each other.

Add a collider, mesh collider live box collider

Unity3d: Why does gravity and elasticity increase?

The ideal status is definitely not high, but the actual ball may fall into different points, whether there is really no initial speed, whether the ball itself has elasticity, whether the elasticity is even, etc., these may have an impact

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.