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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Having errors using AI follow script C#

Discussion in 'Scripting' started by Wils, Dec 4, 2012.

  1. Wils

    Wils

    Joined:
    Dec 4, 2012
    Posts:
    6
    This is the script I'm trying to convert from js to C#. It gave me compiler errors, Cannot convert 'UnityEngine.Vector3' expression to type 'UnityEngine.Quaternion'

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AI_Follow : MonoBehaviour
    5. {
    6.     public Transform target;
    7.     public Transform self;
    8.     public float moveSpeed = 3.0F;
    9.     public float rotationSpeed = 3.0F;
    10.    
    11.     void Awake()
    12.     {
    13.         self = transform;
    14.     }
    15.    
    16.     void Start ()
    17.     {
    18.         target = GameObject.FindWithTag("Player").transform;
    19.     }
    20.  
    21.     void Update ()
    22.     {
    23.         //Rotate to look at enemy player
    24.         self.rotation = Quaternion.Slerp(self.position, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
    25.        
    26.         self.position += self.forward * moveSpeed * Time.deltaTime;
    27.     }
    28. }
    29.  
     
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Code (csharp):
    1.  
    2.  
    3. self.rotation = Quaternion.Slerp(self.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
    4.  
     
  3. Wils

    Wils

    Joined:
    Dec 4, 2012
    Posts:
    6
    what should I change it to?
     
  4. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I posted what should be the correct code...
     
  5. Loius

    Loius

    Joined:
    Aug 16, 2012
    Posts:
    546
    In other words, you can't rotate from a position to a rotation. That's nonsense. You have to rotate from one rotation to another. Instead of '.position', you need to use '.rotation'