I want my character to move towards an enemy in my case and for that movement to be seen and smooth rather than it to be a button press and he is then automatically.
So like when a button is pressed it move towards the enemy like how enemys can move towards player or towrds waypoints. I know it something to do with the transform.position method or even translate and I’ve tried these but i still get a snap movement if you know i mean.
What is the right way to produce the type of movement I’ve described i’m looking to achieve. In the code below I’m using Vector3.Lerp and maybe I’m using it wrong , but in
the past I’ve used Vector3.MoveTowards and i still get the snap movement
Heres the code if you want to look at it:
function OnDrawGizmosSelected ()
{
Gizmos.color = Color.green;
Gizmos.DrawWireSphere (transform.position, radius);
// Draws a blue line from this transform to the target
Gizmos.color = Color.blue;
Gizmos.DrawLine (transform.position, target.position);
}
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
// gets the character controller and contains it information inside variable named controller
var distance = Vector3.Distance(target.position,transform.position);
startTime = Time.time;
// Rotate around y - axis
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
// Move forward / backward
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
var curSpeed : float = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
if(controller.isGrounded) // if the character controller is colliding with anything that is
// collidiable
{
controller.height = standardHeight ;
controller.center.y=0.05; // repositions the character controller to 0.05 on the y axis
if (Input.GetButton("Jump")) // If the jump button is held down
{
moveDirection.y= jumpSpeed;
controller.height = jumpHeight;
controller.center.y=0.06;
}
}
if(Input.GetButtonUp("Jump")) // If jump button has been pressed and released
{
controller.height = middleHeight;
controller.center.y =0.04;
}
if( jumpSpeed==70.0 && !controller.isGrounded )// if the jump speed if 70.0 and its not
// colliding with anything or not on the ground
{
if(Input.GetButtonDown("Fire2") && gravity ==90.0 && distance<radius)// if button has been pressed and released
// and gravity at this point is 90.0 then do below
{
if(Physics.Linecast(transform.position,target.position))
{
var step = bouncespeed * extraspeed* Time.deltaTime;
gravity =5.0;
// Move our position a step closer to the target.
transform.position = Vector3.Lerp(transform.position,target.position, step);
animation.CrossFade("Bounce");
}
}
}
if(gravity ==5.0)
{
gravity =90.0;
}
controller.Move(moveDirection * Time.deltaTime);
moveDirection.y -= gravity * Time.deltaTime;
}