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. Dismiss Notice

Turning Spachip controller into on mouse click

Discussion in 'Scripting' started by Berir, Jun 10, 2014.

  1. Berir

    Berir

    Joined:
    Mar 23, 2013
    Posts:
    51
    Hey Guys, is there any way to make this script work with a mouse click instead...

    I would really appreciate any help

    ( it is a space ship controller script from Unity space shooter tutorial)

    I love the movement in this code but would like to use it with mouse click instead

    Thanks in advance, I would appreciate any help
    Beri :)


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. [System.Serializable]
    4. public class Boundary  
    5. {
    6.    public float xMin, xMax, zMin, zMax;
    7. }
    8. public class PlayerController : MonoBehaviour
    9. {
    10.    public float speed;
    11.    public float tilt;
    12.    public Boundary boundary;
    13.  
    14.    void FixedUpdate ()
    15.    {
    16.      float moveHorizontal = Input.GetAxis ("Horizontal");
    17.      float moveVertical = Input.GetAxis ("Vertical");
    18.    
    19.      Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
    20.      rigidbody.velocity = movement * speed;
    21.    
    22.      rigidbody.position = new Vector3
    23.        (
    24.          Mathf.Clamp (rigidbody.position.x, boundary.xMin, boundary.xMax),
    25.          0.0f,
    26.          Mathf.Clamp (rigidbody.position.z, boundary.zMin, boundary.zMax)
    27.          );
    28.    
    29.      rigidbody.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
    30.    }
    31. }
    32.  
     
  2. raviraj_vdy

    raviraj_vdy

    Joined:
    May 14, 2009
    Posts:
    309
    Hi Berir,

    You will have to change the following lines


    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");

    Vector3movement = newVector3 (moveHorizontal, 0.0f, moveVertical);


    to something like this

    //float moveHorizontal = Input.GetAxis ("Horizontal");
    //float moveVertical = Input.GetAxis ("Vertical");

    Vector3newPos = Camera.main.ScreenToWorldPoint(newVector3(Input.mousePosition.x , Input.mousePosition.y , 0));

    playerMovement = newVector3(newPos.x , 0 , newPos.y);
     
  3. Berir

    Berir

    Joined:
    Mar 23, 2013
    Posts:
    51
    Thank you Raviraj for your reply, i tried to use that code but kept getting lots of errors, i tried to modify and fix it in any way my limited programing skills allowed me, but sadly i hit the wall...
    also I have noticed you used y position which I did correct into z... because it is a top down shooter so Y position is not used ... but still it did not work :(


    just curious did you try attaching that script on to a cube did it work?

    Thanks
     
    Last edited: Jun 10, 2014
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Berir likes this.
  5. Berir

    Berir

    Joined:
    Mar 23, 2013
    Posts:
    51
    Thank you very much for this
    this is what i was looking for ... even though I don't want my ship to move in this particular way , I will be able to modify it
    to my needs ...

    Thanks again i really appreciate it :)