- Home /
Transform player to RaycastHit.point depending on speed float
I'm working on a Third Person cover system shooter and currently I'm working on the cover functionality.
I have two scripts: a character controller script and a cover detection script.
I have code on my cover detection script that detects cover with a raycast and creates a raycasthit.point at the point of collision with the collider. I also have a float on the same script called coverMoveSpeed that I'd like to multiply against time.delta so the player doesn't instantly snap to the cover, instead I'd like it to smoothly move there depending on the coverMoveSpeed float.
In my head I thought it would be as simple as this:
bool coverKey = Input.GetButton("Cover");
if (coverKey & coverMovement.canCover)
{
transform.position = coverMovement.hit.point + coverMoveSpeed * Time.deltaTime;
}
else
{
}
Here's my implementation
//on my cover detect script
public float coverMoveSpeed = 3.0f;
//Here's my character controller script
bool coverKey = Input.GetButton("Cover");
if (coverKey & coverMovement.canCover)
{
transform.position = coverMovement.hit.point;
}
else
{
}
However it doesnt compile, I know I'm close so I'm turning to the unity forums once again!
beep beep "Paging Dr. Stark" @LCStark
Answer by LCStark · Oct 05, 2018 at 09:14 PM
transform.position = coverMovement.hit.point + coverMoveSpeed * Time.deltaTime;
This will not compile - you are trying to add two different data types: coverMovement.hit.point
which is a Vector3
, and coverMoveSpeed * Time.deltaTime
, which is a float
.
You want to move your transform from its current position ( transform.position
) to the point where you found the collision with the cover ( coverMovement.hit.point
? ). You can use Vector3.MoveTowards
function to do that. (Vector3.MoveTowards documentation)
As you can see in the example given in the Scripting API documentation, it's pretty simple:
void Update() {
// The step size is equal to speed times frame time.
float step = speed * Time.deltaTime;
// Move our position a step closer to the target.
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
Your answer
Follow this Question
Related Questions
RayCastHit 2 Answers
moving objects with transform position 2 Answers
Changing the position of an object hit with a RaycastHit -1 Answers
Select Object and move on Right click Position - Error! 0 Answers
2D - Move an object 1 Answer