- Home /
I need help with dragging objects
Im currently creating a FPS game and I wanted to create this moving and throwing objects option, I've created a raycast but I really dont know how to move the object based on the raycast, this is my script: private void Interaction() { Vector3 origin = transform.position; Vector3 direction = transform.forward; Debug.DrawRay(origin, direction * 2.5f, Color.blue); Ray ray = new Ray(origin, direction);
if(Physics.Raycast(ray, out RaycastHit raycastHit, 2.5f))
{
raycastHit.collider.GetComponent<Rigidbody>().transform.position = ;
}
}
I really dont know what to do from there, thanks to anyone who helps.
Answer by Llama_w_2Ls · Jul 25, 2020 at 01:23 PM
You could get the hit.point of your raycast and in the update, put an if statement that says if Im holding down the mouse and my raycast is hitting a grabable object (by checking the tag) set that objects position to the raycast hit.point. As you move your mouse while holding down, the raycast should move up, meaning the cube should move up too. Ill try and provide a coded demonstration later if you need it. Hope that gives you a starting point.
Yeah I've tried that and it worked thanks, by the way, currently, I created this movement where you can slide down slopes, and you do have extra speed when sliding down, but after you finish sliding down the slope you immediately go to a halt once you reach the ground, I hope that made sense, do you know how I can keep up the speed after going down the slope? Thanks @Llama_w_2Ls
Like momentum? Since im guessing youre still using the rb.velocity, the only things that would slow you down are drag and friction. If youre immediately co$$anonymous$$g to a halt then that means that you probably scripted your movement slightly incorrectly. If you want to keep the momentum, when sliding down a slope, you should be increasing the velocity of your rb, not setting it to a value although a max value would be a good idea. $$anonymous$$g.`//In update method if (rb.velocity < maxspeed && OnSlope == true){ rb.velocity += speedincrease}` else if (rb.velocity > normalspeed && OnSlope == false){ rb.velocity += -speedincrease}
If you want to keep momentum then just put like whilst im holding down 'w', dont do the else if which decreases my speed slowly.
Your answer
Follow this Question
Related Questions
scripting problem 0 Answers
script problem even though i have no errors 2 Answers
Can you figure out raycast origin position from RacyastHit? 2 Answers
Ray origin moving with player movement and mouse movement 2 Answers
Using Ray cast for click to move 2 Answers