Search Unity

Unity 3D move left & right by mouseclick

Discussion in 'Scripting' started by argens, Oct 22, 2018.

  1. argens

    argens

    Joined:
    Jun 21, 2018
    Posts:
    26
    Hello, making a script in c# with which I would like to move my ship left or right (only on x), for a space attack game like, but when I press the left mouse button, the ship doesnt move. Can anyone help me to understand why?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Player3D : MonoBehaviour {
    6.  
    7.     public float speed;
    8.  
    9.     // Use this for initialization
    10.     void Start ()
    11.     {
    12.        
    13.     }
    14.    
    15.     // Update is called once per frame
    16.     void Update ()
    17.     {
    18.         MoveHandler();
    19.     }
    20.  
    21.     void MoveHandler()
    22.     {
    23.  
    24.         if (Input.GetMouseButton(0))
    25.         {
    26.             Vector3 clickPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    27.  
    28.             if (gameObject.transform.position.x < clickPosition.x)
    29.             {
    30.                 GetComponent<Rigidbody>().velocity = new Vector3(speed, 0, 0);
    31.             }
    32.  
    33.             else if (gameObject.transform.position.x > clickPosition.x)
    34.             {
    35.                 GetComponent<Rigidbody>().velocity = new Vector3(-speed, 0, 0);
    36.             }
    37.         }
    38.  
    39.         else
    40.         {
    41.             GetComponent<Rigidbody>().velocity = Vector3.zero;
    42.         }
    43.     }
    44. }
     
  2. argens

    argens

    Joined:
    Jun 21, 2018
    Posts:
    26
    Am now here:

    Code (CSharp):
    1. if (Input.GetMouseButton(0))
    2.         {
    3.             Vector2 clickPosition = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y));
    4.  
    5.             if(clickPosition.x < gameObject.transform.localPosition.x)
    6.             {
    7.                 GetComponent<Rigidbody>().velocity = new Vector3(-speed, 0, 0);
    8.  
    9.                 Debug.Log("Left Click Position" + clickPosition.x);
    10.                 Debug.Log("Game Object" + gameObject.transform.position.x);
    11.             }
    12.  
    13.             else if (clickPosition.x > gameObject.transform.localPosition.x)
    14.             {
    15.                 GetComponent<Rigidbody>().velocity = new Vector3(speed, 0, 0);
    16.  
    17.                 Debug.Log("Right Click Position" + clickPosition.x);
    18.                 Debug.Log("Game Object" + gameObject.transform.position.x);
    19.             }
    20.  
    21.             Debug.Log("Zero Click Position " + clickPosition.x);
    22.             Debug.Log("Game Object " + gameObject.transform.position.x);
    23. }
    The position of "Zero Click Position" is 0
    Position of "Game Object" is also 0.

    Why is this and how to change it?
     
  3. barskey

    barskey

    Joined:
    Nov 19, 2017
    Posts:
    207
    A couple of things...

    ScreenToWorldPoint takes a Vector3. Input.mousePosition is also a Vector3. So you could write:
    Vector3 clickPosition = Camera.main.ScreenToWorldPoint (Input.mousePosition);


    Input.GetMouseButton() will return true while the mouse is held down, and false when it is released. You might want Input.GetMouseButtonDown() instead, just as an FYI.

    If the ship is not a child gameobject, then transform.localPosition is the same as transform.position, just as an FYI.

    If you're only ever seeing the "Zero Click Position" debug statement, then neither if conditional is ever resulting in true. Try adding another Debug or two to see what is not getting set as you expected.
    Debug.Log (Input.mousePosition);

    Debug.Log (Camera.main.ScreenToWorldPoint (Input.mousePosition));
     
  4. argens

    argens

    Joined:
    Jun 21, 2018
    Posts:
    26
    Thanks for the answer, I solved it halway now.

    The main reason for the strange behaviour of the ScreenToWorldPoint calculation is, becouse the main camera is an an angle. I changed the camera projection in unity from perspective to ortographic and now it works.
    Not sure however, how to solve the problem, when the camera is in perspective projection and is also set in an angle (like 45°) in the x axis.