Search Unity

[Need help] - Basic movement (Camera direction)

Discussion in 'Scripting' started by Rahas, Jul 24, 2014.

  1. Rahas

    Rahas

    Joined:
    Jul 24, 2014
    Posts:
    2
    Hello! im new in unity and I don't have a lot of skills in coding, I am trying to make a simple movement systeme for my "player" which is a cube, I can make it move nicely in each direction perfectly. here's the script im using at this moment:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class playerMovement : MonoBehaviour {
    5.     public float moveSpeed;
    6.     private float maxSpeed = 5f;
    7.     public GameObject deathParticles;
    8.     private Vector3 spawn;
    9.     private Vector3 input;
    10.  
    11.     // Use this for initialization
    12.     void Start () {
    13.         spawn = transform.position;
    14.     }
    15.    
    16.     // Update is called once per frame
    17.     void Update () {
    18.         input = new Vector3 (Input.GetAxis ("Horizontal"), 0, Input.GetAxis ("Vertical"));
    19.         if (rigidbody.velocity.magnitude < maxSpeed)
    20.         {
    21.             rigidbody.AddRelativeForce (input * moveSpeed);
    22.         }
    23.         if(transform.position.y < -5)
    24.         {
    25.             Die ();
    26.         }
    27.     }
    28.  
    29.     void OnCollisionEnter(Collision other)
    30.     {
    31.         if(other.transform.tag == "Enemy")
    32.         {
    33.             Die();
    34.         }
    35.     }
    36.     void OnTriggerEnter(Collider other)
    37.     {
    38.         if(other.transform.tag == "Goal")
    39.         {
    40.             GameManager.CompleteLevel();
    41.         }
    42.     }
    43.  
    44.     void Die()
    45.     {
    46.         Instantiate(deathParticles, transform.position, Quaternion.identity);
    47.         transform.position = spawn;
    48.     }
    49. }
    50.  
    I have a mouse orbit camera attached to it and I would like to know how to push my cube in the direction my mouse is pointing instead of only going in one the direction the block is facing.

    Hope it's not too complicated :p
    Thanks
     
  2. OboShape

    OboShape

    Joined:
    Feb 17, 2014
    Posts:
    836
    Afternoon Rahas.

    Just to give my tuppence worth to get you started, im a beginner myself, but i did have to use this before.

    have a look at ScreenToWorldPoint ()
    http://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html


    it can take your current mouse position x and y, and translate that into world space coordinates.
    then you can use that to move your player towards that vector value.

    or use the LookAt() function on your player, and use the move keys from that.
    http://unity3d.com/learn/tutorials/modules/beginner/scripting/look-at
    or add your force using that vector .

    hope it helps a wee bit.
     
  3. Rahas

    Rahas

    Joined:
    Jul 24, 2014
    Posts:
    2
    the LookAt() function doesnt because it make my cube rotate to face the camera, even if I freezed the rotation. Thr ScreenToWorldPoint seems interesting, but I have no idea how to use it... Can someone tell me how please?
     
  4. herman111

    herman111

    Joined:
    May 11, 2014
    Posts:
    119
    use the search on here...or try the scripting reference