- Home /
Click to Move script problem
Hello guys i need your help. I have just started scripting in c# and whole unity engine is new for me. Can someone help me with my click to move script? I followed this tutorial https://www.youtube.com/watch?v=EAiV7gZv69M&feature=youtu.be but my "character" doesnt move towards location. Hers is code : using UnityEngine; using System.Collections;
public class movement2 : MonoBehaviour {
public float speed;
public CharacterController controller;
private Vector3 position;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButton (0))
{
locatePosition();
}
moveToPosition();
}
void locatePosition()
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit, 1000))
{
position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
Debug.Log(position);
}
}
void moveToPosition(){
Quaternion newRotation = Quaternion.LookRotation(position-transform.position, Vector3.forward);
newRotation.x = 0f;
newRotation.z = 0f;
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, Time.deltaTime * 10);
controller.SimpleMove(transform.forward * speed);
}
}
Thank you for all help.
Answer by deltamish · Sep 14, 2014 at 01:57 PM
Hi try using
controller.SimpleMove((position - transform.position) * speed);
It will move the object towards the target with respect to its realtive position
I´ve tried it out but nothing. Cube is rotating towards point where i clicked (sometimes acting strange) but still doesnt move.
Your answer
Follow this Question
Related Questions
How to make player stop a certain distance away from enemy? 1 Answer
How to move to specific point with character controller 1 Answer
click to move script goes through objects 0 Answers
Unity Objects blurring when camera moves 2 Answers
Character Controller moves forever after colliding with object 1 Answer