Ok so I have been working on making my own character controller script and slowly adding functionality to it that I want. One thing I have hit a snag on is making a color, or an image or effect appear where the player clicks, which also happens to be the position where the player is headed to.
This is what I have so far.
using UnityEngine;
using System.Collections;
public class SimpleMove : MonoBehaviour {
private Transform myTransform; // this transform
private Vector3 destinationPosition; // The destination Point
private float destinationDistance; // The distance between myTransform and destinationPosition
public float moveSpeed; // The Speed the character will move
private Vector3 target;
private Vector3 lookAtPoint;
public float rotateSpeed;
public Texture cColor;
private SpriteRenderer spriteRenderer;
void Start () {
target = transform.position;
myTransform = transform; // sets myTransform to this GameObject.transform
destinationPosition = myTransform.position; // prevents myTransform reset
spriteRenderer = GetComponent<SpriteRenderer>();
if (!spriteRenderer){
Debug.Log("need a spriteRenderer");
} else {
spriteRenderer.transform = myTransform;
}
}
void Update () {
destinationDistance = Vector3.Distance(destinationPosition, myTransform.position);
if (Input.GetMouseButtonDown(0)) {
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Plane groundPlane = new Plane(Vector3.up, transform.position);
float rayDistance;
if (groundPlane.Raycast(ray, out rayDistance))
{
lookAtPoint = ray.GetPoint(rayDistance);
destinationPosition =ray.GetPoint(rayDistance);
}
Quaternion targetRotation = Quaternion.LookRotation(lookAtPoint - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0f * rotateSpeed);
target.z = transform.position.z;
}
myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
}
}
My problem ofcouse is I have no idea how to be using the spriterender correctly. I am getting an error telling me that [quote]
Property or indexer `UnityEngine.Component.transform’ cannot be assigned to (it is read only)
[/quote]
and all I was trying to do to start was make the sprite appear in the same spot as the player (the gameobject it is attatched to), this isn’t really important, my main priority or focus or desire really is to not have it appear but for just a split second where the player clicked and dissappear as well. Also I have looked into many other options such as OnGuiDraw, and many more but any help in getting this basics to work and explaining to me the code used would be much appreciated.