on how to control the movement of a character

Source: Internet
Author: User

Here will collect updates about how to control the move.

1. How to control the movement of characters by Wsad (i)

1  Public classPlayermovemeng:monobehaviour2 {3 //movement speed of the character4  Public floatSpeed=6f;5 //change in the movement of coordinates6 Vector3 movement;7 rigidbody playerrigidbody;8 //layer layers of plane that collide with Ray '9 intFloormask;Ten floatCamraylength=100f; One voidAwake () A { -Floormask=layermask.getmask (" Floor"); -Playerrigidbody=getcomponent<rigidbody>(); the } - voidfixedupdate () - { - //the control before and after WS + floatH=input.getaxisraw ("Horizontal"): - //control of ad around + floatV=input.getaxisraw ("Vertical") A Move (h,v); at Turning (); - } - //Implement Mobile - voidMove (floatHfloatv) - { -Movement. Set (H,0, v); inmovement=movement.normalized*speed*Time.deltatime; - //Use the moveposition of a rigid body to achieve character movement toPlayerrigidbody.moveposition (trandorm.position+movement); + } - //Steering Mouse the voidTurning () * { $Ray camray=Cameta.main.ScreenPointToRay (input.mouseposition);Panax Notoginseng Raycasthit Floorhit; - if(Physics.raycast (camray,out floorhit,camraylength,floormask)); the { + //get a vector of people and mouse clicks AVevtor3 playertomouse=floorhit.point-transform.position;
Gets the four-dollar number used for rotation between the character and mouse
theQuaternion newrotation=quaternion.lookrotation (Playertomouse);
To achieve a person's turn over
+ playerrigidbody.moverotation (newrotation); - } $ } $}

2. Implement the camera to follow the characters of the script (no rotation direction)

1  PublicTransform Target;2 //Lens sliding Speed3  Public floatsmoothing=5f;4 //the distance between the camera and the character5 Vector3 offset;6 voidStart ()7 {8offset=transform.positiion-target.position;9 }Ten voidfixedupdate () One { AVevtor3 targetcampos=target.position+offset; -Transform.position=vector3.lerp (transform.position,targetcampos,smoothing*time.datle); -}

on how to control the movement of a character

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.