Search Unity

Using Raycast with iTween movement

Discussion in 'Scripting' started by GenPhi, Feb 12, 2016.

  1. GenPhi

    GenPhi

    Joined:
    Dec 27, 2014
    Posts:
    12
    Hi,

    I am trying to use Raycast in a player movement script to detect objects in front of them to stop movement in that direction. When the game starts, the player can not move forward, only left and right. Once I move to the left or right, I can move forward once then I have to move left or right again to move forward. On top of that, the raycast doesn't seem to work in the CanMove method. The debug says I hit something but yet I can still move.
    Something easy I'm sure I'm overlooking but I'm stuck...

    using UnityEngine;
    using System.Collections;

    public class PlayerMovement : MonoBehaviour {

    private float moveSpeed = 20f;
    public float moveDistance;
    private bool canMove;
    private float swipeDistanceX;
    private float swipeDistanceY;
    private Vector2 startPos;
    RaycastHit hit;

    void Start () {
    canMove = true;
    }

    void Update ()
    {
    Vector3 startRay = transform.position;
    Vector3 endRay = transform.forward * 3f;
    Debug.DrawRay(startRay, endRay);
    if (Input.GetKey(KeyCode.UpArrow))
    {
    if(transform.rotation.y != 0)
    {
    iTween.RotateTo(gameObject, iTween.Hash("y", 0f, "time", .01f, "onComplete", "MovePlayer", "onCompleteTarget", gameObject));
    }
    }
    else if (Input.GetKey(KeyCode.DownArrow))
    {
    if(transform.rotation.y != 180)
    {
    iTween.RotateTo(gameObject, iTween.Hash("y", 180f, "time", .01f, "onComplete", "MovePlayer", "onCompleteTarget", gameObject));
    }
    }
    else if (Input.GetKey(KeyCode.RightArrow))
    {
    if(transform.rotation.y != 90)
    {
    iTween.RotateTo(gameObject, iTween.Hash("y", 90f,"time", .01f, "onComplete", "MovePlayer", "onCompleteTarget", gameObject));
    }
    }
    else if (Input.GetKey(KeyCode.LeftArrow))
    {
    if(transform.rotation.y != -90)
    {
    iTween.RotateTo(gameObject, iTween.Hash("y", -90f, "time", .01f, "onComplete", "MovePlayer", "onCompleteTarget", gameObject));
    }
    }
    }

    void MovePlayer()
    {
    if(canMove)
    {
    iTween.MoveAdd(gameObject, iTween.Hash("z", 3, "speed", moveSpeed));
    }
    canMove = CanMove();
    }

    bool CanMove()
    {

    Vector3 startRay = transform.position * 1.5f;
    Vector3 endRay = transform.forward * 3f;
    if (Physics.Raycast(startRay, endRay, out hit, 5))
    {

    if(hit.collider.gameObject.tag == "Player")
    {
    return true;
    }
    else
    {
    Debug.Log("I hit " + hit.collider.gameObject.name);
    return false;
    }
    }
    else
    {
    return true;
    }
    }

    }
    [/code]
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,528
    You missed the opening code tag.

    Also, is there a reason you're using iTween during Update like that?
     
  3. GenPhi

    GenPhi

    Joined:
    Dec 27, 2014
    Posts:
    12
    I'm sorry. What do you mean by Opening code tag? :oops:

    I've been using iTween because I can't use Unity Animations and compile to iOS. There is a bug in the latest build where the animated objects disappear. Why I'm using it in Update... I'm just waiting on a key press and calling the iTweenRotateTo. The iTween calls the MovePlayer() when it completes it's rotation. Do you see a better way? As for the CanMove(). I've been playing with it so I have the bool and also the method... just trying to figure this out.