Search Unity

"my" script doesn't go where my mouse is pointed, just to right.

Discussion in 'Scripting' started by Temp10101, Apr 17, 2015.

  1. Temp10101

    Temp10101

    Joined:
    Feb 11, 2015
    Posts:
    54
    I got following script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour {  
    5.     public float movementSpeed;
    6.     public float rotationSpeed;
    7.     public float EXT_movementSpeedModifier;
    8.  
    9.     Transform selfTransform;
    10.     Vector3 selfPosition;
    11.     Vector3 selfRotation;
    12.  
    13.     float angleTurnal;
    14.    
    15.     void Start() {      
    16.         selfTransform = transform;
    17.         selfPosition = selfTransform.position;
    18.         selfRotation = selfTransform.rotation.eulerAngles;
    19.     }
    20.  
    21.     void Update() {
    22.         Vector3 mousePos = Input.mousePosition;
    23.         mousePos.z = 5.23f;
    24.        
    25.         Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
    26.         mousePos.x = mousePos.x - objectPos.x;
    27.         mousePos.y = mousePos.y - objectPos.y;
    28.        
    29.         float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
    30.         transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
    31.     }
    32.  
    33.     void FixedUpdate() {
    34.         // angleTurnal = selfTransform.eulerAngles.magnitude * Mathf.Deg2Rad;
    35.  
    36.         /*if (Input.GetKey (KeyCode.RightArrow)) {
    37.             selfRotation.z -= rotationSpeed;
    38.         }
    39.         if (Input.GetKey (KeyCode.LeftArrow)) {
    40.             selfRotation.z += rotationSpeed;
    41.         }*/
    42.  
    43.  
    44.         if (Input.GetKey (KeyCode.UpArrow)) {
    45.             selfPosition.x += (Mathf.Cos(angleTurnal) * movementSpeed) * Time.deltaTime;
    46.             selfPosition.y += (Mathf.Sin(angleTurnal) * movementSpeed) * Time.deltaTime;
    47.         }
    48.         if (Input.GetKey (KeyCode.DownArrow)) {
    49.             selfPosition.x += Mathf.Cos(angleTurnal) * Time.deltaTime;
    50.             selfPosition.y += Mathf.Sin(angleTurnal) * Time.deltaTime;  
    51.         }
    52.  
    53.         selfTransform.position = selfPosition;
    54.         selfTransform.rotation = Quaternion.Euler(selfRotation);
    55.        
    56.     }
    57. }
    58.  
    Code isn't mine, I just modified it to basics of my purposes. My idea behind it would be that GameObject (player in this case), turns around to current position of mouse, W would make it go forward, S would make it go forward but slower. But all it does when I press is W or E, is going to right faster or slower. Regardless where I have my mouse, it does turn, but it doesn't move there.
     
  2. Codermic

    Codermic

    Joined:
    Feb 12, 2015
    Posts:
    7
  3. Temp10101

    Temp10101

    Joined:
    Feb 11, 2015
    Posts:
    54
    I actually found this out, but it isn't working either:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour {  
    5.     public float movementSpeed;
    6.     public float rotationSpeed;
    7.     public float EXT_movementSpeedModifier;
    8.  
    9.     Transform selfTransform;
    10.     Vector3 selfPosition;
    11.     Vector3 selfRotation;
    12.  
    13.     float angleTurnal;
    14.    
    15.     void Start() {      
    16.         selfTransform = transform;
    17.         selfPosition = selfTransform.position;
    18.         selfRotation = selfTransform.rotation.eulerAngles;
    19.     }
    20.  
    21.     void Update() {
    22.         Vector3 mousePos = Input.mousePosition;
    23.         mousePos.z = 5.23f;
    24.        
    25.         Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
    26.         mousePos.x = mousePos.x - objectPos.x;
    27.         mousePos.y = mousePos.y - objectPos.y;
    28.        
    29.         float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
    30.         transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
    31.     }
    32.  
    33.     void FixedUpdate() {
    34.         // angleTurnal = selfTransform.eulerAngles.magnitude * Mathf.Deg2Rad;
    35.  
    36.         /*if (Input.GetKey (KeyCode.RightArrow)) {
    37.             selfRotation.z -= rotationSpeed;
    38.         }
    39.         if (Input.GetKey (KeyCode.LeftArrow)) {
    40.             selfRotation.z += rotationSpeed;
    41.         }*/
    42.  
    43.  
    44.         if (Input.GetKey (KeyCode.UpArrow)) { /*
    45.             selfPosition.x += (Mathf.Cos(angleTurnal) * movementSpeed) * Time.deltaTime;
    46.             selfPosition.y += (Mathf.Sin(angleTurnal) * movementSpeed) * Time.deltaTime; */
    47.  
    48.             selfPosition += Vector3.forward * Time.deltaTime;
    49.         }
    50.         if (Input.GetKey (KeyCode.DownArrow)) {
    51.             selfPosition.x += Mathf.Cos(angleTurnal) * Time.deltaTime;
    52.             selfPosition.y += Mathf.Sin(angleTurnal) * Time.deltaTime;  
    53.         }
    54.  
    55.         selfTransform.position = selfPosition;
    56.         selfTransform.rotation = Quaternion.Euler(selfRotation);
    57.        
    58.     }
    59. }
    60.  
     
  4. Temp10101

    Temp10101

    Joined:
    Feb 11, 2015
    Posts:
    54