I'm directly modifying an object position by moving it around with the mouse button.
Vector3 touchPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector3 touchPosition = new Vector3 (touchPosition.x, touchPosition.y, transform.position.z);
touchPosition.z = transform.position.z;
if(Input.GetMouseButton(0)) {
transform.position = newPosition;
}
What I want to find out is what is the average velocity of the object in the last few frames. Any idea how to do this?
Probably would look something like this:
Vector3 velocity;
Vector3 lastPosition = transform.position;
Vector3 touchPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);
Vector3 touchPosition = new Vector3 (touchPosition.x, touchPosition.y, transform.position.z);
touchPosition.z = transform.position.z;
if(Input.GetMouseButton(0)) {
transform.position = newPosition;
velocity = (newPosition - lastPosition) / Time.deltaTime;
}
Related
im currently making a fps game and i have a rigidbody character controller and im trying to make it dash towards the direction the player is facing however my current dash function makes it go downwards and goes very fast
any ideas for how i can either fix the dashing or make a new dash mechanism?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class movement : MonoBehaviour
{
float yaw = 0f, pitch = 0f;
Rigidbody rb;
public float walkSpeed = 5f, sensitivity = 2f;
bool jumping = false;
private float DashDistance = 5f;
private void Start()
{
Cursor.lockState = CursorLockMode.Locked;
rb = GetComponent<Rigidbody>();
}
private void Update()
{
if (Input.GetKey(KeyCode.Space) && Physics.Raycast(rb.transform.position, Vector3.down, 1 + 0.001f))
rb.velocity = new Vector3(rb.velocity.x, 5f, rb.velocity.z);
if (Physics.Raycast(rb.transform.position, Vector3.down, 1 + 0.001f))
jumping = false;
else
jumping = true;
if (jumping && Input.GetKey(KeyCode.LeftControl))
Dash();
Look();
}
private void FixedUpdate()
{
Movement();
}
void Look()
{
pitch -= Input.GetAxisRaw("Mouse Y") * sensitivity;
pitch = Mathf.Clamp(pitch, -90f, 90f);
yaw += Input.GetAxisRaw("Mouse X") * sensitivity;
Camera.main.transform.localRotation = Quaternion.Euler(pitch, yaw, 0f);
}
void Movement()
{
Vector2 axis = new Vector2(Input.GetAxis("Vertical"), Input.GetAxis("Horizontal")) * walkSpeed;
Vector3 forward = new Vector3(-Camera.main.transform.right.z, 0f, Camera.main.transform.right.x);
Vector3 wishDir = (forward * axis.x + Camera.main.transform.right * axis.y + Vector3.up * rb.velocity.y);
rb.velocity = wishDir;
}
void Dash()
{
Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 1f, Input.GetAxis("Vertical"));
Vector3 offset = new Vector3(movement.x * transform.position.x, movement.y * transform.position.y, movement.z * transform.position.z);
rb.AddForce(transform.position + (offset * DashDistance), ForceMode.VelocityChange);
}
}
I think you should add force in the forward direction.
AddForce(transform.forward * yourForceValue);
Code:
private Touch touch;
public float speed;
public Animator animator;
Vector3 point = new Vector3();
Vector3 targetPos;
void Start()
{
targetPos = transform.position;
}
void Update()
{
if (Input.touchCount > 0)
{
touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Moved)
{
transform.position = new Vector3(transform.position.x + touch.deltaPosition.x * Time.deltaTime, transform.position.y, transform.position.z + touch.deltaPosition.y * Time.deltaTime);
Plane plane = new Plane(Vector3.up,transform.position);
Ray ray = Camera.main.ScreenPointToRay(touch.deltaPosition);
float point = 0f;
if (plane.Raycast(ray, out point)) targetPos = ray.GetPoint(point);
transform.LookAt(targetPos);
}
}
}
The problem is when I touch the screen the character looks in the touched direction, but when I move it forward (bottom of the screen to slightly above) it still looks towards the touch position. I want it so when it moves it faces the direction its going.
touch = Input.GetTouch(0);
if(touch.phase == TouchPhase.Moved)
{
transform.position = new Vector3(transform.position.x + touch.deltaPosition.x *
Time.deltaTime,transform.position.y,
transform.position.z + touch.deltaPosition.y * Time.deltaTime);
transform.LookAt(transform.position + touch.deltaPosition);
}
I am making a 3D game but my player can only move on the X and Y axis. I have a player with an attached camera following my mouse, but I only want it to follow up to a max radius distance from Vector3.zero, even if my mouse is beyond those bounds.
I have tried repositioning the player to the max distance on radius every frame it tries to follow the mouse outside its bounds, but this causes camera jitters even in LateUpdate.
void LateUpdate()
{
if (Input.GetMouseButtonDown(0)) {
firstTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
playerPos = transform.position;
}
if (Input.GetMouseButton(0)) {
Ray currentTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
Vector2 direction = currentTouchPos.origin - firstTouchPos.origin;
float distance = Vector3.Distance(transform.position, Vector3.zero);
if (distance >= radius) {
targetPosition = direction.normalized * radius;
} else {
targetPosition = playerPos + direction * touchSensitivity;
}
}
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * followSpeed);
}
I'm thinking there must be a way to clamp the positioning of the player to a radius so that I don't have to "leash" him back by repositioning him through code every frame.
You should try using Vector3.ClampMagnitude().
The solution was to use Clamp Magnitude.
void LateUpdate()
{
if (Input.GetMouseButtonDown(0)) {
firstTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
playerPos = transform.position;
}
if (Input.GetMouseButton(0)) {
// targetPosition will follow finger movements
Ray currentTouchPos = movementCam.ScreenPointToRay(Input.mousePosition);
Vector2 direction = currentTouchPos.origin - firstTouchPos.origin;
targetPosition = playerPos + direction * touchSensitivity;
}
transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * followSpeed);
transform.position = Vector3.ClampMagnitude(transform.position, radius);
}
I have been trying to drag and drop a rigidbody. I have used this script. It's moving the rigidbody with mouse but does not drop the rigidbody, it keeps moving.
public class move : MonoBehaviour {
public Camera cam;
private float maxWidth;
// Use this for initialization
void Start()
{
if (cam == null)
{
cam = Camera.main;
}
Vector3 upperCorner = new Vector3(Screen.width, Screen.height, 0.0f);
Vector3 targetWidth = cam.ScreenToWorldPoint(upperCorner);
float hatWidth = renderer.bounds.extents.x;
maxWidth = targetWidth.x - hatWidth;
}
// Update is called once per frame
void FixedUpdate () {
Vector3 rawPosition = cam.ScreenToWorldPoint(Input.mousePosition);
Vector3 targetPosition = new Vector3(rawPosition.x, 0.0f, 0.0f);
float targetWidth = Mathf.Clamp(targetPosition.x, -maxWidth, maxWidth);
targetPosition = new Vector3(targetWidth, targetPosition.y, targetPosition.z);
rigidbody.MovePosition(targetPosition);
}
}
I want something like this example that means it will drag a gameobject having rigidbody and drop in place.
Thanks
You need to use Input.GetMouseButtonDown for begin drag,
Input.GetMouseButton for dragging,
Input.GetMouseButtonUp for end dragging.
Simple code is:
void FixedUpdate () {
// 0 means left mouse button.
if(Input.GetMouseButton(0)){ // Check left button is still down so change rigidbody position
Vector3 rawPosition = cam.ScreenToWorldPoint(Input.mousePosition);
Vector3 targetPosition = new Vector3(rawPosition.x, 0.0f, 0.0f);
float targetWidth = Mathf.Clamp(targetPosition.x, -maxWidth, maxWidth);
targetPosition = new Vector3(targetWidth, targetPosition.y, targetPosition.z);
rigidbody.MovePosition(targetPosition);
}else if(Input.GetMouseButtonDown(0)){
Vector3 startPositionRaw = cam.ScreenToWorldPoint(Input.mousePosition);
}else if(Input.GetMouseButtonUp(0)){
cam.ScreenToWorldPoint(Input.mousePosition);
}
Update for your need
void FixedUpdate () {
// 0 means left mouse button.
if(Input.GetMouseButton(0)){ // Check left button is still down so change rigidbody position
RaycastHit hit;
Ray ray = cam.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray,out hit,100)){
if(hit.collider.gameObject.name=="TargetObjectName"){
Vector3 rawPosition = cam.ScreenToWorldPoint(Input.mousePosition);
//This is your solution
Vector3 targetPosition = new Vector3(rawPosition.x, 0.0f, 0.0f);
float targetWidth = Mathf.Clamp(targetPosition.x, -maxWidth, maxWidth);
targetPosition = new Vector3(targetWidth, targetPosition.y, targetPosition.z);
// Maybe this is more usefull
Vector3 targetPosition = new Vector3(rawPosition.x, 0.0f, 0.0f);
rigidbody.MovePosition(targetPosition);
}
}
}else if(Input.GetMouseButtonDown(0)){
Vector3 startPositionRaw = cam.ScreenToWorldPoint(Input.mousePosition);
}else if(Input.GetMouseButtonUp(0)){
Vector3 endPositionRaw = cam.ScreenToWorldPoint(Input.mousePosition);
}
PS: Maybe, some syntax error in code i don't check it.
I am creating a 2D game and I have a prefab enemy, this enemy is a cannon. I want rotate this cannon when player change position.
The cannon should always rotate to the player position
I'm trying this.
// Update is called once per frame
void Update () {
float distance = Vector2.Distance(player.position, transform.position);
if(distance < 10){
Vector2 dir = player.position - transform.position;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
Quaternion qto = Quaternion.AngleAxis(angle, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, qto, 5f * Time.deltaTime);
}
}
see the result: https://www.youtube.com/watch?v=REeSNKWLvIQ
The cannon isn't rotate to the player position.
How can I solve this problem ?
Your Code is Fine Just Make a Small Change And You Are Good to Go :
void Update () {
float distance = Vector2.Distance(player.position, transform.position);
if(distance < 10){
Vector2 dir = player.position - transform.position;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
Quaternion qto = Quaternion.AngleAxis(angle, Vector3.forward);
Quaternion qto2 = Quaternion.Euler (qto.eulerAngles.x,
qto.eulerAngles.y,
qto.eulerAngles.z + 90);
transform.rotation = Quaternion.Slerp(transform.rotation, qto2, 5f * Time.deltaTime);
}
}
I'm Just Add qto2 and Slerp to that.
You can try this code:
float turnspeed=1.0f;
//set a turning speed
void Update ()
{
dir = player.position - transform.position;
dir.Normalize();
transform.rotation = Quaternion.Slerp(transform.rotation,Quaternion.LookRotation(dir), turnSpeed *Time.deltaTime);
}