Out of scope question
i am making a mario clone in my class and i keep getting this error (11,52): error CS0029: Cannot implicitly convert type `UnityEngine.Vector3' to `float'
if anyone can help me out would be appreciated, does it mean you can't use a vector3 in this line of code? I wrote it out just like my teacher put it, but not sure why am getting the error - and of course I can't move on with the assignment without fixing this error - I have rewritten it many times just in case I missed something - but still get the error code.
thanks in advance
using UnityEngine; using System.Collections;
public class Camerafollow : MonoBehaviour {
public Transform target; public Transform leftBounds; public Transform rightBounds;
public float smoothDampTime = 0.15f; private float smoothDampVelocity = Vector3.zero;
private float camWidth, camHeight, levelMinX, levelMaxX;
// Use this for initialization void Start () {
camHeight = Camera.main.orthographicSize 2; camWidth = camHeight Camera.main.aspect;
float leftBoundsWidth = leftBounds.GetComponentInChildren ().bounds.size.x / 2; float rightBoundsWidth = rightBounds.GetComponentInChildren ().bounds.size.x / 2;
levelMinX = leftBounds.position.x + leftBoundsWidth + (camWidth / 2); levelMaxX = rightBounds.position.x - rightBoundsWidth - (camWidth / 2);
}
// Update is called once per frame void Update () {
if (target) {
float targetX = Mathf.Max (levelMinX, Mathf.Min (levelMaxX,target.position.x));
float x = Mathf.SmoothDamp(transform.position.x, targetX, ref smoothDampVelocity, smoothDampTime);
transform.position = new Vector3(x, transform.position.y, transform.position.z); } } }
here is a shorten version of my question -
this is the error I keep getting Assets/scripts/Camerafollow.cs(11,52): error CS0029: Cannot implicitly convert type UnityEngine.Vector3' to
float'
for this line or code - private float smoothDampVelocity = Vector3.zero;
Answer by NoseKills · Nov 23, 2015 at 07:23 PM
Doesn't that look weird to you :) ? float x = Vector3.something
Ask yourself this: what do you expect the value of some float to become when you try to put a Vector3 (consisting of 3 floats) into it ? I guess '0' in this case since the name of the Vector3 variable happens to be "zero", but just as well you could be doing
private float smoothDampVelocity = new Vector3(-3, 6, 10);
What would the float value become then ? -3, 6 or 10 .... or 10-3+6 ? And how could the compiler know ?
The compiler doesn't know how to make 1 float out of the Vector3 you are trying to put into it. If you want your float to be "0", just do private float smoothDampVelocity = 0f;
Otherwise you have to do some more coding to tell the compiler what number to store in your float variable every time you assign a Vector3 into it.
There are strict rules for what types pf data you can store in different types of variables and what types can be converted to other types
Answer by vespa39 · Nov 27, 2015 at 10:10 PM
I appreciate you answering my question - am still very new to scripting, I will try what you suggest and hope it works -
Follow this Question
Related Questions
Normalize(); help 0 Answers
Problem with Crouch Script - 3D C# 2 Answers
What is causing my Spawner script to break Unity? 1 Answer
Position is infinite 0 Answers
NullReferenceException: Object reference not set to an instance of an object 1 Answer