Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Click Effect! How to produce color or sprite on click.

Discussion in 'Scripting' started by Codinablack, May 31, 2016.

  1. Codinablack

    Codinablack

    Joined:
    May 8, 2016
    Posts:
    12
    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.

    Code (CSharp):
    1.  using UnityEngine;
    2. using System.Collections;
    3. public class SimpleMove : MonoBehaviour {
    4.      private Transform myTransform;                // this transform
    5.     private Vector3 destinationPosition;        // The destination Point
    6.     private float destinationDistance;            // The distance between myTransform and destinationPosition
    7.     public float moveSpeed;                        // The Speed the character will move
    8.     private Vector3 target;
    9.     private Vector3 lookAtPoint;
    10.     public float rotateSpeed;
    11.     public Texture cColor;
    12.     private SpriteRenderer spriteRenderer;
    13.      void Start () {
    14.          target = transform.position;
    15.          myTransform = transform;                            // sets myTransform to this GameObject.transform
    16.         destinationPosition = myTransform.position;            // prevents myTransform reset
    17.         spriteRenderer = GetComponent<SpriteRenderer>();
    18.         if (!spriteRenderer){
    19.             Debug.Log("need a spriteRenderer");
    20.         } else {
    21.             spriteRenderer.transform = myTransform;
    22.         }
    23.      }
    24.    
    25.      void Update () {
    26.          destinationDistance = Vector3.Distance(destinationPosition, myTransform.position);
    27.          if (Input.GetMouseButtonDown(0)) {
    28.              target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    29.              Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    30.              Plane groundPlane = new Plane(Vector3.up, transform.position);
    31.               float rayDistance;
    32.                 if (groundPlane.Raycast(ray, out rayDistance))
    33.                 {
    34.                     lookAtPoint = ray.GetPoint(rayDistance);
    35.                     destinationPosition =ray.GetPoint(rayDistance);
    36.                 }
    37.  
    38.                 Quaternion targetRotation = Quaternion.LookRotation(lookAtPoint - transform.position, Vector3.up);
    39.                 transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0f * rotateSpeed);
    40.              target.z = transform.position.z;
    41.          }
    42.          myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
    43.      }  
    44. }
    My problem ofcouse is I have no idea how to be using the spriterender correctly. I am getting an error telling me that
    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.
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    It's says exactly what the error is, you can't simply assign a transform to an another transform. Just think about how many things it could break. If you want to keep the position, use Vector3 position = transform.position;
    you want the rotation? Quaternion rot = transform.rotation;

    EDIT:
    by the way, your title is a tad bit unrelated to the problem
     
  3. Codinablack

    Codinablack

    Joined:
    May 8, 2016
    Posts:
    12
    Thanks for the reply but I hate to point out that you didn't help at all. There must be a miscommunication, but I specifically said what I was trying to do (The name of the thread, doesn't matter which one), and what my problem was, and you didn't really tell me how to fix it.

    Vector3 position = transform.position

    take a look at line 26, did you even look at the code? I am obviously a very new person who barely knows the syntax of C# and not much more, let alone the api for unity yet. I am learning, very much learning. And what you told me was nothing, it was less than nothing, you gave me some little bits of code that are already used in my script as it is. How about breaking down what it is you told me because if thats the code I need to make it work and I'm already doing that then obviously i am not completely understanding what it is I am doing. I come here for help, pls don't mistreat me and boost your post count and try to make it all look as if you were being helpful.

    My title will stay as it is because I am still hoping someone can show me such as simpler way to make a small effect appear where I just click without me having to have it overcomplicated by using a spriterenderer.
     
  4. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Your error states, that youre trying to assign a transform to an another transform, on line 15, I tried to help yoi fix your code
    The rest of the script I actually read surprise surprise, but I didnt had the time to help you. If you want to get the point a player is looking at, you neee to raycast. You have the ray, the only thing left to do is to sikply cast it, using:
    Code (CSharp):
    1. HitInfo hit;
    2. If (Physics.Raycast(ray, out hit)) {
    3.   And the point is hit.point
    4. }
    But theres a useful webpage out there for starters, and for those who seek answee for their quiestion, and yes, Im talking about google, because if you type in "get point player is looking at unity) then you get plenty of material to work with. And then you can use a sprite renderer, an object, a particle effect, whatever comes in your mind
     
  5. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Next time Ill give you a lmgtfy link with how to not be assholes to the guy who tries to help