Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello.
I have this code that works quite well for moving an object but i need it to work differently.
I want it to move only when the mouse clicks and moves instead of moving where the mouse clicks.


using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class newmove : MonoBehaviour
{

    private Rigidbody rb;

    Vector3 pos;
    private Vector3 distance;

    public float speed = 6f;
    Vector3 tempP;

    public float maxY;
    public float minY;
    public float maxX;
    public float minX;

    void Start()
    {
        rb = GetComponent<Rigidbody>();

        tempP = transform.position;
    }

    // Update is called once per frame
    void FixedUpdate()
    {

        // If the game is paused, don't do anything
        if (PauseScreenBehaviour.paused)
            return;



        // Give the player forward velocity
        Vector3 moveVector = new Vector3(0, 0, speed);

        // Move him!
        rb.velocity = (moveVector * Time.deltaTime);



        distance = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0)) - transform.position;
        if (Input.GetMouseButton(0))
        {


            pos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 1));

            //pos.x = Mathf.Clamp(pos.x, minX, maxX);
            //pos.y = Mathf.Clamp(pos.y, minY, maxY);
            tempP.x = pos.x;
            tempP.y = pos.y;

        }

        transform.position = new Vector3(tempP.x + distance.x / 1.5f, tempP.y + distance.y /1.5f , transform.position.z);

       
           
        


    }

    }


What I have tried:

Because i do not have much experience with touch controls yet i chose to do it with the mouse click since i only need one mouse click(touch).
It is for an android game.
Posted
Updated 17-Jul-20 12:37pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900