Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Rotating an object based on mouse position

Discussion in 'Physics' started by raooffarhang, Apr 4, 2018.

  1. raooffarhang

    raooffarhang

    Joined:
    Apr 4, 2018
    Posts:
    4
    Hi everybody!
    I've been making a flippy knife game and I want to make my knife rotate based on where the mouse position was in the last frame before releasing the button and I have frozen the rotation of the knife in z and y and movement in the z-axis.
    this is the script I used to just rotate the knife only to left :

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

    public class knifesc : MonoBehaviour
    {
    public Rigidbody rb;

    public float force = 5f;
    public float torque = 20f;

    private Vector3 startswipe;
    private Vector3 endswipe;

    private float swipedelay;
    void Start ()
    {

    }
    void Update ()
    {
    if (!rb.isKinematic)
    {
    return;
    }
    if (Input.GetMouseButtonDown(0))
    {
    startswipe = Camera.main.ScreenToViewportPoint(Input.mousePosition);
    }
    if (Input.GetMouseButtonUp(0))
    {
    endswipe = Camera.main.ScreenToViewportPoint(Input.mousePosition);
    swipe();
    }
    }
    void swipe ()
    {
    swipedelay = Time.time;
    rb.isKinematic = false;
    Vector2 swipe = endswipe - startswipe;
    rb.AddForce(swipe * force , ForceMode.Impulse);
    rb.AddTorque(0f, 0f, torque, ForceMode.Impulse);
    }
    void OnTriggerEnter (Collider col)
    {
    if (col.tag == "Log")
    {
    rb.isKinematic = true;
    }
    else
    {
    Debug.Log("Fail");
    Restart();
    }

    }
    void OnCollisionEnter()
    {
    float TimeInAir = Time.time - swipedelay;
    if (!rb.isKinematic && TimeInAir >= 0.07f)
    {
    Restart();
    Debug.Log("Fail");
    }
    }
    void Restart ()
    {
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
    }

    _________________________
    and this is the one I tried to rotate it as I said and does not have any problems according to visual studio but does not rotate at all :

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

    public class knifesc : MonoBehaviour
    {
    public Rigidbody rb;

    public float force = 5f;
    public float torque = 20f;

    private Vector3 startswipe;
    private Vector3 endswipe;

    private float swipedelay;
    void Start ()
    {

    }
    void Update ()
    {
    if (!rb.isKinematic)
    {
    return;
    }
    if (Input.GetMouseButtonDown(0))
    {
    startswipe = Camera.main.ScreenToViewportPoint(Input.mousePosition);
    }
    if (Input.GetMouseButtonUp(0))
    {
    endswipe = Camera.main.ScreenToViewportPoint(Input.mousePosition);
    swipe();
    }
    }
    void swipe ()
    {
    if (endswipe.x > Screen.width/2 && startswipe.x > Screen.width/2)
    {
    rb.AddTorque(0f, 0f, -torque, ForceMode.Impulse);
    }
    if (endswipe.x < Screen.width / 2 && startswipe.x < Screen.width / 2)
    {
    rb.AddTorque(0f, 0f, torque, ForceMode.Impulse);
    }
    swipedelay = Time.time;
    rb.isKinematic = false;
    Vector2 swipe = endswipe - startswipe;
    rb.AddForce(swipe * force , ForceMode.Impulse);

    }
    void OnTriggerEnter (Collider col)
    {
    if (col.tag == "Log")
    {
    rb.isKinematic = true;
    }
    else
    {
    Debug.Log("Fail");
    Restart();
    }

    }
    void OnCollisionEnter()
    {
    float TimeInAir = Time.time - swipedelay;
    if (!rb.isKinematic && TimeInAir >= 0.07f)
    {
    Restart();
    Debug.Log("Fail");
    }
    }
    void Restart ()
    {
    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
    }
    }
     
  2. ThisGamesTooCool

    ThisGamesTooCool

    Joined:
    Apr 5, 2018
    Posts:
    28
    You can use Raycast for this:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         float oldZ;
    4.  
    5.         var v3 = Input.mousePosition;
    6.         v3 = Camera.main.ScreenToWorldPoint(v3);
    7.         if (Input.GetButtonDown("Fire1"))
    8.         {
    9.             if (Physics.Raycast(ray))
    10.                 knife.AddTorque(0.0f, 0.0f, oldZ - v3.z, ForceMode.Impulse);
    11.         }
    12.         oldZ = v3.z;
    13.     }
    I don't know if it can be useful for you because I don't know if I understanded exacly what do you want to do, but I thing it can work.
     
  3. raooffarhang

    raooffarhang

    Joined:
    Apr 4, 2018
    Posts:
    4
    I'm sorry if I made it hard to understand.
    In first code which it works, knife only rotates to the left when thrown but I want it to rotate to the side which player releases the mouse.
    For example, if they release the button on the left side of the screen knife rotates to left and if they release the button on the right side of the screen then it will rotate to right.
     
  4. ThisGamesTooCool

    ThisGamesTooCool

    Joined:
    Apr 5, 2018
    Posts:
    28
    Then the only thing you had to do is:
    Code (CSharp):
    1. Vector2 mousePosition = new Vector2(Input.mousePosition.x, Input.mousePosition.y);
    2. if (mousePosition < Screen.width / 2) {
    3.     //Rotate to the left
    4. } else {
    5.   //Rotate to the right
    6. }
    I think this can be suficient; if player releases the knife at the left side of the secreen then rotate the knife with your force based torque, and if you go to the right, well, the same thing as before.
    I think probably is better to do the knife rotate to the direction of moving what you give to it with the mouse, but if you want to do with the side of the screen this simple code can work anyways!
    PS: Sorry for my bad English... :p
     
  5. ThisGamesTooCool

    ThisGamesTooCool

    Joined:
    Apr 5, 2018
    Posts:
    28
    And other thing: you can post a code with code formating by the button in top of the post bar.
    This can do the code more organized!