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

FPS -- Need help

Discussion in 'Scripting' started by SlowLorris, Jun 15, 2014.

  1. SlowLorris

    SlowLorris

    Joined:
    Feb 21, 2013
    Posts:
    4
    Hi there,

    I'm trying to make FPS Game, just starting out. And these is my problem:

    1. When I use this code, the Debug.DrawLine draws an straight line (as expected), but Debug.DrawRay does not:

    Code (CSharp):
    1. if (Input.GetMouseButton (0))
    2.         {
    3.             Vector3 direction = transform.TransformDirection (Vector3.forward);
    4.             RaycastHit hit;
    5.            
    6.             if (Physics.Raycast (transform.position, direction, out hit, 100))
    7.             {
    8.  
    9.                 Debug.DrawRay (transform.position, hit.point, Color.blue);
    10.                 Debug.DrawLine (transform.position, hit.point, Color.red);
    11.  
    12.             }
    13.            
    14.         }
    ( I have attached a picture to show what I mean.) Why is it happening? Isn't it supposed to just draw a straight ray?

    2. When you actually use a Soldier Model, how do you make the model's head and hand properly animate/rotate/move with respect to the gun aim?

    Thanks ...
     

    Attached Files:

  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    DrawRay needs an origin and direction while DrawLine need Start and End point.
    hit.point is not the right direction, if you want the direct of the vector3 between your gun and the hit point, subtract them from each other.
    typically I would create a ray on top like
    Ray ray = new Ray(transform.position, Vector3.forward);
    and then use that for the cast and the debug
    if(Physics.Raycast (ray, out hit,...)
    and
    DeBug.DrawRay(ray.origin, ray.direction)
     
    SlowLorris likes this.
  3. SlowLorris

    SlowLorris

    Joined:
    Feb 21, 2013
    Posts:
    4
    Thanks ... but how do I go with the Model body part movement/rotation? I think I need to use Inverse Kinematics thing, but I couldn't find any tutorial teaching such thing, do you know of one?

    (I did look at the Bootcamp project, it looks like it does use IK, but the project is just too bigfor me to go line by line, file by file and try to understand things ... :( )

    Thanks ....
     
  4. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    you can put a mouselook script on your spine bone (or the bone you want to follow) of you character
     
    SlowLorris likes this.
  5. SlowLorris

    SlowLorris

    Joined:
    Feb 21, 2013
    Posts:
    4
    Thanks ... I will try that and report back ...
    Thanks again ....