Search Unity

Need Smart People Help for 2D Aiming!

Discussion in 'Scripting' started by Claymore, Aug 21, 2014.

  1. Claymore

    Claymore

    Joined:
    Oct 17, 2012
    Posts:
    158
    Hi smart people,i tried all methods on this subject but they just cant seem to work for me,trust me topics out there cant help,but you guys can! All i want to do is this :

    for my 3D character in 2D Space,character head,arm,gun fallows mouse position and when mouse gets over his head or over to opposite side the whole character rotates to oposite side.
     
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    look man, it's not cool that you're asking people to write scripts for you. We're not here to do that, I am 100% sure that one of the topics you've looked at do indeed work but you've done something wrong.. That being said here's a example showing how to take the mouse position, convert it to world space and then look at it.. you can then do the rest for the other objects..

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ShootTest : MonoBehaviour {
    5.  
    6.     Vector3 mousePos;
    7.  
    8.     public Transform cursorObject;
    9.     public Transform head;
    10.  
    11.     void Update () {
    12.         Vector3 tempMousePosition = Input.mousePosition; //Get the temp mouse position
    13.         tempMousePosition.z = -Camera.main.transform.position.z; //Set the mouse position Z
    14.  
    15.         //MOUSE POSITION IS IN SCREEN SPACE SO WE NEED TO GET IT IN WORK SPACE:
    16.         mousePos = Camera.main.ScreenToWorldPoint (tempMousePosition);
    17.         cursorObject.position = mousePos;
    18.  
    19.         //Get look vector
    20.         Vector3 lookDirectionHead = mousePos - head.position;
    21.         //look at it
    22.         head.LookAt (lookDirectionHead);
    23.     }
    24. }
    25.  
    I've personally tested it and it works..
     
  3. Claymore

    Claymore

    Joined:
    Oct 17, 2012
    Posts:
    158
    Hey man,thanks for your help,i love people who can help noobs like me,well im not asking to write whole script. Well Im facing very VERY difficult problem,no its not to make player head FALLOW mouse,but to rotate whole character and character head to opposite side when mouse is over 90 degrees (or opposite side) like in video,i only want that to manage.

    Thanks man you are good at scripting,take care!
     
    Last edited: Aug 21, 2014
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    To be honest, that is not a difficult problem. You need to look at transform.rotation and transform.position. If the x component of the the position cursor is smaller than the x component of the player, you turn the player.
     
  5. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    your not facing a hard problem though, your just over thinking things. As Fluzing said, all you need to do is check if the cursor x position is less or greater than your characters position. if it's greater face it forwards, if it's less face him backwards
     
  6. Claymore

    Claymore

    Joined:
    Oct 17, 2012
    Posts:
    158
    Allright,please show me some script examples.
     
  7. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    you really need to go back and do the basics http://unity3d.com/learn

    I've already shown you how to get the mouse position.. surely you know how to get the players position? that's just a part of the transform component, that's one of the first things you learn.
     
    Fluzing likes this.