using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move : MonoBehaviour
{
Vector3 targetPos;
float moveSpeed = 2;
Vector3 velotity = Vector3.zero;
float smoothTime = .3f;
Rigidbody rigidbody;
void Start()
{
// targetPos = transform.position;
targetPos = new Vector3(10, 10, 10);
rigidbody = gameObject.GetComponent<Rigidbody>();
}
void Update()
{
/// ****************!Tranform !***start**********/
///* !1.transform.Translate 物体朝某个方向移动
// transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
/// 键盘控制物体上下左右移动
/* float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
transform.Translate(new Vector3(h, 0, v) * moveSpeed * Time.deltaTime);*/
///* !2.Vector3.MoveTowards 物体朝着某个点移动
// transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
/// 物体移动到鼠标点击位置
/* if (Input.GetMouseButtonUp(0))
{
//鼠标位置转换为世界坐标
Vector3 selfPos = Camera.main.WorldToScreenPoint(transform.position);
Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, selfPos.z);
targetPos = Camera.main.ScreenToWorldPoint(mousePos);
}
transform.position = Vector3.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);*/
///* !3.Vector3.SmoothDamp 通常用于平滑跟随相机
// transform.position = Vector3.SmoothDamp(transform.position, targetPos+相机初始位置, ref velotity, smoothTime);
///* !4.直接赋值跟随时间逐渐移动
//transform.position += Vector3.right * moveSpeed * Time.deltaTime;
/// ****************!Tranform !***end**********/
}
private void FixedUpdate()
{
/// ****************!Rigidbody !***start**********/
///物体添加 Rigidbody组件 避免受重力影响 属性Use Gravity取消勾选
///* !1.rigidbody.MovePosition 朝某方向移动
// rigidbody.MovePosition(targetPos * moveSpeed * Time.fixedDeltaTime);
/// 键盘控制物体上下左右移动
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
//rigidbody.MovePosition(transform.position + new Vector3(h, 0, v) * moveSpeed * Time.fixedDeltaTime);
///* !2.rigidbody.velocity
//rigidbody.velocity = moveSpeed * new Vector3(h, 0, v);
///* !3.rigidbody.AddForce
// 朝某方向发力
//rigidbody.AddForce(transform.right, ForceMode.Impulse);
//朝某方向匀速移动
// rigidbody.AddForce(Vector3.right * moveSpeed);
/// ****************!Rigidbody !***end**********/
}
}