Question by
fergie77 · Nov 11, 2015 at 01:27 PM ·
c#raycastingfootball
Raycasting a "tractor beam" in a football game
I'm making a football (soccer) game and I'm trying to use a raycast from the players feet to the ball. I want it to recognise the ball at a set distance from the player and drag the ball closer to the player's feet, then set a spring joint. I have this code so far, but this sets the spring joint to the balls current position and I don't know how to make it come closer!
Thanks for your help!
using UnityEngine;
using System.Collections;
public class rayCast : MonoBehaviour {
public Collider coll;
private bool springWaiting = true;
// Update is called once per frame
private void Update () {
RaycastHit hit;
if (Physics.Raycast (transform.position, transform.forward, out hit, 0.5f))
{
if (hit.collider.gameObject.tag == "ball")
{
if (springWaiting == true) //if spring is not active
{
var springJoint = gameObject.AddComponent<SpringJoint> ();
springJoint.connectedBody = coll.attachedRigidbody;
springJoint.maxDistance = 0f;
springJoint.damper = 0;
springJoint.spring = 1000000;
}
springWaiting = false;
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Pinball Flippers Control with multitouch 0 Answers
Raycast positioning problem 1 Answer
Why is my logic happening 9 times? 0 Answers